http.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. 'use strict';
  2. import utils from './../utils.js';
  3. import settle from './../core/settle.js';
  4. import buildFullPath from '../core/buildFullPath.js';
  5. import buildURL from './../helpers/buildURL.js';
  6. import proxyFromEnv from 'proxy-from-env';
  7. import http from 'http';
  8. import https from 'https';
  9. import util from 'util';
  10. import followRedirects from 'follow-redirects';
  11. import zlib from 'zlib';
  12. import {VERSION} from '../env/data.js';
  13. import transitionalDefaults from '../defaults/transitional.js';
  14. import AxiosError from '../core/AxiosError.js';
  15. import CanceledError from '../cancel/CanceledError.js';
  16. import platform from '../platform/index.js';
  17. import fromDataURI from '../helpers/fromDataURI.js';
  18. import stream from 'stream';
  19. import AxiosHeaders from '../core/AxiosHeaders.js';
  20. import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
  21. import {EventEmitter} from 'events';
  22. import formDataToStream from "../helpers/formDataToStream.js";
  23. import readBlob from "../helpers/readBlob.js";
  24. import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
  25. import callbackify from "../helpers/callbackify.js";
  26. import {progressEventReducer, progressEventDecorator, asyncDecorator} from "../helpers/progressEventReducer.js";
  27. import estimateDataURLDecodedBytes from '../helpers/estimateDataURLDecodedBytes.js';
  28. const zlibOptions = {
  29. flush: zlib.constants.Z_SYNC_FLUSH,
  30. finishFlush: zlib.constants.Z_SYNC_FLUSH
  31. };
  32. const brotliOptions = {
  33. flush: zlib.constants.BROTLI_OPERATION_FLUSH,
  34. finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
  35. }
  36. const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
  37. const {http: httpFollow, https: httpsFollow} = followRedirects;
  38. const isHttps = /https:?/;
  39. const supportedProtocols = platform.protocols.map(protocol => {
  40. return protocol + ':';
  41. });
  42. const flushOnFinish = (stream, [throttled, flush]) => {
  43. stream
  44. .on('end', flush)
  45. .on('error', flush);
  46. return throttled;
  47. }
  48. /**
  49. * If the proxy or config beforeRedirects functions are defined, call them with the options
  50. * object.
  51. *
  52. * @param {Object<string, any>} options - The options object that was passed to the request.
  53. *
  54. * @returns {Object<string, any>}
  55. */
  56. function dispatchBeforeRedirect(options, responseDetails) {
  57. if (options.beforeRedirects.proxy) {
  58. options.beforeRedirects.proxy(options);
  59. }
  60. if (options.beforeRedirects.config) {
  61. options.beforeRedirects.config(options, responseDetails);
  62. }
  63. }
  64. /**
  65. * If the proxy or config afterRedirects functions are defined, call them with the options
  66. *
  67. * @param {http.ClientRequestArgs} options
  68. * @param {AxiosProxyConfig} configProxy configuration from Axios options object
  69. * @param {string} location
  70. *
  71. * @returns {http.ClientRequestArgs}
  72. */
  73. function setProxy(options, configProxy, location) {
  74. let proxy = configProxy;
  75. if (!proxy && proxy !== false) {
  76. const proxyUrl = proxyFromEnv.getProxyForUrl(location);
  77. if (proxyUrl) {
  78. proxy = new URL(proxyUrl);
  79. }
  80. }
  81. if (proxy) {
  82. // Basic proxy authorization
  83. if (proxy.username) {
  84. proxy.auth = (proxy.username || '') + ':' + (proxy.password || '');
  85. }
  86. if (proxy.auth) {
  87. // Support proxy auth object form
  88. if (proxy.auth.username || proxy.auth.password) {
  89. proxy.auth = (proxy.auth.username || '') + ':' + (proxy.auth.password || '');
  90. }
  91. const base64 = Buffer
  92. .from(proxy.auth, 'utf8')
  93. .toString('base64');
  94. options.headers['Proxy-Authorization'] = 'Basic ' + base64;
  95. }
  96. options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
  97. const proxyHost = proxy.hostname || proxy.host;
  98. options.hostname = proxyHost;
  99. // Replace 'host' since options is not a URL object
  100. options.host = proxyHost;
  101. options.port = proxy.port;
  102. options.path = location;
  103. if (proxy.protocol) {
  104. options.protocol = proxy.protocol.includes(':') ? proxy.protocol : `${proxy.protocol}:`;
  105. }
  106. }
  107. options.beforeRedirects.proxy = function beforeRedirect(redirectOptions) {
  108. // Configure proxy for redirected request, passing the original config proxy to apply
  109. // the exact same logic as if the redirected request was performed by axios directly.
  110. setProxy(redirectOptions, configProxy, redirectOptions.href);
  111. };
  112. }
  113. const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process';
  114. // temporary hotfix
  115. const wrapAsync = (asyncExecutor) => {
  116. return new Promise((resolve, reject) => {
  117. let onDone;
  118. let isDone;
  119. const done = (value, isRejected) => {
  120. if (isDone) return;
  121. isDone = true;
  122. onDone && onDone(value, isRejected);
  123. }
  124. const _resolve = (value) => {
  125. done(value);
  126. resolve(value);
  127. };
  128. const _reject = (reason) => {
  129. done(reason, true);
  130. reject(reason);
  131. }
  132. asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);
  133. })
  134. };
  135. const resolveFamily = ({address, family}) => {
  136. if (!utils.isString(address)) {
  137. throw TypeError('address must be a string');
  138. }
  139. return ({
  140. address,
  141. family: family || (address.indexOf('.') < 0 ? 6 : 4)
  142. });
  143. }
  144. const buildAddressEntry = (address, family) => resolveFamily(utils.isObject(address) ? address : {address, family});
  145. /*eslint consistent-return:0*/
  146. export default isHttpAdapterSupported && function httpAdapter(config) {
  147. return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
  148. let {data, lookup, family} = config;
  149. const {responseType, responseEncoding} = config;
  150. const method = config.method.toUpperCase();
  151. let isDone;
  152. let rejected = false;
  153. let req;
  154. if (lookup) {
  155. const _lookup = callbackify(lookup, (value) => utils.isArray(value) ? value : [value]);
  156. // hotfix to support opt.all option which is required for node 20.x
  157. lookup = (hostname, opt, cb) => {
  158. _lookup(hostname, opt, (err, arg0, arg1) => {
  159. if (err) {
  160. return cb(err);
  161. }
  162. const addresses = utils.isArray(arg0) ? arg0.map(addr => buildAddressEntry(addr)) : [buildAddressEntry(arg0, arg1)];
  163. opt.all ? cb(err, addresses) : cb(err, addresses[0].address, addresses[0].family);
  164. });
  165. }
  166. }
  167. // temporary internal emitter until the AxiosRequest class will be implemented
  168. const emitter = new EventEmitter();
  169. const onFinished = () => {
  170. if (config.cancelToken) {
  171. config.cancelToken.unsubscribe(abort);
  172. }
  173. if (config.signal) {
  174. config.signal.removeEventListener('abort', abort);
  175. }
  176. emitter.removeAllListeners();
  177. }
  178. onDone((value, isRejected) => {
  179. isDone = true;
  180. if (isRejected) {
  181. rejected = true;
  182. onFinished();
  183. }
  184. });
  185. function abort(reason) {
  186. emitter.emit('abort', !reason || reason.type ? new CanceledError(null, config, req) : reason);
  187. }
  188. emitter.once('abort', reject);
  189. if (config.cancelToken || config.signal) {
  190. config.cancelToken && config.cancelToken.subscribe(abort);
  191. if (config.signal) {
  192. config.signal.aborted ? abort() : config.signal.addEventListener('abort', abort);
  193. }
  194. }
  195. // Parse url
  196. const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
  197. const parsed = new URL(fullPath, platform.hasBrowserEnv ? platform.origin : undefined);
  198. const protocol = parsed.protocol || supportedProtocols[0];
  199. if (protocol === 'data:') {
  200. // Apply the same semantics as HTTP: only enforce if a finite, non-negative cap is set.
  201. if (config.maxContentLength > -1) {
  202. // Use the exact string passed to fromDataURI (config.url); fall back to fullPath if needed.
  203. const dataUrl = String(config.url || fullPath || '');
  204. const estimated = estimateDataURLDecodedBytes(dataUrl);
  205. if (estimated > config.maxContentLength) {
  206. return reject(new AxiosError(
  207. 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
  208. AxiosError.ERR_BAD_RESPONSE,
  209. config
  210. ));
  211. }
  212. }
  213. let convertedData;
  214. if (method !== 'GET') {
  215. return settle(resolve, reject, {
  216. status: 405,
  217. statusText: 'method not allowed',
  218. headers: {},
  219. config
  220. });
  221. }
  222. try {
  223. convertedData = fromDataURI(config.url, responseType === 'blob', {
  224. Blob: config.env && config.env.Blob
  225. });
  226. } catch (err) {
  227. throw AxiosError.from(err, AxiosError.ERR_BAD_REQUEST, config);
  228. }
  229. if (responseType === 'text') {
  230. convertedData = convertedData.toString(responseEncoding);
  231. if (!responseEncoding || responseEncoding === 'utf8') {
  232. convertedData = utils.stripBOM(convertedData);
  233. }
  234. } else if (responseType === 'stream') {
  235. convertedData = stream.Readable.from(convertedData);
  236. }
  237. return settle(resolve, reject, {
  238. data: convertedData,
  239. status: 200,
  240. statusText: 'OK',
  241. headers: new AxiosHeaders(),
  242. config
  243. });
  244. }
  245. if (supportedProtocols.indexOf(protocol) === -1) {
  246. return reject(new AxiosError(
  247. 'Unsupported protocol ' + protocol,
  248. AxiosError.ERR_BAD_REQUEST,
  249. config
  250. ));
  251. }
  252. const headers = AxiosHeaders.from(config.headers).normalize();
  253. // Set User-Agent (required by some servers)
  254. // See https://github.com/axios/axios/issues/69
  255. // User-Agent is specified; handle case where no UA header is desired
  256. // Only set header if it hasn't been set in config
  257. headers.set('User-Agent', 'axios/' + VERSION, false);
  258. const {onUploadProgress, onDownloadProgress} = config;
  259. const maxRate = config.maxRate;
  260. let maxUploadRate = undefined;
  261. let maxDownloadRate = undefined;
  262. // support for spec compliant FormData objects
  263. if (utils.isSpecCompliantForm(data)) {
  264. const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
  265. data = formDataToStream(data, (formHeaders) => {
  266. headers.set(formHeaders);
  267. }, {
  268. tag: `axios-${VERSION}-boundary`,
  269. boundary: userBoundary && userBoundary[1] || undefined
  270. });
  271. // support for https://www.npmjs.com/package/form-data api
  272. } else if (utils.isFormData(data) && utils.isFunction(data.getHeaders)) {
  273. headers.set(data.getHeaders());
  274. if (!headers.hasContentLength()) {
  275. try {
  276. const knownLength = await util.promisify(data.getLength).call(data);
  277. Number.isFinite(knownLength) && knownLength >= 0 && headers.setContentLength(knownLength);
  278. /*eslint no-empty:0*/
  279. } catch (e) {
  280. }
  281. }
  282. } else if (utils.isBlob(data) || utils.isFile(data)) {
  283. data.size && headers.setContentType(data.type || 'application/octet-stream');
  284. headers.setContentLength(data.size || 0);
  285. data = stream.Readable.from(readBlob(data));
  286. } else if (data && !utils.isStream(data)) {
  287. if (Buffer.isBuffer(data)) {
  288. // Nothing to do...
  289. } else if (utils.isArrayBuffer(data)) {
  290. data = Buffer.from(new Uint8Array(data));
  291. } else if (utils.isString(data)) {
  292. data = Buffer.from(data, 'utf-8');
  293. } else {
  294. return reject(new AxiosError(
  295. 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
  296. AxiosError.ERR_BAD_REQUEST,
  297. config
  298. ));
  299. }
  300. // Add Content-Length header if data exists
  301. headers.setContentLength(data.length, false);
  302. if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
  303. return reject(new AxiosError(
  304. 'Request body larger than maxBodyLength limit',
  305. AxiosError.ERR_BAD_REQUEST,
  306. config
  307. ));
  308. }
  309. }
  310. const contentLength = utils.toFiniteNumber(headers.getContentLength());
  311. if (utils.isArray(maxRate)) {
  312. maxUploadRate = maxRate[0];
  313. maxDownloadRate = maxRate[1];
  314. } else {
  315. maxUploadRate = maxDownloadRate = maxRate;
  316. }
  317. if (data && (onUploadProgress || maxUploadRate)) {
  318. if (!utils.isStream(data)) {
  319. data = stream.Readable.from(data, {objectMode: false});
  320. }
  321. data = stream.pipeline([data, new AxiosTransformStream({
  322. maxRate: utils.toFiniteNumber(maxUploadRate)
  323. })], utils.noop);
  324. onUploadProgress && data.on('progress', flushOnFinish(
  325. data,
  326. progressEventDecorator(
  327. contentLength,
  328. progressEventReducer(asyncDecorator(onUploadProgress), false, 3)
  329. )
  330. ));
  331. }
  332. // HTTP basic authentication
  333. let auth = undefined;
  334. if (config.auth) {
  335. const username = config.auth.username || '';
  336. const password = config.auth.password || '';
  337. auth = username + ':' + password;
  338. }
  339. if (!auth && parsed.username) {
  340. const urlUsername = parsed.username;
  341. const urlPassword = parsed.password;
  342. auth = urlUsername + ':' + urlPassword;
  343. }
  344. auth && headers.delete('authorization');
  345. let path;
  346. try {
  347. path = buildURL(
  348. parsed.pathname + parsed.search,
  349. config.params,
  350. config.paramsSerializer
  351. ).replace(/^\?/, '');
  352. } catch (err) {
  353. const customErr = new Error(err.message);
  354. customErr.config = config;
  355. customErr.url = config.url;
  356. customErr.exists = true;
  357. return reject(customErr);
  358. }
  359. headers.set(
  360. 'Accept-Encoding',
  361. 'gzip, compress, deflate' + (isBrotliSupported ? ', br' : ''), false
  362. );
  363. const options = {
  364. path,
  365. method: method,
  366. headers: headers.toJSON(),
  367. agents: { http: config.httpAgent, https: config.httpsAgent },
  368. auth,
  369. protocol,
  370. family,
  371. beforeRedirect: dispatchBeforeRedirect,
  372. beforeRedirects: {}
  373. };
  374. // cacheable-lookup integration hotfix
  375. !utils.isUndefined(lookup) && (options.lookup = lookup);
  376. if (config.socketPath) {
  377. options.socketPath = config.socketPath;
  378. } else {
  379. options.hostname = parsed.hostname.startsWith("[") ? parsed.hostname.slice(1, -1) : parsed.hostname;
  380. options.port = parsed.port;
  381. setProxy(options, config.proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path);
  382. }
  383. let transport;
  384. const isHttpsRequest = isHttps.test(options.protocol);
  385. options.agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
  386. if (config.transport) {
  387. transport = config.transport;
  388. } else if (config.maxRedirects === 0) {
  389. transport = isHttpsRequest ? https : http;
  390. } else {
  391. if (config.maxRedirects) {
  392. options.maxRedirects = config.maxRedirects;
  393. }
  394. if (config.beforeRedirect) {
  395. options.beforeRedirects.config = config.beforeRedirect;
  396. }
  397. transport = isHttpsRequest ? httpsFollow : httpFollow;
  398. }
  399. if (config.maxBodyLength > -1) {
  400. options.maxBodyLength = config.maxBodyLength;
  401. } else {
  402. // follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
  403. options.maxBodyLength = Infinity;
  404. }
  405. if (config.insecureHTTPParser) {
  406. options.insecureHTTPParser = config.insecureHTTPParser;
  407. }
  408. // Create the request
  409. req = transport.request(options, function handleResponse(res) {
  410. if (req.destroyed) return;
  411. const streams = [res];
  412. const responseLength = +res.headers['content-length'];
  413. if (onDownloadProgress || maxDownloadRate) {
  414. const transformStream = new AxiosTransformStream({
  415. maxRate: utils.toFiniteNumber(maxDownloadRate)
  416. });
  417. onDownloadProgress && transformStream.on('progress', flushOnFinish(
  418. transformStream,
  419. progressEventDecorator(
  420. responseLength,
  421. progressEventReducer(asyncDecorator(onDownloadProgress), true, 3)
  422. )
  423. ));
  424. streams.push(transformStream);
  425. }
  426. // decompress the response body transparently if required
  427. let responseStream = res;
  428. // return the last request in case of redirects
  429. const lastRequest = res.req || req;
  430. // if decompress disabled we should not decompress
  431. if (config.decompress !== false && res.headers['content-encoding']) {
  432. // if no content, but headers still say that it is encoded,
  433. // remove the header not confuse downstream operations
  434. if (method === 'HEAD' || res.statusCode === 204) {
  435. delete res.headers['content-encoding'];
  436. }
  437. switch ((res.headers['content-encoding'] || '').toLowerCase()) {
  438. /*eslint default-case:0*/
  439. case 'gzip':
  440. case 'x-gzip':
  441. case 'compress':
  442. case 'x-compress':
  443. // add the unzipper to the body stream processing pipeline
  444. streams.push(zlib.createUnzip(zlibOptions));
  445. // remove the content-encoding in order to not confuse downstream operations
  446. delete res.headers['content-encoding'];
  447. break;
  448. case 'deflate':
  449. streams.push(new ZlibHeaderTransformStream());
  450. // add the unzipper to the body stream processing pipeline
  451. streams.push(zlib.createUnzip(zlibOptions));
  452. // remove the content-encoding in order to not confuse downstream operations
  453. delete res.headers['content-encoding'];
  454. break;
  455. case 'br':
  456. if (isBrotliSupported) {
  457. streams.push(zlib.createBrotliDecompress(brotliOptions));
  458. delete res.headers['content-encoding'];
  459. }
  460. }
  461. }
  462. responseStream = streams.length > 1 ? stream.pipeline(streams, utils.noop) : streams[0];
  463. const offListeners = stream.finished(responseStream, () => {
  464. offListeners();
  465. onFinished();
  466. });
  467. const response = {
  468. status: res.statusCode,
  469. statusText: res.statusMessage,
  470. headers: new AxiosHeaders(res.headers),
  471. config,
  472. request: lastRequest
  473. };
  474. if (responseType === 'stream') {
  475. response.data = responseStream;
  476. settle(resolve, reject, response);
  477. } else {
  478. const responseBuffer = [];
  479. let totalResponseBytes = 0;
  480. responseStream.on('data', function handleStreamData(chunk) {
  481. responseBuffer.push(chunk);
  482. totalResponseBytes += chunk.length;
  483. // make sure the content length is not over the maxContentLength if specified
  484. if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
  485. // stream.destroy() emit aborted event before calling reject() on Node.js v16
  486. rejected = true;
  487. responseStream.destroy();
  488. reject(new AxiosError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
  489. AxiosError.ERR_BAD_RESPONSE, config, lastRequest));
  490. }
  491. });
  492. responseStream.on('aborted', function handlerStreamAborted() {
  493. if (rejected) {
  494. return;
  495. }
  496. const err = new AxiosError(
  497. 'stream has been aborted',
  498. AxiosError.ERR_BAD_RESPONSE,
  499. config,
  500. lastRequest
  501. );
  502. responseStream.destroy(err);
  503. reject(err);
  504. });
  505. responseStream.on('error', function handleStreamError(err) {
  506. if (req.destroyed) return;
  507. reject(AxiosError.from(err, null, config, lastRequest));
  508. });
  509. responseStream.on('end', function handleStreamEnd() {
  510. try {
  511. let responseData = responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
  512. if (responseType !== 'arraybuffer') {
  513. responseData = responseData.toString(responseEncoding);
  514. if (!responseEncoding || responseEncoding === 'utf8') {
  515. responseData = utils.stripBOM(responseData);
  516. }
  517. }
  518. response.data = responseData;
  519. } catch (err) {
  520. return reject(AxiosError.from(err, null, config, response.request, response));
  521. }
  522. settle(resolve, reject, response);
  523. });
  524. }
  525. emitter.once('abort', err => {
  526. if (!responseStream.destroyed) {
  527. responseStream.emit('error', err);
  528. responseStream.destroy();
  529. }
  530. });
  531. });
  532. emitter.once('abort', err => {
  533. reject(err);
  534. req.destroy(err);
  535. });
  536. // Handle errors
  537. req.on('error', function handleRequestError(err) {
  538. // @todo remove
  539. // if (req.aborted && err.code !== AxiosError.ERR_FR_TOO_MANY_REDIRECTS) return;
  540. reject(AxiosError.from(err, null, config, req));
  541. });
  542. // set tcp keep alive to prevent drop connection by peer
  543. req.on('socket', function handleRequestSocket(socket) {
  544. // default interval of sending ack packet is 1 minute
  545. socket.setKeepAlive(true, 1000 * 60);
  546. });
  547. // Handle request timeout
  548. if (config.timeout) {
  549. // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
  550. const timeout = parseInt(config.timeout, 10);
  551. if (Number.isNaN(timeout)) {
  552. reject(new AxiosError(
  553. 'error trying to parse `config.timeout` to int',
  554. AxiosError.ERR_BAD_OPTION_VALUE,
  555. config,
  556. req
  557. ));
  558. return;
  559. }
  560. // Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
  561. // And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
  562. // 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.
  563. // And then these socket which be hang up will devouring CPU little by little.
  564. // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
  565. req.setTimeout(timeout, function handleRequestTimeout() {
  566. if (isDone) return;
  567. let timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';
  568. const transitional = config.transitional || transitionalDefaults;
  569. if (config.timeoutErrorMessage) {
  570. timeoutErrorMessage = config.timeoutErrorMessage;
  571. }
  572. reject(new AxiosError(
  573. timeoutErrorMessage,
  574. transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
  575. config,
  576. req
  577. ));
  578. abort();
  579. });
  580. }
  581. // Send the request
  582. if (utils.isStream(data)) {
  583. let ended = false;
  584. let errored = false;
  585. data.on('end', () => {
  586. ended = true;
  587. });
  588. data.once('error', err => {
  589. errored = true;
  590. req.destroy(err);
  591. });
  592. data.on('close', () => {
  593. if (!ended && !errored) {
  594. abort(new CanceledError('Request stream has been aborted', config, req));
  595. }
  596. });
  597. data.pipe(req);
  598. } else {
  599. req.end(data);
  600. }
  601. });
  602. }
  603. export const __setProxy = setProxy;