From 3bb98d6bea7c1e6040dbb85b9876704a10a98a06 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Wed, 28 Aug 2013 20:50:46 +0200 Subject: [PATCH] Add X-CORS-Redirect-# header for debugging --- lib/cors-anywhere.js | 5 +++++ lib/help.txt | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/cors-anywhere.js b/lib/cors-anywhere.js index b7b45c1..a506bab 100644 --- a/lib/cors-anywhere.js +++ b/lib/cors-anywhere.js @@ -142,6 +142,11 @@ function onProxyResponse(req, res, response) { // Exclude 307 & 308, because they are rare, and require preserving the method + request body requestState.redirectCount_ = requestState.requestState_ + 1 || 1; if (requestState.redirectCount_ <= requestState.maxRedirects) { + // Handle redirects within the server, because some clients (e.g. Android Stock Browser) + // cancel redirects. + // Set header for debugging purposes. Do not try to parse it! + res.setHeader('X-CORS-Redirect-' + requestState.redirectCount_, statusCode + ' ' + locationHeader); + req.method = 'GET'; requestState.location = parseURL(locationHeader); proxyRequest(req, res, proxy); diff --git a/lib/help.txt b/lib/help.txt index c1a8594..5fac4c9 100644 --- a/lib/help.txt +++ b/lib/help.txt @@ -10,8 +10,13 @@ If the protocol is omitted, it defaults to http (https if port 443 is specified) Cookies are disabled and stripped from requests. -The requested URL is available in the X-Request-URL response header. Non-existence of this -header implies that the requested URL was not recognized. +Redirects are automatically followed. For debugging purposes, each followed redirect results +in the addition of a X-CORS-Redirect-n header, where n starts at 1. This header is not +accessible by the XMLHttpRequest API. + +The final request URL is available in the X-Request-URL response header. Non-existence of +this header implies that the requested URL was not recognized. + To prevent the use of the proxy for casual browsing, the API requires either the Origin or the X-Requested-With header to be set. To avoid unnecessary preflight (OPTIONS) requests,