index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. var url = require("url");
  2. var http = require("http");
  3. var https = require("https");
  4. var assert = require("assert");
  5. var Writable = require("stream").Writable;
  6. var debug = require("debug")("follow-redirects");
  7. // RFC7231§4.2.1: Of the request methods defined by this specification,
  8. // the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.
  9. var SAFE_METHODS = { GET: true, HEAD: true, OPTIONS: true, TRACE: true };
  10. // Create handlers that pass events from native requests
  11. var eventHandlers = Object.create(null);
  12. ["abort", "aborted", "error", "socket", "timeout"].forEach(function (event) {
  13. eventHandlers[event] = function (arg) {
  14. this._redirectable.emit(event, arg);
  15. };
  16. });
  17. // An HTTP(S) request that can be redirected
  18. function RedirectableRequest(options, responseCallback) {
  19. // Initialize the request
  20. Writable.call(this);
  21. options.headers = options.headers || {};
  22. this._options = options;
  23. this._redirectCount = 0;
  24. this._redirects = [];
  25. this._requestBodyLength = 0;
  26. this._requestBodyBuffers = [];
  27. // Since http.request treats host as an alias of hostname,
  28. // but the url module interprets host as hostname plus port,
  29. // eliminate the host property to avoid confusion.
  30. if (options.host) {
  31. // Use hostname if set, because it has precedence
  32. if (!options.hostname) {
  33. options.hostname = options.host;
  34. }
  35. delete options.host;
  36. }
  37. // Attach a callback if passed
  38. if (responseCallback) {
  39. this.on("response", responseCallback);
  40. }
  41. // React to responses of native requests
  42. var self = this;
  43. this._onNativeResponse = function (response) {
  44. self._processResponse(response);
  45. };
  46. // Complete the URL object when necessary
  47. if (!options.pathname && options.path) {
  48. var searchPos = options.path.indexOf("?");
  49. if (searchPos < 0) {
  50. options.pathname = options.path;
  51. }
  52. else {
  53. options.pathname = options.path.substring(0, searchPos);
  54. options.search = options.path.substring(searchPos);
  55. }
  56. }
  57. // Perform the first request
  58. this._performRequest();
  59. }
  60. RedirectableRequest.prototype = Object.create(Writable.prototype);
  61. // Writes buffered data to the current native request
  62. RedirectableRequest.prototype.write = function (data, encoding, callback) {
  63. // Validate input and shift parameters if necessary
  64. if (!(typeof data === "string" || typeof data === "object" && ("length" in data))) {
  65. throw new Error("data should be a string, Buffer or Uint8Array");
  66. }
  67. if (typeof encoding === "function") {
  68. callback = encoding;
  69. encoding = null;
  70. }
  71. // Ignore empty buffers, since writing them doesn't invoke the callback
  72. // https://github.com/nodejs/node/issues/22066
  73. if (data.length === 0) {
  74. if (callback) {
  75. callback();
  76. }
  77. return;
  78. }
  79. // Only write when we don't exceed the maximum body length
  80. if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
  81. this._requestBodyLength += data.length;
  82. this._requestBodyBuffers.push({ data: data, encoding: encoding });
  83. this._currentRequest.write(data, encoding, callback);
  84. }
  85. // Error when we exceed the maximum body length
  86. else {
  87. this.emit("error", new Error("Request body larger than maxBodyLength limit"));
  88. this.abort();
  89. }
  90. };
  91. // Ends the current native request
  92. RedirectableRequest.prototype.end = function (data, encoding, callback) {
  93. // Shift parameters if necessary
  94. if (typeof data === "function") {
  95. callback = data;
  96. data = encoding = null;
  97. }
  98. else if (typeof encoding === "function") {
  99. callback = encoding;
  100. encoding = null;
  101. }
  102. // Write data and end
  103. var currentRequest = this._currentRequest;
  104. this.write(data || "", encoding, function () {
  105. currentRequest.end(null, null, callback);
  106. });
  107. };
  108. // Sets a header value on the current native request
  109. RedirectableRequest.prototype.setHeader = function (name, value) {
  110. this._options.headers[name] = value;
  111. this._currentRequest.setHeader(name, value);
  112. };
  113. // Clears a header value on the current native request
  114. RedirectableRequest.prototype.removeHeader = function (name) {
  115. delete this._options.headers[name];
  116. this._currentRequest.removeHeader(name);
  117. };
  118. // Proxy all other public ClientRequest methods
  119. [
  120. "abort", "flushHeaders", "getHeader",
  121. "setNoDelay", "setSocketKeepAlive", "setTimeout",
  122. ].forEach(function (method) {
  123. RedirectableRequest.prototype[method] = function (a, b) {
  124. return this._currentRequest[method](a, b);
  125. };
  126. });
  127. // Proxy all public ClientRequest properties
  128. ["aborted", "connection", "socket"].forEach(function (property) {
  129. Object.defineProperty(RedirectableRequest.prototype, property, {
  130. get: function () { return this._currentRequest[property]; },
  131. });
  132. });
  133. // Executes the next native request (initial or redirect)
  134. RedirectableRequest.prototype._performRequest = function () {
  135. // Load the native protocol
  136. var protocol = this._options.protocol;
  137. var nativeProtocol = this._options.nativeProtocols[protocol];
  138. if (!nativeProtocol) {
  139. this.emit("error", new Error("Unsupported protocol " + protocol));
  140. return;
  141. }
  142. // If specified, use the agent corresponding to the protocol
  143. // (HTTP and HTTPS use different types of agents)
  144. if (this._options.agents) {
  145. var scheme = protocol.substr(0, protocol.length - 1);
  146. this._options.agent = this._options.agents[scheme];
  147. }
  148. // Create the native request
  149. var request = this._currentRequest =
  150. nativeProtocol.request(this._options, this._onNativeResponse);
  151. this._currentUrl = url.format(this._options);
  152. // Set up event handlers
  153. request._redirectable = this;
  154. for (var event in eventHandlers) {
  155. /* istanbul ignore else */
  156. if (event) {
  157. request.on(event, eventHandlers[event]);
  158. }
  159. }
  160. // End a redirected request
  161. // (The first request must be ended explicitly with RedirectableRequest#end)
  162. if (this._isRedirect) {
  163. // Write the request entity and end.
  164. var i = 0;
  165. var buffers = this._requestBodyBuffers;
  166. (function writeNext() {
  167. if (i < buffers.length) {
  168. var buffer = buffers[i++];
  169. request.write(buffer.data, buffer.encoding, writeNext);
  170. }
  171. else {
  172. request.end();
  173. }
  174. }());
  175. }
  176. };
  177. // Processes a response from the current native request
  178. RedirectableRequest.prototype._processResponse = function (response) {
  179. // Store the redirected response
  180. if (this._options.trackRedirects) {
  181. this._redirects.push({
  182. url: this._currentUrl,
  183. headers: response.headers,
  184. statusCode: response.statusCode,
  185. });
  186. }
  187. // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  188. // that further action needs to be taken by the user agent in order to
  189. // fulfill the request. If a Location header field is provided,
  190. // the user agent MAY automatically redirect its request to the URI
  191. // referenced by the Location field value,
  192. // even if the specific status code is not understood.
  193. var location = response.headers.location;
  194. if (location && this._options.followRedirects !== false &&
  195. response.statusCode >= 300 && response.statusCode < 400) {
  196. // RFC7231§6.4: A client SHOULD detect and intervene
  197. // in cyclical redirections (i.e., "infinite" redirection loops).
  198. if (++this._redirectCount > this._options.maxRedirects) {
  199. this.emit("error", new Error("Max redirects exceeded."));
  200. return;
  201. }
  202. // RFC7231§6.4: Automatic redirection needs to done with
  203. // care for methods not known to be safe […],
  204. // since the user might not wish to redirect an unsafe request.
  205. // RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates
  206. // that the target resource resides temporarily under a different URI
  207. // and the user agent MUST NOT change the request method
  208. // if it performs an automatic redirection to that URI.
  209. var header;
  210. var headers = this._options.headers;
  211. if (response.statusCode !== 307 && !(this._options.method in SAFE_METHODS)) {
  212. this._options.method = "GET";
  213. // Drop a possible entity and headers related to it
  214. this._requestBodyBuffers = [];
  215. for (header in headers) {
  216. if (/^content-/i.test(header)) {
  217. delete headers[header];
  218. }
  219. }
  220. }
  221. // Drop the Host header, as the redirect might lead to a different host
  222. if (!this._isRedirect) {
  223. for (header in headers) {
  224. if (/^host$/i.test(header)) {
  225. delete headers[header];
  226. }
  227. }
  228. }
  229. // Perform the redirected request
  230. var redirectUrl = url.resolve(this._currentUrl, location);
  231. debug("redirecting to", redirectUrl);
  232. Object.assign(this._options, url.parse(redirectUrl));
  233. this._isRedirect = true;
  234. this._performRequest();
  235. // Discard the remainder of the response to avoid waiting for data
  236. response.destroy();
  237. }
  238. else {
  239. // The response is not a redirect; return it as-is
  240. response.responseUrl = this._currentUrl;
  241. response.redirects = this._redirects;
  242. this.emit("response", response);
  243. // Clean up
  244. this._requestBodyBuffers = [];
  245. }
  246. };
  247. // Wraps the key/value object of protocols with redirect functionality
  248. function wrap(protocols) {
  249. // Default settings
  250. var exports = {
  251. maxRedirects: 21,
  252. maxBodyLength: 10 * 1024 * 1024,
  253. };
  254. // Wrap each protocol
  255. var nativeProtocols = {};
  256. Object.keys(protocols).forEach(function (scheme) {
  257. var protocol = scheme + ":";
  258. var nativeProtocol = nativeProtocols[protocol] = protocols[scheme];
  259. var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
  260. // Executes a request, following redirects
  261. wrappedProtocol.request = function (options, callback) {
  262. if (typeof options === "string") {
  263. options = url.parse(options);
  264. options.maxRedirects = exports.maxRedirects;
  265. }
  266. else {
  267. options = Object.assign({
  268. protocol: protocol,
  269. maxRedirects: exports.maxRedirects,
  270. maxBodyLength: exports.maxBodyLength,
  271. }, options);
  272. }
  273. options.nativeProtocols = nativeProtocols;
  274. assert.equal(options.protocol, protocol, "protocol mismatch");
  275. debug("options", options);
  276. return new RedirectableRequest(options, callback);
  277. };
  278. // Executes a GET request, following redirects
  279. wrappedProtocol.get = function (options, callback) {
  280. var request = wrappedProtocol.request(options, callback);
  281. request.end();
  282. return request;
  283. };
  284. });
  285. return exports;
  286. }
  287. // Exports
  288. module.exports = wrap({ http: http, https: https });
  289. module.exports.wrap = wrap;