http.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. 'use strict';
  2. var utils = require('./../utils');
  3. var settle = require('./../core/settle');
  4. var buildFullPath = require('../core/buildFullPath');
  5. var buildURL = require('./../helpers/buildURL');
  6. var http = require('http');
  7. var https = require('https');
  8. var httpFollow = require('follow-redirects').http;
  9. var httpsFollow = require('follow-redirects').https;
  10. var url = require('url');
  11. var zlib = require('zlib');
  12. var VERSION = require('./../env/data').version;
  13. var transitionalDefaults = require('../defaults/transitional');
  14. var AxiosError = require('../core/AxiosError');
  15. var CanceledError = require('../cancel/CanceledError');
  16. var isHttps = /https:?/;
  17. var supportedProtocols = [ 'http:', 'https:', 'file:' ];
  18. /**
  19. *
  20. * @param {http.ClientRequestArgs} options
  21. * @param {AxiosProxyConfig} proxy
  22. * @param {string} location
  23. */
  24. function setProxy(options, proxy, location) {
  25. options.hostname = proxy.host;
  26. options.host = proxy.host;
  27. options.port = proxy.port;
  28. options.path = location;
  29. // Basic proxy authorization
  30. if (proxy.auth) {
  31. var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
  32. options.headers['Proxy-Authorization'] = 'Basic ' + base64;
  33. }
  34. // If a proxy is used, any redirects must also pass through the proxy
  35. options.beforeRedirect = function beforeRedirect(redirection) {
  36. redirection.headers.host = redirection.host;
  37. setProxy(redirection, proxy, redirection.href);
  38. };
  39. }
  40. /*eslint consistent-return:0*/
  41. module.exports = function httpAdapter(config) {
  42. return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
  43. var onCanceled;
  44. function done() {
  45. if (config.cancelToken) {
  46. config.cancelToken.unsubscribe(onCanceled);
  47. }
  48. if (config.signal) {
  49. config.signal.removeEventListener('abort', onCanceled);
  50. }
  51. }
  52. var resolve = function resolve(value) {
  53. done();
  54. resolvePromise(value);
  55. };
  56. var rejected = false;
  57. var reject = function reject(value) {
  58. done();
  59. rejected = true;
  60. rejectPromise(value);
  61. };
  62. var data = config.data;
  63. var headers = config.headers;
  64. var headerNames = {};
  65. Object.keys(headers).forEach(function storeLowerName(name) {
  66. headerNames[name.toLowerCase()] = name;
  67. });
  68. // Set User-Agent (required by some servers)
  69. // See https://github.com/axios/axios/issues/69
  70. if ('user-agent' in headerNames) {
  71. // User-Agent is specified; handle case where no UA header is desired
  72. if (!headers[headerNames['user-agent']]) {
  73. delete headers[headerNames['user-agent']];
  74. }
  75. // Otherwise, use specified value
  76. } else {
  77. // Only set header if it hasn't been set in config
  78. headers['User-Agent'] = 'axios/' + VERSION;
  79. }
  80. // support for https://www.npmjs.com/package/form-data api
  81. if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
  82. Object.assign(headers, data.getHeaders());
  83. } else if (data && !utils.isStream(data)) {
  84. if (Buffer.isBuffer(data)) {
  85. // Nothing to do...
  86. } else if (utils.isArrayBuffer(data)) {
  87. data = Buffer.from(new Uint8Array(data));
  88. } else if (utils.isString(data)) {
  89. data = Buffer.from(data, 'utf-8');
  90. } else {
  91. return reject(new AxiosError(
  92. 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
  93. AxiosError.ERR_BAD_REQUEST,
  94. config
  95. ));
  96. }
  97. if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
  98. return reject(new AxiosError(
  99. 'Request body larger than maxBodyLength limit',
  100. AxiosError.ERR_BAD_REQUEST,
  101. config
  102. ));
  103. }
  104. // Add Content-Length header if data exists
  105. if (!headerNames['content-length']) {
  106. headers['Content-Length'] = data.length;
  107. }
  108. }
  109. // HTTP basic authentication
  110. var auth = undefined;
  111. if (config.auth) {
  112. var username = config.auth.username || '';
  113. var password = config.auth.password || '';
  114. auth = username + ':' + password;
  115. }
  116. // Parse url
  117. var fullPath = buildFullPath(config.baseURL, config.url);
  118. var parsed = url.parse(fullPath);
  119. var protocol = parsed.protocol || supportedProtocols[0];
  120. if (supportedProtocols.indexOf(protocol) === -1) {
  121. return reject(new AxiosError(
  122. 'Unsupported protocol ' + protocol,
  123. AxiosError.ERR_BAD_REQUEST,
  124. config
  125. ));
  126. }
  127. if (!auth && parsed.auth) {
  128. var urlAuth = parsed.auth.split(':');
  129. var urlUsername = urlAuth[0] || '';
  130. var urlPassword = urlAuth[1] || '';
  131. auth = urlUsername + ':' + urlPassword;
  132. }
  133. if (auth && headerNames.authorization) {
  134. delete headers[headerNames.authorization];
  135. }
  136. var isHttpsRequest = isHttps.test(protocol);
  137. var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
  138. try {
  139. buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, '');
  140. } catch (err) {
  141. var customErr = new Error(err.message);
  142. customErr.config = config;
  143. customErr.url = config.url;
  144. customErr.exists = true;
  145. reject(customErr);
  146. }
  147. var options = {
  148. path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
  149. method: config.method.toUpperCase(),
  150. headers: headers,
  151. agent: agent,
  152. agents: { http: config.httpAgent, https: config.httpsAgent },
  153. auth: auth
  154. };
  155. if (config.socketPath) {
  156. options.socketPath = config.socketPath;
  157. } else {
  158. options.hostname = parsed.hostname;
  159. options.port = parsed.port;
  160. }
  161. var proxy = config.proxy;
  162. if (!proxy && proxy !== false) {
  163. var proxyEnv = protocol.slice(0, -1) + '_proxy';
  164. var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
  165. if (proxyUrl) {
  166. var parsedProxyUrl = url.parse(proxyUrl);
  167. var noProxyEnv = process.env.no_proxy || process.env.NO_PROXY;
  168. var shouldProxy = true;
  169. if (noProxyEnv) {
  170. var noProxy = noProxyEnv.split(',').map(function trim(s) {
  171. return s.trim();
  172. });
  173. shouldProxy = !noProxy.some(function proxyMatch(proxyElement) {
  174. if (!proxyElement) {
  175. return false;
  176. }
  177. if (proxyElement === '*') {
  178. return true;
  179. }
  180. if (proxyElement[0] === '.' &&
  181. parsed.hostname.substr(parsed.hostname.length - proxyElement.length) === proxyElement) {
  182. return true;
  183. }
  184. return parsed.hostname === proxyElement;
  185. });
  186. }
  187. if (shouldProxy) {
  188. proxy = {
  189. host: parsedProxyUrl.hostname,
  190. port: parsedProxyUrl.port,
  191. protocol: parsedProxyUrl.protocol
  192. };
  193. if (parsedProxyUrl.auth) {
  194. var proxyUrlAuth = parsedProxyUrl.auth.split(':');
  195. proxy.auth = {
  196. username: proxyUrlAuth[0],
  197. password: proxyUrlAuth[1]
  198. };
  199. }
  200. }
  201. }
  202. }
  203. if (proxy) {
  204. options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
  205. setProxy(options, proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path);
  206. }
  207. var transport;
  208. var isHttpsProxy = isHttpsRequest && (proxy ? isHttps.test(proxy.protocol) : true);
  209. if (config.transport) {
  210. transport = config.transport;
  211. } else if (config.maxRedirects === 0) {
  212. transport = isHttpsProxy ? https : http;
  213. } else {
  214. if (config.maxRedirects) {
  215. options.maxRedirects = config.maxRedirects;
  216. }
  217. if (config.beforeRedirect) {
  218. options.beforeRedirect = config.beforeRedirect;
  219. }
  220. transport = isHttpsProxy ? httpsFollow : httpFollow;
  221. }
  222. if (config.maxBodyLength > -1) {
  223. options.maxBodyLength = config.maxBodyLength;
  224. }
  225. if (config.insecureHTTPParser) {
  226. options.insecureHTTPParser = config.insecureHTTPParser;
  227. }
  228. // Create the request
  229. var req = transport.request(options, function handleResponse(res) {
  230. if (req.aborted) return;
  231. // uncompress the response body transparently if required
  232. var stream = res;
  233. // return the last request in case of redirects
  234. var lastRequest = res.req || req;
  235. // if no content, is HEAD request or decompress disabled we should not decompress
  236. if (res.statusCode !== 204 && lastRequest.method !== 'HEAD' && config.decompress !== false) {
  237. switch (res.headers['content-encoding']) {
  238. /*eslint default-case:0*/
  239. case 'gzip':
  240. case 'compress':
  241. case 'deflate':
  242. // add the unzipper to the body stream processing pipeline
  243. stream = stream.pipe(zlib.createUnzip());
  244. // remove the content-encoding in order to not confuse downstream operations
  245. delete res.headers['content-encoding'];
  246. break;
  247. }
  248. }
  249. var response = {
  250. status: res.statusCode,
  251. statusText: res.statusMessage,
  252. headers: res.headers,
  253. config: config,
  254. request: lastRequest
  255. };
  256. if (config.responseType === 'stream') {
  257. response.data = stream;
  258. settle(resolve, reject, response);
  259. } else {
  260. var responseBuffer = [];
  261. var totalResponseBytes = 0;
  262. stream.on('data', function handleStreamData(chunk) {
  263. responseBuffer.push(chunk);
  264. totalResponseBytes += chunk.length;
  265. // make sure the content length is not over the maxContentLength if specified
  266. if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
  267. // stream.destoy() emit aborted event before calling reject() on Node.js v16
  268. rejected = true;
  269. stream.destroy();
  270. reject(new AxiosError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
  271. AxiosError.ERR_BAD_RESPONSE, config, lastRequest));
  272. }
  273. });
  274. stream.on('aborted', function handlerStreamAborted() {
  275. if (rejected) {
  276. return;
  277. }
  278. stream.destroy();
  279. reject(new AxiosError(
  280. 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
  281. AxiosError.ERR_BAD_RESPONSE,
  282. config,
  283. lastRequest
  284. ));
  285. });
  286. stream.on('error', function handleStreamError(err) {
  287. if (req.aborted) return;
  288. reject(AxiosError.from(err, null, config, lastRequest));
  289. });
  290. stream.on('end', function handleStreamEnd() {
  291. try {
  292. var responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
  293. if (config.responseType !== 'arraybuffer') {
  294. responseData = responseData.toString(config.responseEncoding);
  295. if (!config.responseEncoding || config.responseEncoding === 'utf8') {
  296. responseData = utils.stripBOM(responseData);
  297. }
  298. }
  299. response.data = responseData;
  300. } catch (err) {
  301. reject(AxiosError.from(err, null, config, response.request, response));
  302. }
  303. settle(resolve, reject, response);
  304. });
  305. }
  306. });
  307. // Handle errors
  308. req.on('error', function handleRequestError(err) {
  309. // @todo remove
  310. // if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;
  311. reject(AxiosError.from(err, null, config, req));
  312. });
  313. // set tcp keep alive to prevent drop connection by peer
  314. req.on('socket', function handleRequestSocket(socket) {
  315. // default interval of sending ack packet is 1 minute
  316. socket.setKeepAlive(true, 1000 * 60);
  317. });
  318. // Handle request timeout
  319. if (config.timeout) {
  320. // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
  321. var timeout = parseInt(config.timeout, 10);
  322. if (isNaN(timeout)) {
  323. reject(new AxiosError(
  324. 'error trying to parse `config.timeout` to int',
  325. AxiosError.ERR_BAD_OPTION_VALUE,
  326. config,
  327. req
  328. ));
  329. return;
  330. }
  331. // Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
  332. // And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
  333. // At this time, if we have a large number of request, nodejs will hang up some socket on background. and the number will up and up.
  334. // And then these socket which be hang up will devoring CPU little by little.
  335. // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
  336. req.setTimeout(timeout, function handleRequestTimeout() {
  337. req.abort();
  338. var transitional = config.transitional || transitionalDefaults;
  339. reject(new AxiosError(
  340. 'timeout of ' + timeout + 'ms exceeded',
  341. transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
  342. config,
  343. req
  344. ));
  345. });
  346. }
  347. if (config.cancelToken || config.signal) {
  348. // Handle cancellation
  349. // eslint-disable-next-line func-names
  350. onCanceled = function(cancel) {
  351. if (req.aborted) return;
  352. req.abort();
  353. reject(!cancel || (cancel && cancel.type) ? new CanceledError() : cancel);
  354. };
  355. config.cancelToken && config.cancelToken.subscribe(onCanceled);
  356. if (config.signal) {
  357. config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
  358. }
  359. }
  360. // Send the request
  361. if (utils.isStream(data)) {
  362. data.on('error', function handleStreamError(err) {
  363. reject(AxiosError.from(err, config, null, req));
  364. }).pipe(req);
  365. } else {
  366. req.end(data);
  367. }
  368. });
  369. };