index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. var url = require("url");
  2. var URL = url.URL;
  3. var http = require("http");
  4. var https = require("https");
  5. var Writable = require("stream").Writable;
  6. var assert = require("assert");
  7. var debug = require("./debug");
  8. // Preventive platform detection
  9. // istanbul ignore next
  10. (function detectUnsupportedEnvironment() {
  11. var looksLikeNode = typeof process !== "undefined";
  12. var looksLikeBrowser = typeof window !== "undefined" && typeof document !== "undefined";
  13. var looksLikeV8 = isFunction(Error.captureStackTrace);
  14. if (!looksLikeNode && (looksLikeBrowser || !looksLikeV8)) {
  15. console.warn("The follow-redirects package should be excluded from browser builds.");
  16. }
  17. }());
  18. // Whether to use the native URL object or the legacy url module
  19. var useNativeURL = false;
  20. try {
  21. assert(new URL(""));
  22. }
  23. catch (error) {
  24. useNativeURL = error.code === "ERR_INVALID_URL";
  25. }
  26. // URL fields to preserve in copy operations
  27. var preservedUrlFields = [
  28. "auth",
  29. "host",
  30. "hostname",
  31. "href",
  32. "path",
  33. "pathname",
  34. "port",
  35. "protocol",
  36. "query",
  37. "search",
  38. "hash",
  39. ];
  40. // Create handlers that pass events from native requests
  41. var events = ["abort", "aborted", "connect", "error", "socket", "timeout"];
  42. var eventHandlers = Object.create(null);
  43. events.forEach(function (event) {
  44. eventHandlers[event] = function (arg1, arg2, arg3) {
  45. this._redirectable.emit(event, arg1, arg2, arg3);
  46. };
  47. });
  48. // Error types with codes
  49. var InvalidUrlError = createErrorType(
  50. "ERR_INVALID_URL",
  51. "Invalid URL",
  52. TypeError
  53. );
  54. var RedirectionError = createErrorType(
  55. "ERR_FR_REDIRECTION_FAILURE",
  56. "Redirected request failed"
  57. );
  58. var TooManyRedirectsError = createErrorType(
  59. "ERR_FR_TOO_MANY_REDIRECTS",
  60. "Maximum number of redirects exceeded",
  61. RedirectionError
  62. );
  63. var MaxBodyLengthExceededError = createErrorType(
  64. "ERR_FR_MAX_BODY_LENGTH_EXCEEDED",
  65. "Request body larger than maxBodyLength limit"
  66. );
  67. var WriteAfterEndError = createErrorType(
  68. "ERR_STREAM_WRITE_AFTER_END",
  69. "write after end"
  70. );
  71. // istanbul ignore next
  72. var destroy = Writable.prototype.destroy || noop;
  73. // An HTTP(S) request that can be redirected
  74. function RedirectableRequest(options, responseCallback) {
  75. // Initialize the request
  76. Writable.call(this);
  77. this._sanitizeOptions(options);
  78. this._options = options;
  79. this._ended = false;
  80. this._ending = false;
  81. this._redirectCount = 0;
  82. this._redirects = [];
  83. this._requestBodyLength = 0;
  84. this._requestBodyBuffers = [];
  85. // Attach a callback if passed
  86. if (responseCallback) {
  87. this.on("response", responseCallback);
  88. }
  89. // React to responses of native requests
  90. var self = this;
  91. this._onNativeResponse = function (response) {
  92. try {
  93. self._processResponse(response);
  94. }
  95. catch (cause) {
  96. self.emit("error", cause instanceof RedirectionError ?
  97. cause : new RedirectionError({ cause: cause }));
  98. }
  99. };
  100. // Perform the first request
  101. this._performRequest();
  102. }
  103. RedirectableRequest.prototype = Object.create(Writable.prototype);
  104. RedirectableRequest.prototype.abort = function () {
  105. destroyRequest(this._currentRequest);
  106. this._currentRequest.abort();
  107. this.emit("abort");
  108. };
  109. RedirectableRequest.prototype.destroy = function (error) {
  110. destroyRequest(this._currentRequest, error);
  111. destroy.call(this, error);
  112. return this;
  113. };
  114. // Writes buffered data to the current native request
  115. RedirectableRequest.prototype.write = function (data, encoding, callback) {
  116. // Writing is not allowed if end has been called
  117. if (this._ending) {
  118. throw new WriteAfterEndError();
  119. }
  120. // Validate input and shift parameters if necessary
  121. if (!isString(data) && !isBuffer(data)) {
  122. throw new TypeError("data should be a string, Buffer or Uint8Array");
  123. }
  124. if (isFunction(encoding)) {
  125. callback = encoding;
  126. encoding = null;
  127. }
  128. // Ignore empty buffers, since writing them doesn't invoke the callback
  129. // https://github.com/nodejs/node/issues/22066
  130. if (data.length === 0) {
  131. if (callback) {
  132. callback();
  133. }
  134. return;
  135. }
  136. // Only write when we don't exceed the maximum body length
  137. if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
  138. this._requestBodyLength += data.length;
  139. this._requestBodyBuffers.push({ data: data, encoding: encoding });
  140. this._currentRequest.write(data, encoding, callback);
  141. }
  142. // Error when we exceed the maximum body length
  143. else {
  144. this.emit("error", new MaxBodyLengthExceededError());
  145. this.abort();
  146. }
  147. };
  148. // Ends the current native request
  149. RedirectableRequest.prototype.end = function (data, encoding, callback) {
  150. // Shift parameters if necessary
  151. if (isFunction(data)) {
  152. callback = data;
  153. data = encoding = null;
  154. }
  155. else if (isFunction(encoding)) {
  156. callback = encoding;
  157. encoding = null;
  158. }
  159. // Write data if needed and end
  160. if (!data) {
  161. this._ended = this._ending = true;
  162. this._currentRequest.end(null, null, callback);
  163. }
  164. else {
  165. var self = this;
  166. var currentRequest = this._currentRequest;
  167. this.write(data, encoding, function () {
  168. self._ended = true;
  169. currentRequest.end(null, null, callback);
  170. });
  171. this._ending = true;
  172. }
  173. };
  174. // Sets a header value on the current native request
  175. RedirectableRequest.prototype.setHeader = function (name, value) {
  176. this._options.headers[name] = value;
  177. this._currentRequest.setHeader(name, value);
  178. };
  179. // Clears a header value on the current native request
  180. RedirectableRequest.prototype.removeHeader = function (name) {
  181. delete this._options.headers[name];
  182. this._currentRequest.removeHeader(name);
  183. };
  184. // Global timeout for all underlying requests
  185. RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
  186. var self = this;
  187. // Destroys the socket on timeout
  188. function destroyOnTimeout(socket) {
  189. socket.setTimeout(msecs);
  190. socket.removeListener("timeout", socket.destroy);
  191. socket.addListener("timeout", socket.destroy);
  192. }
  193. // Sets up a timer to trigger a timeout event
  194. function startTimer(socket) {
  195. if (self._timeout) {
  196. clearTimeout(self._timeout);
  197. }
  198. self._timeout = setTimeout(function () {
  199. self.emit("timeout");
  200. clearTimer();
  201. }, msecs);
  202. destroyOnTimeout(socket);
  203. }
  204. // Stops a timeout from triggering
  205. function clearTimer() {
  206. // Clear the timeout
  207. if (self._timeout) {
  208. clearTimeout(self._timeout);
  209. self._timeout = null;
  210. }
  211. // Clean up all attached listeners
  212. self.removeListener("abort", clearTimer);
  213. self.removeListener("error", clearTimer);
  214. self.removeListener("response", clearTimer);
  215. self.removeListener("close", clearTimer);
  216. if (callback) {
  217. self.removeListener("timeout", callback);
  218. }
  219. if (!self.socket) {
  220. self._currentRequest.removeListener("socket", startTimer);
  221. }
  222. }
  223. // Attach callback if passed
  224. if (callback) {
  225. this.on("timeout", callback);
  226. }
  227. // Start the timer if or when the socket is opened
  228. if (this.socket) {
  229. startTimer(this.socket);
  230. }
  231. else {
  232. this._currentRequest.once("socket", startTimer);
  233. }
  234. // Clean up on events
  235. this.on("socket", destroyOnTimeout);
  236. this.on("abort", clearTimer);
  237. this.on("error", clearTimer);
  238. this.on("response", clearTimer);
  239. this.on("close", clearTimer);
  240. return this;
  241. };
  242. // Proxy all other public ClientRequest methods
  243. [
  244. "flushHeaders", "getHeader",
  245. "setNoDelay", "setSocketKeepAlive",
  246. ].forEach(function (method) {
  247. RedirectableRequest.prototype[method] = function (a, b) {
  248. return this._currentRequest[method](a, b);
  249. };
  250. });
  251. // Proxy all public ClientRequest properties
  252. ["aborted", "connection", "socket"].forEach(function (property) {
  253. Object.defineProperty(RedirectableRequest.prototype, property, {
  254. get: function () { return this._currentRequest[property]; },
  255. });
  256. });
  257. RedirectableRequest.prototype._sanitizeOptions = function (options) {
  258. // Ensure headers are always present
  259. if (!options.headers) {
  260. options.headers = {};
  261. }
  262. // Since http.request treats host as an alias of hostname,
  263. // but the url module interprets host as hostname plus port,
  264. // eliminate the host property to avoid confusion.
  265. if (options.host) {
  266. // Use hostname if set, because it has precedence
  267. if (!options.hostname) {
  268. options.hostname = options.host;
  269. }
  270. delete options.host;
  271. }
  272. // Complete the URL object when necessary
  273. if (!options.pathname && options.path) {
  274. var searchPos = options.path.indexOf("?");
  275. if (searchPos < 0) {
  276. options.pathname = options.path;
  277. }
  278. else {
  279. options.pathname = options.path.substring(0, searchPos);
  280. options.search = options.path.substring(searchPos);
  281. }
  282. }
  283. };
  284. // Executes the next native request (initial or redirect)
  285. RedirectableRequest.prototype._performRequest = function () {
  286. // Load the native protocol
  287. var protocol = this._options.protocol;
  288. var nativeProtocol = this._options.nativeProtocols[protocol];
  289. if (!nativeProtocol) {
  290. throw new TypeError("Unsupported protocol " + protocol);
  291. }
  292. // If specified, use the agent corresponding to the protocol
  293. // (HTTP and HTTPS use different types of agents)
  294. if (this._options.agents) {
  295. var scheme = protocol.slice(0, -1);
  296. this._options.agent = this._options.agents[scheme];
  297. }
  298. // Create the native request and set up its event handlers
  299. var request = this._currentRequest =
  300. nativeProtocol.request(this._options, this._onNativeResponse);
  301. request._redirectable = this;
  302. for (var event of events) {
  303. request.on(event, eventHandlers[event]);
  304. }
  305. // RFC7230§5.3.1: When making a request directly to an origin server, […]
  306. // a client MUST send only the absolute path […] as the request-target.
  307. this._currentUrl = /^\//.test(this._options.path) ?
  308. url.format(this._options) :
  309. // When making a request to a proxy, […]
  310. // a client MUST send the target URI in absolute-form […].
  311. this._options.path;
  312. // End a redirected request
  313. // (The first request must be ended explicitly with RedirectableRequest#end)
  314. if (this._isRedirect) {
  315. // Write the request entity and end
  316. var i = 0;
  317. var self = this;
  318. var buffers = this._requestBodyBuffers;
  319. (function writeNext(error) {
  320. // Only write if this request has not been redirected yet
  321. // istanbul ignore else
  322. if (request === self._currentRequest) {
  323. // Report any write errors
  324. // istanbul ignore if
  325. if (error) {
  326. self.emit("error", error);
  327. }
  328. // Write the next buffer if there are still left
  329. else if (i < buffers.length) {
  330. var buffer = buffers[i++];
  331. // istanbul ignore else
  332. if (!request.finished) {
  333. request.write(buffer.data, buffer.encoding, writeNext);
  334. }
  335. }
  336. // End the request if `end` has been called on us
  337. else if (self._ended) {
  338. request.end();
  339. }
  340. }
  341. }());
  342. }
  343. };
  344. // Processes a response from the current native request
  345. RedirectableRequest.prototype._processResponse = function (response) {
  346. // Store the redirected response
  347. var statusCode = response.statusCode;
  348. if (this._options.trackRedirects) {
  349. this._redirects.push({
  350. url: this._currentUrl,
  351. headers: response.headers,
  352. statusCode: statusCode,
  353. });
  354. }
  355. // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  356. // that further action needs to be taken by the user agent in order to
  357. // fulfill the request. If a Location header field is provided,
  358. // the user agent MAY automatically redirect its request to the URI
  359. // referenced by the Location field value,
  360. // even if the specific status code is not understood.
  361. // If the response is not a redirect; return it as-is
  362. var location = response.headers.location;
  363. if (!location || this._options.followRedirects === false ||
  364. statusCode < 300 || statusCode >= 400) {
  365. response.responseUrl = this._currentUrl;
  366. response.redirects = this._redirects;
  367. this.emit("response", response);
  368. // Clean up
  369. this._requestBodyBuffers = [];
  370. return;
  371. }
  372. // The response is a redirect, so abort the current request
  373. destroyRequest(this._currentRequest);
  374. // Discard the remainder of the response to avoid waiting for data
  375. response.destroy();
  376. // RFC7231§6.4: A client SHOULD detect and intervene
  377. // in cyclical redirections (i.e., "infinite" redirection loops).
  378. if (++this._redirectCount > this._options.maxRedirects) {
  379. throw new TooManyRedirectsError();
  380. }
  381. // Store the request headers if applicable
  382. var requestHeaders;
  383. var beforeRedirect = this._options.beforeRedirect;
  384. if (beforeRedirect) {
  385. requestHeaders = Object.assign({
  386. // The Host header was set by nativeProtocol.request
  387. Host: response.req.getHeader("host"),
  388. }, this._options.headers);
  389. }
  390. // RFC7231§6.4: Automatic redirection needs to done with
  391. // care for methods not known to be safe, […]
  392. // RFC7231§6.4.2–3: For historical reasons, a user agent MAY change
  393. // the request method from POST to GET for the subsequent request.
  394. var method = this._options.method;
  395. if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" ||
  396. // RFC7231§6.4.4: The 303 (See Other) status code indicates that
  397. // the server is redirecting the user agent to a different resource […]
  398. // A user agent can perform a retrieval request targeting that URI
  399. // (a GET or HEAD request if using HTTP) […]
  400. (statusCode === 303) && !/^(?:GET|HEAD)$/.test(this._options.method)) {
  401. this._options.method = "GET";
  402. // Drop a possible entity and headers related to it
  403. this._requestBodyBuffers = [];
  404. removeMatchingHeaders(/^content-/i, this._options.headers);
  405. }
  406. // Drop the Host header, as the redirect might lead to a different host
  407. var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
  408. // If the redirect is relative, carry over the host of the last request
  409. var currentUrlParts = parseUrl(this._currentUrl);
  410. var currentHost = currentHostHeader || currentUrlParts.host;
  411. var currentUrl = /^\w+:/.test(location) ? this._currentUrl :
  412. url.format(Object.assign(currentUrlParts, { host: currentHost }));
  413. // Create the redirected request
  414. var redirectUrl = resolveUrl(location, currentUrl);
  415. debug("redirecting to", redirectUrl.href);
  416. this._isRedirect = true;
  417. spreadUrlObject(redirectUrl, this._options);
  418. // Drop confidential headers when redirecting to a less secure protocol
  419. // or to a different domain that is not a superdomain
  420. if (redirectUrl.protocol !== currentUrlParts.protocol &&
  421. redirectUrl.protocol !== "https:" ||
  422. redirectUrl.host !== currentHost &&
  423. !isSubdomain(redirectUrl.host, currentHost)) {
  424. removeMatchingHeaders(/^(?:(?:proxy-)?authorization|cookie)$/i, this._options.headers);
  425. }
  426. // Evaluate the beforeRedirect callback
  427. if (isFunction(beforeRedirect)) {
  428. var responseDetails = {
  429. headers: response.headers,
  430. statusCode: statusCode,
  431. };
  432. var requestDetails = {
  433. url: currentUrl,
  434. method: method,
  435. headers: requestHeaders,
  436. };
  437. beforeRedirect(this._options, responseDetails, requestDetails);
  438. this._sanitizeOptions(this._options);
  439. }
  440. // Perform the redirected request
  441. this._performRequest();
  442. };
  443. // Wraps the key/value object of protocols with redirect functionality
  444. function wrap(protocols) {
  445. // Default settings
  446. var exports = {
  447. maxRedirects: 21,
  448. maxBodyLength: 10 * 1024 * 1024,
  449. };
  450. // Wrap each protocol
  451. var nativeProtocols = {};
  452. Object.keys(protocols).forEach(function (scheme) {
  453. var protocol = scheme + ":";
  454. var nativeProtocol = nativeProtocols[protocol] = protocols[scheme];
  455. var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
  456. // Executes a request, following redirects
  457. function request(input, options, callback) {
  458. // Parse parameters, ensuring that input is an object
  459. if (isURL(input)) {
  460. input = spreadUrlObject(input);
  461. }
  462. else if (isString(input)) {
  463. input = spreadUrlObject(parseUrl(input));
  464. }
  465. else {
  466. callback = options;
  467. options = validateUrl(input);
  468. input = { protocol: protocol };
  469. }
  470. if (isFunction(options)) {
  471. callback = options;
  472. options = null;
  473. }
  474. // Set defaults
  475. options = Object.assign({
  476. maxRedirects: exports.maxRedirects,
  477. maxBodyLength: exports.maxBodyLength,
  478. }, input, options);
  479. options.nativeProtocols = nativeProtocols;
  480. if (!isString(options.host) && !isString(options.hostname)) {
  481. options.hostname = "::1";
  482. }
  483. assert.equal(options.protocol, protocol, "protocol mismatch");
  484. debug("options", options);
  485. return new RedirectableRequest(options, callback);
  486. }
  487. // Executes a GET request, following redirects
  488. function get(input, options, callback) {
  489. var wrappedRequest = wrappedProtocol.request(input, options, callback);
  490. wrappedRequest.end();
  491. return wrappedRequest;
  492. }
  493. // Expose the properties on the wrapped protocol
  494. Object.defineProperties(wrappedProtocol, {
  495. request: { value: request, configurable: true, enumerable: true, writable: true },
  496. get: { value: get, configurable: true, enumerable: true, writable: true },
  497. });
  498. });
  499. return exports;
  500. }
  501. function noop() { /* empty */ }
  502. function parseUrl(input) {
  503. var parsed;
  504. // istanbul ignore else
  505. if (useNativeURL) {
  506. parsed = new URL(input);
  507. }
  508. else {
  509. // Ensure the URL is valid and absolute
  510. parsed = validateUrl(url.parse(input));
  511. if (!isString(parsed.protocol)) {
  512. throw new InvalidUrlError({ input });
  513. }
  514. }
  515. return parsed;
  516. }
  517. function resolveUrl(relative, base) {
  518. // istanbul ignore next
  519. return useNativeURL ? new URL(relative, base) : parseUrl(url.resolve(base, relative));
  520. }
  521. function validateUrl(input) {
  522. if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) {
  523. throw new InvalidUrlError({ input: input.href || input });
  524. }
  525. if (/^\[/.test(input.host) && !/^\[[:0-9a-f]+\](:\d+)?$/i.test(input.host)) {
  526. throw new InvalidUrlError({ input: input.href || input });
  527. }
  528. return input;
  529. }
  530. function spreadUrlObject(urlObject, target) {
  531. var spread = target || {};
  532. for (var key of preservedUrlFields) {
  533. spread[key] = urlObject[key];
  534. }
  535. // Fix IPv6 hostname
  536. if (spread.hostname.startsWith("[")) {
  537. spread.hostname = spread.hostname.slice(1, -1);
  538. }
  539. // Ensure port is a number
  540. if (spread.port !== "") {
  541. spread.port = Number(spread.port);
  542. }
  543. // Concatenate path
  544. spread.path = spread.search ? spread.pathname + spread.search : spread.pathname;
  545. return spread;
  546. }
  547. function removeMatchingHeaders(regex, headers) {
  548. var lastValue;
  549. for (var header in headers) {
  550. if (regex.test(header)) {
  551. lastValue = headers[header];
  552. delete headers[header];
  553. }
  554. }
  555. return (lastValue === null || typeof lastValue === "undefined") ?
  556. undefined : String(lastValue).trim();
  557. }
  558. function createErrorType(code, message, baseClass) {
  559. // Create constructor
  560. function CustomError(properties) {
  561. // istanbul ignore else
  562. if (isFunction(Error.captureStackTrace)) {
  563. Error.captureStackTrace(this, this.constructor);
  564. }
  565. Object.assign(this, properties || {});
  566. this.code = code;
  567. this.message = this.cause ? message + ": " + this.cause.message : message;
  568. }
  569. // Attach constructor and set default properties
  570. CustomError.prototype = new (baseClass || Error)();
  571. Object.defineProperties(CustomError.prototype, {
  572. constructor: {
  573. value: CustomError,
  574. enumerable: false,
  575. },
  576. name: {
  577. value: "Error [" + code + "]",
  578. enumerable: false,
  579. },
  580. });
  581. return CustomError;
  582. }
  583. function destroyRequest(request, error) {
  584. for (var event of events) {
  585. request.removeListener(event, eventHandlers[event]);
  586. }
  587. request.on("error", noop);
  588. request.destroy(error);
  589. }
  590. function isSubdomain(subdomain, domain) {
  591. assert(isString(subdomain) && isString(domain));
  592. var dot = subdomain.length - domain.length - 1;
  593. return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
  594. }
  595. function isString(value) {
  596. return typeof value === "string" || value instanceof String;
  597. }
  598. function isFunction(value) {
  599. return typeof value === "function";
  600. }
  601. function isBuffer(value) {
  602. return typeof value === "object" && ("length" in value);
  603. }
  604. function isURL(value) {
  605. return URL && value instanceof URL;
  606. }
  607. // Exports
  608. module.exports = wrap({ http: http, https: https });
  609. module.exports.wrap = wrap;