Comparing version
@@ -88,2 +88,4 @@ 'use strict'; | ||
options.beforeRedirect = options.beforeRedirect || ((redirectMethod, statusCode, location, resHeaders, redirectOptions, next) => next()); | ||
if (options.baseUrl) { | ||
@@ -158,5 +160,6 @@ url = internals.resolveUrl(options.baseUrl, url); | ||
const statusCode = res.statusCode; | ||
const redirectMethod = internals.redirectMethod(statusCode, uri.method, options); | ||
if (redirects === false || | ||
[301, 302, 307, 308].indexOf(statusCode) === -1) { | ||
!redirectMethod) { | ||
@@ -168,5 +171,2 @@ return finishOnce(null, res); | ||
const redirectMethod = (statusCode === 301 || statusCode === 302 ? 'GET' : uri.method); | ||
let location = res.headers.location; | ||
res.destroy(); | ||
@@ -178,2 +178,3 @@ | ||
let location = res.headers.location; | ||
if (!location) { | ||
@@ -188,15 +189,13 @@ return finishOnce(Boom.badGateway('Received redirection without location', _trace)); | ||
const redirectOptions = Hoek.cloneWithShallow(options, internals.shallowOptions); | ||
redirectOptions.payload = shadow || options.payload; // shadow must be ready at this point if set | ||
redirectOptions.payload = shadow || options.payload; // shadow must be ready at this point if set | ||
redirectOptions.redirects = --redirects; | ||
if (options.beforeRedirect) { | ||
options.beforeRedirect(redirectMethod, statusCode, location, redirectOptions); | ||
} | ||
return options.beforeRedirect(redirectMethod, statusCode, location, res.headers, redirectOptions, () => { | ||
const redirectReq = this.request(redirectMethod, location, redirectOptions, finishOnce, _trace); | ||
const redirectReq = this.request(redirectMethod, location, redirectOptions, finishOnce, _trace); | ||
if (options.redirected) { | ||
options.redirected(statusCode, location, redirectReq); | ||
} | ||
if (options.redirected) { | ||
options.redirected(statusCode, location, redirectReq); | ||
} | ||
}); | ||
}; | ||
@@ -287,2 +286,24 @@ | ||
internals.redirectMethod = function (code, method, options) { | ||
switch (code) { | ||
case 301: | ||
case 302: | ||
return 'GET'; | ||
case 303: | ||
if (options.redirect303) { | ||
return 'GET'; | ||
} | ||
break; | ||
case 307: | ||
case 308: | ||
return method; | ||
} | ||
return null; | ||
}; | ||
// read() | ||
@@ -289,0 +310,0 @@ |
{ | ||
"name": "wreck", | ||
"description": "HTTP Client Utilities", | ||
"version": "7.2.1", | ||
"version": "8.0.0", | ||
"repository": "git://github.com/hapijs/wreck", | ||
@@ -20,3 +20,3 @@ "main": "lib/index", | ||
"devDependencies": { | ||
"code": "2.x.x", | ||
"code": "3.x.x", | ||
"lab": "10.x.x", | ||
@@ -23,0 +23,0 @@ "require-reload": "0.2.x" |
@@ -44,3 +44,3 @@  | ||
redirects: 3, | ||
beforeRedirect: function (redirectMethod, statusCode, location, redirectOptions) {}, | ||
beforeRedirect: function (redirectMethod, statusCode, location, resHeaders, redirectOptions, next) { return next() }, | ||
redirected: function (statusCode, location, req) {}, | ||
@@ -88,9 +88,13 @@ timeout: 1000, // 1 second, default: unlimited | ||
- `redirects` - The maximum number of redirects to follow. | ||
- `beforeRedirect` - A callback function that is called before a redirect is triggered, using the signature function (redirectMethod, statusCode, location, redirectOptions) where: | ||
- `redirectMethod` - A string specifying the redirect method. | ||
- `redirect303` - if `true`, treats HTTP 303 status code the same way as a 301/302. Defaults to no redirection on 303. | ||
- `beforeRedirect` - A callback function that is called before a redirect is triggered, using the signature | ||
`function(redirectMethod, statusCode, location, resHeaders, redirectOptions, next)` where: | ||
- `redirectMethod` - A string specifying the redirect method. | ||
- `statusCode` - HTTP status code of the response that triggered the redirect. | ||
- `location` - The redirect location string. | ||
- `resHeaders` - An object with the headers received as part of the redirection response. | ||
- `redirectOptions` - Options that will be applied to the redirect request. Changes to this object are applied to the redirection request. | ||
- `next` - the callback function called to perform the redirection using signature `function()`. | ||
- `redirected` - A callback function that is called when a redirect was triggered, using the signature `function(statusCode, location, req)` where: | ||
- `statusCode` - HTTP status code of the response that triggered the redirect. | ||
- `location` - The redirect location string. | ||
- `redirectOptions` - Options that will be applied to the redirect request. | ||
- `redirected` - A callback function that is called when a redirect was triggered, using the signature `function (statusCode, location, req)` where: | ||
- `statusCode` - HTTP status code of the response that triggered the redirect. | ||
- `location` - The redirected location string. | ||
@@ -97,0 +101,0 @@ - `req` - The new [ClientRequest](http://nodejs.org/api/http.html#http_class_http_clientrequest) object which replaces the one initially returned. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
146019
1.91%2151
2.72%301
1.35%