follow-redirects 1.2.0 → 1.2.4

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.

Files changed (3) hide show
  1. package/LICENSE +19 -0
  2. package/index.js +41 -14
  3. package/package.json +10 -7
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright 2017 Olivier Lalonde <olalonde@gmail.com>, James Talmage <james@talmage.io>, Ruben Verborgh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
package/index.js CHANGED
@@ -42,6 +42,17 @@ function RedirectableRequest(options, responseCallback) {
42
42
  self._processResponse(response);
43
43
  };
44
44
 
45
+ // Complete the URL object when necessary
46
+ if (!options.pathname && options.path) {
47
+ var searchPos = options.path.indexOf('?');
48
+ if (searchPos < 0) {
49
+ options.pathname = options.path;
50
+ } else {
51
+ options.pathname = options.path.substring(0, searchPos);
52
+ options.search = options.path.substring(searchPos);
53
+ }
54
+ }
55
+
45
56
  // Perform the first request
46
57
  this._performRequest();
47
58
  }
@@ -57,7 +68,7 @@ RedirectableRequest.prototype._performRequest = function () {
57
68
  }
58
69
 
59
70
  // Create the native request
60
- var nativeProtocol = nativeProtocols[this._options.protocol];
71
+ var nativeProtocol = nativeProtocols[protocol];
61
72
  var request = this._currentRequest =
62
73
  nativeProtocol.request(this._options, this._onNativeResponse);
63
74
  this._currentUrl = url.format(this._options);
@@ -65,6 +76,7 @@ RedirectableRequest.prototype._performRequest = function () {
65
76
  // Set up event handlers
66
77
  request._redirectable = this;
67
78
  for (var event in eventHandlers) {
79
+ /* istanbul ignore else */
68
80
  if (event) {
69
81
  request.on(event, eventHandlers[event]);
70
82
  }
@@ -72,13 +84,12 @@ RedirectableRequest.prototype._performRequest = function () {
72
84
 
73
85
  // End a redirected request
74
86
  // (The first request must be ended explicitly with RedirectableRequest#end)
75
- if (this._currentResponse) {
76
- // If no body was written to the original request, or the method was changed to GET,
77
- // end the redirected request (without writing a body).
87
+ if (this._isRedirect) {
88
+ // If the request doesn't have en entity, end directly.
78
89
  var bufferedWrites = this._bufferedWrites;
79
- if (bufferedWrites.length === 0 || this._options.method === 'GET') {
90
+ if (bufferedWrites.length === 0) {
80
91
  request.end();
81
- // The body of the original request must be added to the redirected request.
92
+ // Otherwise, write the request entity and end afterwards.
82
93
  } else {
83
94
  var i = 0;
84
95
  (function writeNext() {
@@ -110,16 +121,32 @@ RedirectableRequest.prototype._processResponse = function (response) {
110
121
  return this.emit('error', new Error('Max redirects exceeded.'));
111
122
  }
112
123
 
124
+ // RFC7231§6.4: Automatic redirection needs to done with
125
+ // care for methods not known to be safe […],
126
+ // since the user might not wish to redirect an unsafe request.
113
127
  // RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates
114
128
  // that the target resource resides temporarily under a different URI
115
129
  // and the user agent MUST NOT change the request method
116
130
  // if it performs an automatic redirection to that URI.
117
- if (response.statusCode !== 307) {
118
- // RFC7231§6.4: Automatic redirection needs to done with
119
- // care for methods not known to be safe […],
120
- // since the user might not wish to redirect an unsafe request.
121
- if (!(this._options.method in safeMethods)) {
122
- this._options.method = 'GET';
131
+ var header;
132
+ var headers = this._options.headers;
133
+ if (response.statusCode !== 307 && !(this._options.method in safeMethods)) {
134
+ this._options.method = 'GET';
135
+ // Drop a possible entity and headers related to it
136
+ this._bufferedWrites = [];
137
+ for (header in headers) {
138
+ if (/^content-/i.test(header)) {
139
+ delete headers[header];
140
+ }
141
+ }
142
+ }
143
+
144
+ // Drop the Host header, as the redirect might lead to a different host
145
+ if (!this._isRedirect) {
146
+ for (header in headers) {
147
+ if (/^host$/i.test(header)) {
148
+ delete headers[header];
149
+ }
123
150
  }
124
151
  }
125
152
 
@@ -127,7 +154,7 @@ RedirectableRequest.prototype._processResponse = function (response) {
127
154
  var redirectUrl = url.resolve(this._currentUrl, location);
128
155
  debug('redirecting to', redirectUrl);
129
156
  Object.assign(this._options, url.parse(redirectUrl));
130
- this._currentResponse = response;
157
+ this._isRedirect = true;
131
158
  this._performRequest();
132
159
  } else {
133
160
  // The response is not a redirect; return it as-is
@@ -166,7 +193,7 @@ RedirectableRequest.prototype.setTimeout = function (timeout, callback) {
166
193
  };
167
194
 
168
195
  // Writes buffered data to the current native request
169
- RedirectableRequest.prototype._write = function (data, encoding, callback) {
196
+ RedirectableRequest.prototype.write = function (data, encoding, callback) {
170
197
  this._currentRequest.write(data, encoding, callback);
171
198
  this._bufferedWrites.push({data: data, encoding: encoding});
172
199
  };
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "follow-redirects",
3
- "version": "1.2.0",
3
+ "version": "1.2.4",
4
4
  "description": "HTTP and HTTPS modules that follow redirects.",
5
5
  "main": "index.js",
6
+ "engines": {
7
+ "node": ">=4.0"
8
+ },
6
9
  "scripts": {
7
10
  "test": "xo && BLUEBIRD_DEBUG=1 nyc mocha"
8
11
  },
@@ -39,16 +42,16 @@
39
42
  "https.js"
40
43
  ],
41
44
  "dependencies": {
42
- "debug": "^2.2.0"
45
+ "debug": "^2.4.5"
43
46
  },
44
47
  "devDependencies": {
45
48
  "bluebird": "^3.4.0",
46
- "concat-stream": "^1.5.0",
47
- "coveralls": "^2.11.2",
49
+ "concat-stream": "^1.5.2",
50
+ "coveralls": "^2.11.15",
48
51
  "express": "^4.13.0",
49
- "mocha": "^3.1.2",
50
- "nyc": "^8.3.1",
51
- "xo": "^0.17.0"
52
+ "mocha": "^3.2.0",
53
+ "nyc": "^10.0.0",
54
+ "xo": "^0.17.1"
52
55
  },
53
56
  "license": "MIT",
54
57
  "nyc": {