index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. var url = require('url');
  3. var assert = require('assert');
  4. var http = require('http');
  5. var https = require('https');
  6. var Writable = require('stream').Writable;
  7. var debug = require('debug')('follow-redirects');
  8. var nativeProtocols = {'http:': http, 'https:': https};
  9. var schemes = {};
  10. var exports = module.exports = {
  11. maxRedirects: 21
  12. };
  13. // RFC7231§4.2.1: Of the request methods defined by this specification,
  14. // the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.
  15. var safeMethods = {GET: true, HEAD: true, OPTIONS: true, TRACE: true};
  16. // Create handlers that pass events from native requests
  17. var eventHandlers = Object.create(null);
  18. ['abort', 'aborted', 'error', 'socket'].forEach(function (event) {
  19. eventHandlers[event] = function (arg) {
  20. this._redirectable.emit(event, arg);
  21. };
  22. });
  23. // An HTTP(S) request that can be redirected
  24. function RedirectableRequest(options, responseCallback) {
  25. // Initialize the request
  26. Writable.call(this);
  27. this._options = options;
  28. this._redirectCount = 0;
  29. this._bufferedWrites = [];
  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. // Complete the URL object when necessary
  40. if (!options.pathname && options.path) {
  41. var searchPos = options.path.indexOf('?');
  42. if (searchPos < 0) {
  43. options.pathname = options.path;
  44. } else {
  45. options.pathname = options.path.substring(0, searchPos);
  46. options.search = options.path.substring(searchPos);
  47. }
  48. }
  49. // Perform the first request
  50. this._performRequest();
  51. }
  52. RedirectableRequest.prototype = Object.create(Writable.prototype);
  53. // Executes the next native request (initial or redirect)
  54. RedirectableRequest.prototype._performRequest = function () {
  55. // If specified, use the agent corresponding to the protocol
  56. // (HTTP and HTTPS use different types of agents)
  57. var protocol = this._options.protocol;
  58. if (this._options.agents) {
  59. this._options.agent = this._options.agents[schemes[protocol]];
  60. }
  61. // Create the native request
  62. var nativeProtocol = nativeProtocols[protocol];
  63. var request = this._currentRequest =
  64. nativeProtocol.request(this._options, this._onNativeResponse);
  65. this._currentUrl = url.format(this._options);
  66. // Set up event handlers
  67. request._redirectable = this;
  68. for (var event in eventHandlers) {
  69. /* istanbul ignore else */
  70. if (event) {
  71. request.on(event, eventHandlers[event]);
  72. }
  73. }
  74. // End a redirected request
  75. // (The first request must be ended explicitly with RedirectableRequest#end)
  76. if (this._isRedirect) {
  77. // If the request doesn't have en entity, end directly.
  78. var bufferedWrites = this._bufferedWrites;
  79. if (bufferedWrites.length === 0) {
  80. request.end();
  81. // Otherwise, write the request entity and end afterwards.
  82. } else {
  83. var i = 0;
  84. (function writeNext() {
  85. if (i < bufferedWrites.length) {
  86. var bufferedWrite = bufferedWrites[i++];
  87. request.write(bufferedWrite.data, bufferedWrite.encoding, writeNext);
  88. } else {
  89. request.end();
  90. }
  91. })();
  92. }
  93. }
  94. };
  95. // Processes a response from the current native request
  96. RedirectableRequest.prototype._processResponse = function (response) {
  97. // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  98. // that further action needs to be taken by the user agent in order to
  99. // fulfill the request. If a Location header field is provided,
  100. // the user agent MAY automatically redirect its request to the URI
  101. // referenced by the Location field value,
  102. // even if the specific status code is not understood.
  103. var location = response.headers.location;
  104. if (location && this._options.followRedirects !== false &&
  105. response.statusCode >= 300 && response.statusCode < 400) {
  106. // RFC7231§6.4: A client SHOULD detect and intervene
  107. // in cyclical redirections (i.e., "infinite" redirection loops).
  108. if (++this._redirectCount > this._options.maxRedirects) {
  109. return this.emit('error', new Error('Max redirects exceeded.'));
  110. }
  111. // RFC7231§6.4: Automatic redirection needs to done with
  112. // care for methods not known to be safe […],
  113. // since the user might not wish to redirect an unsafe request.
  114. // RFC7231§6.4.7: The 307 (Temporary Redirect) status code indicates
  115. // that the target resource resides temporarily under a different URI
  116. // and the user agent MUST NOT change the request method
  117. // if it performs an automatic redirection to that URI.
  118. var header;
  119. var headers = this._options.headers;
  120. if (response.statusCode !== 307 && !(this._options.method in safeMethods)) {
  121. this._options.method = 'GET';
  122. // Drop a possible entity and headers related to it
  123. this._bufferedWrites = [];
  124. for (header in headers) {
  125. if (/^content-/i.test(header)) {
  126. delete headers[header];
  127. }
  128. }
  129. }
  130. // Drop the Host header, as the redirect might lead to a different host
  131. if (!this._isRedirect) {
  132. for (header in headers) {
  133. if (/^host$/i.test(header)) {
  134. delete headers[header];
  135. }
  136. }
  137. }
  138. // Perform the redirected request
  139. var redirectUrl = url.resolve(this._currentUrl, location);
  140. debug('redirecting to', redirectUrl);
  141. Object.assign(this._options, url.parse(redirectUrl));
  142. this._isRedirect = true;
  143. this._performRequest();
  144. } else {
  145. // The response is not a redirect; return it as-is
  146. response.responseUrl = this._currentUrl;
  147. this.emit('response', response);
  148. // Clean up
  149. delete this._options;
  150. delete this._bufferedWrites;
  151. }
  152. };
  153. // Aborts the current native request
  154. RedirectableRequest.prototype.abort = function () {
  155. this._currentRequest.abort();
  156. };
  157. // Flushes the headers of the current native request
  158. RedirectableRequest.prototype.flushHeaders = function () {
  159. this._currentRequest.flushHeaders();
  160. };
  161. // Sets the noDelay option of the current native request
  162. RedirectableRequest.prototype.setNoDelay = function (noDelay) {
  163. this._currentRequest.setNoDelay(noDelay);
  164. };
  165. // Sets the socketKeepAlive option of the current native request
  166. RedirectableRequest.prototype.setSocketKeepAlive = function (enable, initialDelay) {
  167. this._currentRequest.setSocketKeepAlive(enable, initialDelay);
  168. };
  169. // Sets the timeout option of the current native request
  170. RedirectableRequest.prototype.setTimeout = function (timeout, callback) {
  171. this._currentRequest.setTimeout(timeout, callback);
  172. };
  173. // Writes buffered data to the current native request
  174. RedirectableRequest.prototype.write = function (data, encoding, callback) {
  175. this._currentRequest.write(data, encoding, callback);
  176. this._bufferedWrites.push({data: data, encoding: encoding});
  177. };
  178. // Ends the current native request
  179. RedirectableRequest.prototype.end = function (data, encoding, callback) {
  180. this._currentRequest.end(data, encoding, callback);
  181. if (data) {
  182. this._bufferedWrites.push({data: data, encoding: encoding});
  183. }
  184. };
  185. // Export a redirecting wrapper for each native protocol
  186. Object.keys(nativeProtocols).forEach(function (protocol) {
  187. var scheme = schemes[protocol] = protocol.substr(0, protocol.length - 1);
  188. var nativeProtocol = nativeProtocols[protocol];
  189. var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
  190. // Executes an HTTP request, following redirects
  191. wrappedProtocol.request = function (options, callback) {
  192. if (typeof options === 'string') {
  193. options = url.parse(options);
  194. options.maxRedirects = exports.maxRedirects;
  195. } else {
  196. options = Object.assign({
  197. maxRedirects: exports.maxRedirects,
  198. protocol: protocol
  199. }, options);
  200. }
  201. assert.equal(options.protocol, protocol, 'protocol mismatch');
  202. debug('options', options);
  203. return new RedirectableRequest(options, callback);
  204. };
  205. // Executes a GET request, following redirects
  206. wrappedProtocol.get = function (options, callback) {
  207. var request = wrappedProtocol.request(options, callback);
  208. request.end();
  209. return request;
  210. };
  211. });