follow-redirects 1.10.0 → 1.13.0
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 +6 -3
- package/debug.js +9 -0
- package/index.js +16 -7
- package/package.json +7 -4
package/README.md
CHANGED
@@ -6,6 +6,7 @@ Drop-in replacement for Node's `http` and `https` modules that automatically fol
|
|
6
6
|
[](https://travis-ci.org/follow-redirects/follow-redirects)
|
7
7
|
[](https://coveralls.io/r/follow-redirects/follow-redirects?branch=master)
|
8
8
|
[](https://www.npmjs.com/package/follow-redirects)
|
9
|
+
[](https://github.com/sponsors/RubenVerborgh)
|
9
10
|
|
10
11
|
`follow-redirects` provides [request](https://nodejs.org/api/http.html#http_http_request_options_callback) and [get](https://nodejs.org/api/http.html#http_http_get_options_callback)
|
11
12
|
methods that behave identically to those found on the native [http](https://nodejs.org/api/http.html#http_http_request_options_callback) and [https](https://nodejs.org/api/https.html#https_https_request_options_callback)
|
@@ -27,13 +28,14 @@ You can inspect the final redirected URL through the `responseUrl` property on t
|
|
27
28
|
If no redirection happened, `responseUrl` is the original request URL.
|
28
29
|
|
29
30
|
```javascript
|
30
|
-
https.request({
|
31
|
+
const request = https.request({
|
31
32
|
host: 'bitly.com',
|
32
33
|
path: '/UHfDGO',
|
33
34
|
}, response => {
|
34
35
|
console.log(response.responseUrl);
|
35
36
|
// 'http://duckduckgo.com/robots.txt'
|
36
37
|
});
|
38
|
+
request.end();
|
37
39
|
```
|
38
40
|
|
39
41
|
## Options
|
@@ -61,8 +63,9 @@ const { http, https } = require('follow-redirects');
|
|
61
63
|
|
62
64
|
const options = url.parse('http://bit.ly/900913');
|
63
65
|
options.maxRedirects = 10;
|
64
|
-
options.beforeRedirect = options => {
|
65
|
-
// Use this
|
66
|
+
options.beforeRedirect = (options, { headers }) => {
|
67
|
+
// Use this to adjust the request options upon redirecting,
|
68
|
+
// to inspect the latest response headers,
|
66
69
|
// or to cancel the request by throwing an error
|
67
70
|
if (options.hostname === "example.com") {
|
68
71
|
options.auth = "user:password";
|
package/debug.js
ADDED
package/index.js
CHANGED
@@ -2,9 +2,9 @@ var url = require("url");
|
|
2
2
|
var URL = url.URL;
|
3
3
|
var http = require("http");
|
4
4
|
var https = require("https");
|
5
|
-
var assert = require("assert");
|
6
5
|
var Writable = require("stream").Writable;
|
7
|
-
var
|
6
|
+
var assert = require("assert");
|
7
|
+
var debug = require("./debug");
|
8
8
|
|
9
9
|
// Create handlers that pass events from native requests
|
10
10
|
var eventHandlers = Object.create(null);
|
@@ -335,20 +335,26 @@ RedirectableRequest.prototype._processResponse = function (response) {
|
|
335
335
|
}
|
336
336
|
|
337
337
|
// Drop the Host header, as the redirect might lead to a different host
|
338
|
-
|
339
|
-
|
340
|
-
}
|
338
|
+
var previousHostName = removeMatchingHeaders(/^host$/i, this._options.headers) ||
|
339
|
+
url.parse(this._currentUrl).hostname;
|
341
340
|
|
342
341
|
// Create the redirected request
|
343
342
|
var redirectUrl = url.resolve(this._currentUrl, location);
|
344
343
|
debug("redirecting to", redirectUrl);
|
345
344
|
this._isRedirect = true;
|
346
|
-
|
345
|
+
var redirectUrlParts = url.parse(redirectUrl);
|
346
|
+
Object.assign(this._options, redirectUrlParts);
|
347
|
+
|
348
|
+
// Drop the Authorization header if redirecting to another host
|
349
|
+
if (redirectUrlParts.hostname !== previousHostName) {
|
350
|
+
removeMatchingHeaders(/^authorization$/i, this._options.headers);
|
351
|
+
}
|
347
352
|
|
348
353
|
// Evaluate the beforeRedirect callback
|
349
354
|
if (typeof this._options.beforeRedirect === "function") {
|
355
|
+
var responseDetails = { headers: response.headers };
|
350
356
|
try {
|
351
|
-
this._options.beforeRedirect.call(null, this._options);
|
357
|
+
this._options.beforeRedirect.call(null, this._options, responseDetails);
|
352
358
|
}
|
353
359
|
catch (err) {
|
354
360
|
this.emit("error", err);
|
@@ -465,11 +471,14 @@ function urlToOptions(urlObject) {
|
|
465
471
|
}
|
466
472
|
|
467
473
|
function removeMatchingHeaders(regex, headers) {
|
474
|
+
var lastValue;
|
468
475
|
for (var header in headers) {
|
469
476
|
if (regex.test(header)) {
|
477
|
+
lastValue = headers[header];
|
470
478
|
delete headers[header];
|
471
479
|
}
|
472
480
|
}
|
481
|
+
return lastValue;
|
473
482
|
}
|
474
483
|
|
475
484
|
function createErrorType(code, defaultMessage) {
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "follow-redirects",
|
3
|
-
"version": "1.
|
3
|
+
"version": "1.13.0",
|
4
4
|
"description": "HTTP and HTTPS modules that follow redirects.",
|
5
5
|
"license": "MIT",
|
6
6
|
"main": "index.js",
|
@@ -37,9 +37,12 @@
|
|
37
37
|
"Olivier Lalonde <olalonde@gmail.com> (http://www.syskall.com)",
|
38
38
|
"James Talmage <james@talmage.io>"
|
39
39
|
],
|
40
|
-
"
|
41
|
-
|
42
|
-
|
40
|
+
"funding": [
|
41
|
+
{
|
42
|
+
"type": "individual",
|
43
|
+
"url": "https://github.com/sponsors/RubenVerborgh"
|
44
|
+
}
|
45
|
+
],
|
43
46
|
"devDependencies": {
|
44
47
|
"concat-stream": "^2.0.0",
|
45
48
|
"eslint": "^5.16.0",
|