follow-redirects 1.14.9 → 1.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of follow-redirects might be problematic. Click here for more details.
- package/README.md +8 -1
- package/index.js +37 -13
- package/package.json +1 -1
package/README.md
CHANGED
@@ -63,10 +63,17 @@ const { http, https } = require('follow-redirects');
|
|
63
63
|
|
64
64
|
const options = url.parse('http://bit.ly/900913');
|
65
65
|
options.maxRedirects = 10;
|
66
|
-
options.beforeRedirect = (options,
|
66
|
+
options.beforeRedirect = (options, response, request) => {
|
67
67
|
// Use this to adjust the request options upon redirecting,
|
68
68
|
// to inspect the latest response headers,
|
69
69
|
// or to cancel the request by throwing an error
|
70
|
+
|
71
|
+
// response.headers = the redirect response headers
|
72
|
+
// response.statusCode = the redirect response code (eg. 301, 307, etc.)
|
73
|
+
|
74
|
+
// request.url = the requested URL that resulted in a redirect
|
75
|
+
// request.headers = the headers in the request that resulted in a redirect
|
76
|
+
// request.method = the method of the request that resulted in a redirect
|
70
77
|
if (options.hostname === "example.com") {
|
71
78
|
options.auth = "user:password";
|
72
79
|
}
|
package/index.js
CHANGED
@@ -270,25 +270,30 @@ RedirectableRequest.prototype._performRequest = function () {
|
|
270
270
|
// If specified, use the agent corresponding to the protocol
|
271
271
|
// (HTTP and HTTPS use different types of agents)
|
272
272
|
if (this._options.agents) {
|
273
|
-
var scheme = protocol.
|
273
|
+
var scheme = protocol.slice(0, -1);
|
274
274
|
this._options.agent = this._options.agents[scheme];
|
275
275
|
}
|
276
276
|
|
277
|
-
// Create the native request
|
277
|
+
// Create the native request and set up its event handlers
|
278
278
|
var request = this._currentRequest =
|
279
279
|
nativeProtocol.request(this._options, this._onNativeResponse);
|
280
|
-
this._currentUrl = url.format(this._options);
|
281
|
-
|
282
|
-
// Set up event handlers
|
283
280
|
request._redirectable = this;
|
284
|
-
for (var
|
285
|
-
request.on(
|
281
|
+
for (var event of events) {
|
282
|
+
request.on(event, eventHandlers[event]);
|
286
283
|
}
|
287
284
|
|
285
|
+
// RFC7230§5.3.1: When making a request directly to an origin server, […]
|
286
|
+
// a client MUST send only the absolute path […] as the request-target.
|
287
|
+
this._currentUrl = /^\//.test(this._options.path) ?
|
288
|
+
url.format(this._options) :
|
289
|
+
// When making a request to a proxy, […]
|
290
|
+
// a client MUST send the target URI in absolute-form […].
|
291
|
+
this._currentUrl = this._options.path;
|
292
|
+
|
288
293
|
// End a redirected request
|
289
294
|
// (The first request must be ended explicitly with RedirectableRequest#end)
|
290
295
|
if (this._isRedirect) {
|
291
|
-
// Write the request entity and end
|
296
|
+
// Write the request entity and end
|
292
297
|
var i = 0;
|
293
298
|
var self = this;
|
294
299
|
var buffers = this._requestBodyBuffers;
|
@@ -362,10 +367,21 @@ RedirectableRequest.prototype._processResponse = function (response) {
|
|
362
367
|
return;
|
363
368
|
}
|
364
369
|
|
370
|
+
// Store the request headers if applicable
|
371
|
+
var requestHeaders;
|
372
|
+
var beforeRedirect = this._options.beforeRedirect;
|
373
|
+
if (beforeRedirect) {
|
374
|
+
requestHeaders = Object.assign({
|
375
|
+
// The Host header was set by nativeProtocol.request
|
376
|
+
Host: response.req.getHeader("host"),
|
377
|
+
}, this._options.headers);
|
378
|
+
}
|
379
|
+
|
365
380
|
// RFC7231§6.4: Automatic redirection needs to done with
|
366
381
|
// care for methods not known to be safe, […]
|
367
382
|
// RFC7231§6.4.2–3: For historical reasons, a user agent MAY change
|
368
383
|
// the request method from POST to GET for the subsequent request.
|
384
|
+
var method = this._options.method;
|
369
385
|
if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" ||
|
370
386
|
// RFC7231§6.4.4: The 303 (See Other) status code indicates that
|
371
387
|
// the server is redirecting the user agent to a different resource […]
|
@@ -413,10 +429,18 @@ RedirectableRequest.prototype._processResponse = function (response) {
|
|
413
429
|
}
|
414
430
|
|
415
431
|
// Evaluate the beforeRedirect callback
|
416
|
-
if (typeof
|
417
|
-
var responseDetails = {
|
432
|
+
if (typeof beforeRedirect === "function") {
|
433
|
+
var responseDetails = {
|
434
|
+
headers: response.headers,
|
435
|
+
statusCode: statusCode,
|
436
|
+
};
|
437
|
+
var requestDetails = {
|
438
|
+
url: currentUrl,
|
439
|
+
method: method,
|
440
|
+
headers: requestHeaders,
|
441
|
+
};
|
418
442
|
try {
|
419
|
-
|
443
|
+
beforeRedirect(this._options, responseDetails, requestDetails);
|
420
444
|
}
|
421
445
|
catch (err) {
|
422
446
|
this.emit("error", err);
|
@@ -557,8 +581,8 @@ function createErrorType(code, defaultMessage) {
|
|
557
581
|
}
|
558
582
|
|
559
583
|
function abortRequest(request) {
|
560
|
-
for (var
|
561
|
-
request.removeListener(
|
584
|
+
for (var event of events) {
|
585
|
+
request.removeListener(event, eventHandlers[event]);
|
562
586
|
}
|
563
587
|
request.on("error", noop);
|
564
588
|
request.abort();
|