You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
14 KiB

4 years ago
  1. var url = require("url");
  2. var URL = url.URL;
  3. var http = require("http");
  4. var https = require("https");
  5. var assert = require("assert");
  6. var Writable = require("stream").Writable;
  7. var debug = require("debug")("follow-redirects");
  8. // RFC7231§4.2.1: Of the request methods defined by this specification,
  9. // the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.
  10. var SAFE_METHODS = { GET: true, HEAD: true, OPTIONS: true, TRACE: true };
  11. // Create handlers that pass events from native requests
  12. var eventHandlers = Object.create(null);
  13. ["abort", "aborted", "connect", "error", "socket", "timeout"].forEach(function (event) {
  14. eventHandlers[event] = function (arg1, arg2, arg3) {
  15. this._redirectable.emit(event, arg1, arg2, arg3);
  16. };
  17. });
  18. // An HTTP(S) request that can be redirected
  19. function RedirectableRequest(options, responseCallback) {
  20. // Initialize the request
  21. Writable.call(this);
  22. this._sanitizeOptions(options);
  23. this._options = options;
  24. this._ended = false;
  25. this._ending = false;
  26. this._redirectCount = 0;
  27. this._redirects = [];
  28. this._requestBodyLength = 0;
  29. this._requestBodyBuffers = [];
  30. // Attach a callback if passed
  31. if (responseCallback) {
  32. this.on("response", responseCallback);
  33. }
  34. // React to responses of native requests
  35. var self = this;
  36. this._onNativeResponse = function (response) {
  37. self._processResponse(response);
  38. };
  39. // Perform the first request
  40. this._performRequest();
  41. }
  42. RedirectableRequest.prototype = Object.create(Writable.prototype);
  43. // Writes buffered data to the current native request
  44. RedirectableRequest.prototype.write = function (data, encoding, callback) {
  45. // Writing is not allowed if end has been called
  46. if (this._ending) {
  47. throw new Error("write after end");
  48. }
  49. // Validate input and shift parameters if necessary
  50. if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
  51. throw new Error("data should be a string, Buffer or Uint8Array");
  52. }
  53. if (typeof encoding === "function") {
  54. callback = encoding;
  55. encoding = null;
  56. }
  57. // Ignore empty buffers, since writing them doesn't invoke the callback
  58. // https://github.com/nodejs/node/issues/22066
  59. if (data.length === 0) {
  60. if (callback) {
  61. callback();
  62. }
  63. return;
  64. }
  65. // Only write when we don't exceed the maximum body length
  66. if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
  67. this._requestBodyLength += data.length;
  68. this._requestBodyBuffers.push({ data: data, encoding: encoding });
  69. this._currentRequest.write(data, encoding, callback);
  70. }
  71. // Error when we exceed the maximum body length
  72. else {
  73. this.emit("error", new Error("Request body larger than maxBodyLength limit"));
  74. this.abort();
  75. }
  76. };
  77. // Ends the current native request
  78. RedirectableRequest.prototype.end = function (data, encoding, callback) {
  79. // Shift parameters if necessary
  80. if (typeof data === "function") {
  81. callback = data;
  82. data = encoding = null;
  83. }
  84. else if (typeof encoding === "function") {
  85. callback = encoding;
  86. encoding = null;
  87. }
  88. // Write data if needed and end
  89. if (!data) {
  90. this._ended = this._ending = true;
  91. this._currentRequest.end(null, null, callback);
  92. }
  93. else {
  94. var self = this;
  95. var currentRequest = this._currentRequest;
  96. this.write(data, encoding, function () {
  97. self._ended = true;
  98. currentRequest.end(null, null, callback);
  99. });
  100. this._ending = true;
  101. }
  102. };
  103. // Sets a header value on the current native request
  104. RedirectableRequest.prototype.setHeader = function (name, value) {
  105. this._options.headers[name] = value;
  106. this._currentRequest.setHeader(name, value);
  107. };
  108. // Clears a header value on the current native request
  109. RedirectableRequest.prototype.removeHeader = function (name) {
  110. delete this._options.headers[name];
  111. this._currentRequest.removeHeader(name);
  112. };
  113. // Global timeout for all underlying requests
  114. RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
  115. if (callback) {
  116. this.once("timeout", callback);
  117. }
  118. if (this.socket) {
  119. startTimer(this, msecs);
  120. }
  121. else {
  122. var self = this;
  123. this._currentRequest.once("socket", function () {
  124. startTimer(self, msecs);
  125. });
  126. }
  127. this.once("response", clearTimer);
  128. this.once("error", clearTimer);
  129. return this;
  130. };
  131. function startTimer(request, msecs) {
  132. clearTimeout(request._timeout);
  133. request._timeout = setTimeout(function () {
  134. request.emit("timeout");
  135. }, msecs);
  136. }
  137. function clearTimer() {
  138. clearTimeout(this._timeout);
  139. }
  140. // Proxy all other public ClientRequest methods
  141. [
  142. "abort", "flushHeaders", "getHeader",
  143. "setNoDelay", "setSocketKeepAlive",
  144. ].forEach(function (method) {
  145. RedirectableRequest.prototype[method] = function (a, b) {
  146. return this._currentRequest[method](a, b);
  147. };
  148. });
  149. // Proxy all public ClientRequest properties
  150. ["aborted", "connection", "socket"].forEach(function (property) {
  151. Object.defineProperty(RedirectableRequest.prototype, property, {
  152. get: function () { return this._currentRequest[property]; },
  153. });
  154. });
  155. RedirectableRequest.prototype._sanitizeOptions = function (options) {
  156. // Ensure headers are always present
  157. if (!options.headers) {
  158. options.headers = {};
  159. }
  160. // Since http.request treats host as an alias of hostname,
  161. // but the url module interprets host as hostname plus port,
  162. // eliminate the host property to avoid confusion.
  163. if (options.host) {
  164. // Use hostname if set, because it has precedence
  165. if (!options.hostname) {
  166. options.hostname = options.host;
  167. }
  168. delete options.host;
  169. }
  170. // Complete the URL object when necessary
  171. if (!options.pathname && options.path) {
  172. var searchPos = options.path.indexOf("?");
  173. if (searchPos < 0) {
  174. options.pathname = options.path;
  175. }
  176. else {
  177. options.pathname = options.path.substring(0, searchPos);
  178. options.search = options.path.substring(searchPos);
  179. }
  180. }
  181. };
  182. // Executes the next native request (initial or redirect)
  183. RedirectableRequest.prototype._performRequest = function () {
  184. // Load the native protocol
  185. var protocol = this._options.protocol;
  186. var nativeProtocol = this._options.nativeProtocols[protocol];
  187. if (!nativeProtocol) {
  188. this.emit("error", new Error("Unsupported protocol " + protocol));
  189. return;
  190. }
  191. // If specified, use the agent corresponding to the protocol
  192. // (HTTP and HTTPS use different types of agents)
  193. if (this._options.agents) {
  194. var scheme = protocol.substr(0, protocol.length - 1);
  195. this._options.agent = this._options.agents[scheme];
  196. }
  197. // Create the native request
  198. var request = this._currentRequest =
  199. nativeProtocol.request(this._options, this._onNativeResponse);
  200. this._currentUrl = url.format(this._options);
  201. // Set up event handlers
  202. request._redirectable = this;
  203. for (var event in eventHandlers) {
  204. /* istanbul ignore else */
  205. if (event) {
  206. request.on(event, eventHandlers[event]);
  207. }
  208. }
  209. // End a redirected request
  210. // (The first request must be ended explicitly with RedirectableRequest#end)
  211. if (this._isRedirect) {
  212. // Write the request entity and end.
  213. var i = 0;
  214. var self = this;
  215. var buffers = this._requestBodyBuffers;
  216. (function writeNext(error) {
  217. // Only write if this request has not been redirected yet
  218. /* istanbul ignore else */
  219. if (request === self._currentRequest) {
  220. // Report any write errors
  221. /* istanbul ignore if */
  222. if (error) {
  223. self.emit("error", error);
  224. }
  225. // Write the next buffer if there are still left
  226. else if (i < buffers.length) {
  227. var buffer = buffers[i++];
  228. /* istanbul ignore else */
  229. if (!request.finished) {
  230. request.write(buffer.data, buffer.encoding, writeNext);
  231. }
  232. }
  233. // End the request if `end` has been called on us
  234. else if (self._ended) {
  235. request.end();
  236. }
  237. }
  238. }());
  239. }
  240. };
  241. // Processes a response from the current native request
  242. RedirectableRequest.prototype._processResponse = function (response) {
  243. // Store the redirected response
  244. var statusCode = response.statusCode;
  245. if (this._options.trackRedirects) {
  246. this._redirects.push({
  247. url: this._currentUrl,
  248. headers: response.headers,
  249. statusCode: statusCode,
  250. });
  251. }
  252. // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  253. // that further action needs to be taken by the user agent in order to
  254. // fulfill the request. If a Location header field is provided,
  255. // the user agent MAY automatically redirect its request to the URI
  256. // referenced by the Location field value,
  257. // even if the specific status code is not understood.
  258. var location = response.headers.location;
  259. if (location && this._options.followRedirects !== false &&
  260. statusCode >= 300 && statusCode < 400) {
  261. // Abort the current request
  262. this._currentRequest.removeAllListeners();
  263. this._currentRequest.on("error", noop);
  264. this._currentRequest.abort();
  265. // Discard the remainder of the response to avoid waiting for data
  266. response.destroy();
  267. // RFC7231§6.4: A client SHOULD detect and intervene
  268. // in cyclical redirections (i.e., "infinite" redirection loops).
  269. if (++this._redirectCount > this._options.maxRedirects) {
  270. this.emit("error", new Error("Max redirects exceeded."));
  271. return;
  272. }
  273. // RFC7231§6.4: Automatic redirection needs to done with
  274. // care for methods not known to be safe […],
  275. // since the user might not wish to redirect an unsafe request.
  276. // RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates
  277. // that the target resource resides temporarily under a different URI
  278. // and the user agent MUST NOT change the request method
  279. // if it performs an automatic redirection to that URI.
  280. var header;
  281. var headers = this._options.headers;
  282. if (statusCode !== 307 && !(this._options.method in SAFE_METHODS)) {
  283. this._options.method = "GET";
  284. // Drop a possible entity and headers related to it
  285. this._requestBodyBuffers = [];
  286. for (header in headers) {
  287. if (/^content-/i.test(header)) {
  288. delete headers[header];
  289. }
  290. }
  291. }
  292. // Drop the Host header, as the redirect might lead to a different host
  293. if (!this._isRedirect) {
  294. for (header in headers) {
  295. if (/^host$/i.test(header)) {
  296. delete headers[header];
  297. }
  298. }
  299. }
  300. // Perform the redirected request
  301. var redirectUrl = url.resolve(this._currentUrl, location);
  302. debug("redirecting to", redirectUrl);
  303. Object.assign(this._options, url.parse(redirectUrl));
  304. if (typeof this._options.beforeRedirect === "function") {
  305. try {
  306. this._options.beforeRedirect.call(null, this._options);
  307. }
  308. catch (err) {
  309. this.emit("error", err);
  310. return;
  311. }
  312. this._sanitizeOptions(this._options);
  313. }
  314. this._isRedirect = true;
  315. this._performRequest();
  316. }
  317. else {
  318. // The response is not a redirect; return it as-is
  319. response.responseUrl = this._currentUrl;
  320. response.redirects = this._redirects;
  321. this.emit("response", response);
  322. // Clean up
  323. this._requestBodyBuffers = [];
  324. }
  325. };
  326. // Wraps the key/value object of protocols with redirect functionality
  327. function wrap(protocols) {
  328. // Default settings
  329. var exports = {
  330. maxRedirects: 21,
  331. maxBodyLength: 10 * 1024 * 1024,
  332. };
  333. // Wrap each protocol
  334. var nativeProtocols = {};
  335. Object.keys(protocols).forEach(function (scheme) {
  336. var protocol = scheme + ":";
  337. var nativeProtocol = nativeProtocols[protocol] = protocols[scheme];
  338. var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
  339. // Executes a request, following redirects
  340. wrappedProtocol.request = function (input, options, callback) {
  341. // Parse parameters
  342. if (typeof input === "string") {
  343. var urlStr = input;
  344. try {
  345. input = urlToOptions(new URL(urlStr));
  346. }
  347. catch (err) {
  348. /* istanbul ignore next */
  349. input = url.parse(urlStr);
  350. }
  351. }
  352. else if (URL && (input instanceof URL)) {
  353. input = urlToOptions(input);
  354. }
  355. else {
  356. callback = options;
  357. options = input;
  358. input = { protocol: protocol };
  359. }
  360. if (typeof options === "function") {
  361. callback = options;
  362. options = null;
  363. }
  364. // Set defaults
  365. options = Object.assign({
  366. maxRedirects: exports.maxRedirects,
  367. maxBodyLength: exports.maxBodyLength,
  368. }, input, options);
  369. options.nativeProtocols = nativeProtocols;
  370. assert.equal(options.protocol, protocol, "protocol mismatch");
  371. debug("options", options);
  372. return new RedirectableRequest(options, callback);
  373. };
  374. // Executes a GET request, following redirects
  375. wrappedProtocol.get = function (input, options, callback) {
  376. var request = wrappedProtocol.request(input, options, callback);
  377. request.end();
  378. return request;
  379. };
  380. });
  381. return exports;
  382. }
  383. /* istanbul ignore next */
  384. function noop() { /* empty */ }
  385. // from https://github.com/nodejs/node/blob/master/lib/internal/url.js
  386. function urlToOptions(urlObject) {
  387. var options = {
  388. protocol: urlObject.protocol,
  389. hostname: urlObject.hostname.startsWith("[") ?
  390. /* istanbul ignore next */
  391. urlObject.hostname.slice(1, -1) :
  392. urlObject.hostname,
  393. hash: urlObject.hash,
  394. search: urlObject.search,
  395. pathname: urlObject.pathname,
  396. path: urlObject.pathname + urlObject.search,
  397. href: urlObject.href,
  398. };
  399. if (urlObject.port !== "") {
  400. options.port = Number(urlObject.port);
  401. }
  402. return options;
  403. }
  404. // Exports
  405. module.exports = wrap({ http: http, https: https });
  406. module.exports.wrap = wrap;