diff --git a/README.md b/README.md index 82570d1..2d92928 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ The url as a string (e.g. `http://example.com`). It must be fully qualified and - `agent` - (default: `false`) controlls keep-alive (see http://nodejs.org/api/http.html#http_http_request_options_callback) - `followRedirects` - (default: `false`) - if true, redirects are followed (note that this only affects the result in the callback) - `maxRedirects` - (default: `Infinity`) - limit the number of redirects allowed. + - `allowRedirectHeaders` (default: `null`) - an array of headers allowed for redirects (none if `null`). - `gzip` (default: `false`) - automatically accept gzip and deflate encodings. This is kept completely transparent to the user. - `cache` - (default: `null`) - `'memory'` or `'file'` to use the default built in caches or you can pass your own cache implementation. - `timeout` (default: `false`) - times out if no response is returned within the given number of milliseconds. diff --git a/index.js b/index.js index 242f906..0ea4ed9 100644 --- a/index.js +++ b/index.js @@ -120,7 +120,18 @@ function request(method, url, options, callback) { // This fixes a problem where a POST to http://example.com // might result in a GET to http://example.co.uk that includes "content-length" // as a header - options.headers = {}; + var headers = caseless(options.headers), redirectHeaders = {}; + if (options.allowRedirectHeaders) { + var headerName, headerValue; + for (var i = 0; i < options.allowRedirectHeaders.length; i++) { + headerName = options.allowRedirectHeaders[i]; + headerValue = headers.get(headerName); + if (headerValue) { + redirectHeaders[headerName] = headerValue; + } + } + } + options.headers = redirectHeaders; return request(duplex ? 'GET' : method, resolveUrl(urlString, res.headers.location), options, callback); } else { return callback(null, res); diff --git a/test/index.js b/test/index.js index d275dc2..d5c8242 100644 --- a/test/index.js +++ b/test/index.js @@ -79,3 +79,11 @@ request('GET', CACHED, {cache: 'file'}, function (err, res) { }, 1000); }); }); + +request('GET', 'https://api.github.com/repos/isaacs/npm', {allowRedirectHeaders: ['User-Agent'], followRedirects: true, headers: {'User-Agent': 'http-basic'}}, function (err, res) { + if (err) throw err; + + console.log('response I'); + assert(res.statusCode === 200); + res.body.resume(); +}); \ No newline at end of file