http.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. import utils from '../utils.js';
  2. import settle from '../core/settle.js';
  3. import buildFullPath from '../core/buildFullPath.js';
  4. import buildURL from '../helpers/buildURL.js';
  5. import { getProxyForUrl } from 'proxy-from-env';
  6. import http from 'http';
  7. import https from 'https';
  8. import http2 from 'http2';
  9. import util from 'util';
  10. import { resolve as resolvePath } from 'path';
  11. import followRedirects from 'follow-redirects';
  12. import zlib from 'zlib';
  13. import { VERSION } from '../env/data.js';
  14. import transitionalDefaults from '../defaults/transitional.js';
  15. import AxiosError from '../core/AxiosError.js';
  16. import CanceledError from '../cancel/CanceledError.js';
  17. import platform from '../platform/index.js';
  18. import fromDataURI from '../helpers/fromDataURI.js';
  19. import stream from 'stream';
  20. import AxiosHeaders from '../core/AxiosHeaders.js';
  21. import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
  22. import { EventEmitter } from 'events';
  23. import formDataToStream from '../helpers/formDataToStream.js';
  24. import readBlob from '../helpers/readBlob.js';
  25. import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
  26. import callbackify from '../helpers/callbackify.js';
  27. import shouldBypassProxy from '../helpers/shouldBypassProxy.js';
  28. import {
  29. progressEventReducer,
  30. progressEventDecorator,
  31. asyncDecorator,
  32. } from '../helpers/progressEventReducer.js';
  33. import estimateDataURLDecodedBytes from '../helpers/estimateDataURLDecodedBytes.js';
  34. const zlibOptions = {
  35. flush: zlib.constants.Z_SYNC_FLUSH,
  36. finishFlush: zlib.constants.Z_SYNC_FLUSH,
  37. };
  38. const brotliOptions = {
  39. flush: zlib.constants.BROTLI_OPERATION_FLUSH,
  40. finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH,
  41. };
  42. const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
  43. const { http: httpFollow, https: httpsFollow } = followRedirects;
  44. const isHttps = /https:?/;
  45. // Symbols used to bind a single 'error' listener to a pooled socket and track
  46. // the request currently owning that socket across keep-alive reuse (issue #10780).
  47. const kAxiosSocketListener = Symbol('axios.http.socketListener');
  48. const kAxiosCurrentReq = Symbol('axios.http.currentReq');
  49. const supportedProtocols = platform.protocols.map((protocol) => {
  50. return protocol + ':';
  51. });
  52. const flushOnFinish = (stream, [throttled, flush]) => {
  53. stream.on('end', flush).on('error', flush);
  54. return throttled;
  55. };
  56. class Http2Sessions {
  57. constructor() {
  58. this.sessions = Object.create(null);
  59. }
  60. getSession(authority, options) {
  61. options = Object.assign(
  62. {
  63. sessionTimeout: 1000,
  64. },
  65. options
  66. );
  67. let authoritySessions = this.sessions[authority];
  68. if (authoritySessions) {
  69. let len = authoritySessions.length;
  70. for (let i = 0; i < len; i++) {
  71. const [sessionHandle, sessionOptions] = authoritySessions[i];
  72. if (
  73. !sessionHandle.destroyed &&
  74. !sessionHandle.closed &&
  75. util.isDeepStrictEqual(sessionOptions, options)
  76. ) {
  77. return sessionHandle;
  78. }
  79. }
  80. }
  81. const session = http2.connect(authority, options);
  82. let removed;
  83. const removeSession = () => {
  84. if (removed) {
  85. return;
  86. }
  87. removed = true;
  88. let entries = authoritySessions,
  89. len = entries.length,
  90. i = len;
  91. while (i--) {
  92. if (entries[i][0] === session) {
  93. if (len === 1) {
  94. delete this.sessions[authority];
  95. } else {
  96. entries.splice(i, 1);
  97. }
  98. if (!session.closed) {
  99. session.close();
  100. }
  101. return;
  102. }
  103. }
  104. };
  105. const originalRequestFn = session.request;
  106. const { sessionTimeout } = options;
  107. if (sessionTimeout != null) {
  108. let timer;
  109. let streamsCount = 0;
  110. session.request = function () {
  111. const stream = originalRequestFn.apply(this, arguments);
  112. streamsCount++;
  113. if (timer) {
  114. clearTimeout(timer);
  115. timer = null;
  116. }
  117. stream.once('close', () => {
  118. if (!--streamsCount) {
  119. timer = setTimeout(() => {
  120. timer = null;
  121. removeSession();
  122. }, sessionTimeout);
  123. }
  124. });
  125. return stream;
  126. };
  127. }
  128. session.once('close', removeSession);
  129. let entry = [session, options];
  130. authoritySessions
  131. ? authoritySessions.push(entry)
  132. : (authoritySessions = this.sessions[authority] = [entry]);
  133. return session;
  134. }
  135. }
  136. const http2Sessions = new Http2Sessions();
  137. /**
  138. * If the proxy or config beforeRedirects functions are defined, call them with the options
  139. * object.
  140. *
  141. * @param {Object<string, any>} options - The options object that was passed to the request.
  142. *
  143. * @returns {Object<string, any>}
  144. */
  145. function dispatchBeforeRedirect(options, responseDetails) {
  146. if (options.beforeRedirects.proxy) {
  147. options.beforeRedirects.proxy(options);
  148. }
  149. if (options.beforeRedirects.config) {
  150. options.beforeRedirects.config(options, responseDetails);
  151. }
  152. }
  153. /**
  154. * If the proxy or config afterRedirects functions are defined, call them with the options
  155. *
  156. * @param {http.ClientRequestArgs} options
  157. * @param {AxiosProxyConfig} configProxy configuration from Axios options object
  158. * @param {string} location
  159. *
  160. * @returns {http.ClientRequestArgs}
  161. */
  162. function setProxy(options, configProxy, location) {
  163. let proxy = configProxy;
  164. if (!proxy && proxy !== false) {
  165. const proxyUrl = getProxyForUrl(location);
  166. if (proxyUrl) {
  167. if (!shouldBypassProxy(location)) {
  168. proxy = new URL(proxyUrl);
  169. }
  170. }
  171. }
  172. if (proxy) {
  173. // Basic proxy authorization
  174. if (proxy.username) {
  175. proxy.auth = (proxy.username || '') + ':' + (proxy.password || '');
  176. }
  177. if (proxy.auth) {
  178. // Support proxy auth object form
  179. const validProxyAuth = Boolean(proxy.auth.username || proxy.auth.password);
  180. if (validProxyAuth) {
  181. proxy.auth = (proxy.auth.username || '') + ':' + (proxy.auth.password || '');
  182. } else if (typeof proxy.auth === 'object') {
  183. throw new AxiosError('Invalid proxy authorization', AxiosError.ERR_BAD_OPTION, { proxy });
  184. }
  185. const base64 = Buffer.from(proxy.auth, 'utf8').toString('base64');
  186. options.headers['Proxy-Authorization'] = 'Basic ' + base64;
  187. }
  188. options.headers.host = options.hostname + (options.port ? ':' + options.port : '');
  189. const proxyHost = proxy.hostname || proxy.host;
  190. options.hostname = proxyHost;
  191. // Replace 'host' since options is not a URL object
  192. options.host = proxyHost;
  193. options.port = proxy.port;
  194. options.path = location;
  195. if (proxy.protocol) {
  196. options.protocol = proxy.protocol.includes(':') ? proxy.protocol : `${proxy.protocol}:`;
  197. }
  198. }
  199. options.beforeRedirects.proxy = function beforeRedirect(redirectOptions) {
  200. // Configure proxy for redirected request, passing the original config proxy to apply
  201. // the exact same logic as if the redirected request was performed by axios directly.
  202. setProxy(redirectOptions, configProxy, redirectOptions.href);
  203. };
  204. }
  205. const isHttpAdapterSupported =
  206. typeof process !== 'undefined' && utils.kindOf(process) === 'process';
  207. // temporary hotfix
  208. const wrapAsync = (asyncExecutor) => {
  209. return new Promise((resolve, reject) => {
  210. let onDone;
  211. let isDone;
  212. const done = (value, isRejected) => {
  213. if (isDone) return;
  214. isDone = true;
  215. onDone && onDone(value, isRejected);
  216. };
  217. const _resolve = (value) => {
  218. done(value);
  219. resolve(value);
  220. };
  221. const _reject = (reason) => {
  222. done(reason, true);
  223. reject(reason);
  224. };
  225. asyncExecutor(_resolve, _reject, (onDoneHandler) => (onDone = onDoneHandler)).catch(_reject);
  226. });
  227. };
  228. const resolveFamily = ({ address, family }) => {
  229. if (!utils.isString(address)) {
  230. throw TypeError('address must be a string');
  231. }
  232. return {
  233. address,
  234. family: family || (address.indexOf('.') < 0 ? 6 : 4),
  235. };
  236. };
  237. const buildAddressEntry = (address, family) =>
  238. resolveFamily(utils.isObject(address) ? address : { address, family });
  239. const http2Transport = {
  240. request(options, cb) {
  241. const authority =
  242. options.protocol +
  243. '//' +
  244. options.hostname +
  245. ':' +
  246. (options.port || (options.protocol === 'https:' ? 443 : 80));
  247. const { http2Options, headers } = options;
  248. const session = http2Sessions.getSession(authority, http2Options);
  249. const { HTTP2_HEADER_SCHEME, HTTP2_HEADER_METHOD, HTTP2_HEADER_PATH, HTTP2_HEADER_STATUS } =
  250. http2.constants;
  251. const http2Headers = {
  252. [HTTP2_HEADER_SCHEME]: options.protocol.replace(':', ''),
  253. [HTTP2_HEADER_METHOD]: options.method,
  254. [HTTP2_HEADER_PATH]: options.path,
  255. };
  256. utils.forEach(headers, (header, name) => {
  257. name.charAt(0) !== ':' && (http2Headers[name] = header);
  258. });
  259. const req = session.request(http2Headers);
  260. req.once('response', (responseHeaders) => {
  261. const response = req; //duplex
  262. responseHeaders = Object.assign({}, responseHeaders);
  263. const status = responseHeaders[HTTP2_HEADER_STATUS];
  264. delete responseHeaders[HTTP2_HEADER_STATUS];
  265. response.headers = responseHeaders;
  266. response.statusCode = +status;
  267. cb(response);
  268. });
  269. return req;
  270. },
  271. };
  272. /*eslint consistent-return:0*/
  273. export default isHttpAdapterSupported &&
  274. function httpAdapter(config) {
  275. return wrapAsync(async function dispatchHttpRequest(resolve, reject, onDone) {
  276. const own = (key) => (utils.hasOwnProp(config, key) ? config[key] : undefined);
  277. let data = own('data');
  278. let lookup = own('lookup');
  279. let family = own('family');
  280. let httpVersion = own('httpVersion');
  281. if (httpVersion === undefined) httpVersion = 1;
  282. let http2Options = own('http2Options');
  283. const responseType = own('responseType');
  284. const responseEncoding = own('responseEncoding');
  285. const method = config.method.toUpperCase();
  286. let isDone;
  287. let rejected = false;
  288. let req;
  289. httpVersion = +httpVersion;
  290. if (Number.isNaN(httpVersion)) {
  291. throw TypeError(`Invalid protocol version: '${config.httpVersion}' is not a number`);
  292. }
  293. if (httpVersion !== 1 && httpVersion !== 2) {
  294. throw TypeError(`Unsupported protocol version '${httpVersion}'`);
  295. }
  296. const isHttp2 = httpVersion === 2;
  297. if (lookup) {
  298. const _lookup = callbackify(lookup, (value) => (utils.isArray(value) ? value : [value]));
  299. // hotfix to support opt.all option which is required for node 20.x
  300. lookup = (hostname, opt, cb) => {
  301. _lookup(hostname, opt, (err, arg0, arg1) => {
  302. if (err) {
  303. return cb(err);
  304. }
  305. const addresses = utils.isArray(arg0)
  306. ? arg0.map((addr) => buildAddressEntry(addr))
  307. : [buildAddressEntry(arg0, arg1)];
  308. opt.all ? cb(err, addresses) : cb(err, addresses[0].address, addresses[0].family);
  309. });
  310. };
  311. }
  312. const abortEmitter = new EventEmitter();
  313. function abort(reason) {
  314. try {
  315. abortEmitter.emit(
  316. 'abort',
  317. !reason || reason.type ? new CanceledError(null, config, req) : reason
  318. );
  319. } catch (err) {
  320. console.warn('emit error', err);
  321. }
  322. }
  323. abortEmitter.once('abort', reject);
  324. const onFinished = () => {
  325. if (config.cancelToken) {
  326. config.cancelToken.unsubscribe(abort);
  327. }
  328. if (config.signal) {
  329. config.signal.removeEventListener('abort', abort);
  330. }
  331. abortEmitter.removeAllListeners();
  332. };
  333. if (config.cancelToken || config.signal) {
  334. config.cancelToken && config.cancelToken.subscribe(abort);
  335. if (config.signal) {
  336. config.signal.aborted ? abort() : config.signal.addEventListener('abort', abort);
  337. }
  338. }
  339. onDone((response, isRejected) => {
  340. isDone = true;
  341. if (isRejected) {
  342. rejected = true;
  343. onFinished();
  344. return;
  345. }
  346. const { data } = response;
  347. if (data instanceof stream.Readable || data instanceof stream.Duplex) {
  348. const offListeners = stream.finished(data, () => {
  349. offListeners();
  350. onFinished();
  351. });
  352. } else {
  353. onFinished();
  354. }
  355. });
  356. // Parse url
  357. const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
  358. const parsed = new URL(fullPath, platform.hasBrowserEnv ? platform.origin : undefined);
  359. const protocol = parsed.protocol || supportedProtocols[0];
  360. if (protocol === 'data:') {
  361. // Apply the same semantics as HTTP: only enforce if a finite, non-negative cap is set.
  362. if (config.maxContentLength > -1) {
  363. // Use the exact string passed to fromDataURI (config.url); fall back to fullPath if needed.
  364. const dataUrl = String(config.url || fullPath || '');
  365. const estimated = estimateDataURLDecodedBytes(dataUrl);
  366. if (estimated > config.maxContentLength) {
  367. return reject(
  368. new AxiosError(
  369. 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
  370. AxiosError.ERR_BAD_RESPONSE,
  371. config
  372. )
  373. );
  374. }
  375. }
  376. let convertedData;
  377. if (method !== 'GET') {
  378. return settle(resolve, reject, {
  379. status: 405,
  380. statusText: 'method not allowed',
  381. headers: {},
  382. config,
  383. });
  384. }
  385. try {
  386. convertedData = fromDataURI(config.url, responseType === 'blob', {
  387. Blob: config.env && config.env.Blob,
  388. });
  389. } catch (err) {
  390. throw AxiosError.from(err, AxiosError.ERR_BAD_REQUEST, config);
  391. }
  392. if (responseType === 'text') {
  393. convertedData = convertedData.toString(responseEncoding);
  394. if (!responseEncoding || responseEncoding === 'utf8') {
  395. convertedData = utils.stripBOM(convertedData);
  396. }
  397. } else if (responseType === 'stream') {
  398. convertedData = stream.Readable.from(convertedData);
  399. }
  400. return settle(resolve, reject, {
  401. data: convertedData,
  402. status: 200,
  403. statusText: 'OK',
  404. headers: new AxiosHeaders(),
  405. config,
  406. });
  407. }
  408. if (supportedProtocols.indexOf(protocol) === -1) {
  409. return reject(
  410. new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_BAD_REQUEST, config)
  411. );
  412. }
  413. const headers = AxiosHeaders.from(config.headers).normalize();
  414. // Set User-Agent (required by some servers)
  415. // See https://github.com/axios/axios/issues/69
  416. // User-Agent is specified; handle case where no UA header is desired
  417. // Only set header if it hasn't been set in config
  418. headers.set('User-Agent', 'axios/' + VERSION, false);
  419. const { onUploadProgress, onDownloadProgress } = config;
  420. const maxRate = config.maxRate;
  421. let maxUploadRate = undefined;
  422. let maxDownloadRate = undefined;
  423. // support for spec compliant FormData objects
  424. if (utils.isSpecCompliantForm(data)) {
  425. const userBoundary = headers.getContentType(/boundary=([-_\w\d]{10,70})/i);
  426. data = formDataToStream(
  427. data,
  428. (formHeaders) => {
  429. headers.set(formHeaders);
  430. },
  431. {
  432. tag: `axios-${VERSION}-boundary`,
  433. boundary: (userBoundary && userBoundary[1]) || undefined,
  434. }
  435. );
  436. // support for https://www.npmjs.com/package/form-data api
  437. } else if (utils.isFormData(data) && utils.isFunction(data.getHeaders) &&
  438. data.getHeaders !== Object.prototype.getHeaders) {
  439. headers.set(data.getHeaders());
  440. if (!headers.hasContentLength()) {
  441. try {
  442. const knownLength = await util.promisify(data.getLength).call(data);
  443. Number.isFinite(knownLength) &&
  444. knownLength >= 0 &&
  445. headers.setContentLength(knownLength);
  446. /*eslint no-empty:0*/
  447. } catch (e) {}
  448. }
  449. } else if (utils.isBlob(data) || utils.isFile(data)) {
  450. data.size && headers.setContentType(data.type || 'application/octet-stream');
  451. headers.setContentLength(data.size || 0);
  452. data = stream.Readable.from(readBlob(data));
  453. } else if (data && !utils.isStream(data)) {
  454. if (Buffer.isBuffer(data)) {
  455. // Nothing to do...
  456. } else if (utils.isArrayBuffer(data)) {
  457. data = Buffer.from(new Uint8Array(data));
  458. } else if (utils.isString(data)) {
  459. data = Buffer.from(data, 'utf-8');
  460. } else {
  461. return reject(
  462. new AxiosError(
  463. 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
  464. AxiosError.ERR_BAD_REQUEST,
  465. config
  466. )
  467. );
  468. }
  469. // Add Content-Length header if data exists
  470. headers.setContentLength(data.length, false);
  471. if (config.maxBodyLength > -1 && data.length > config.maxBodyLength) {
  472. return reject(
  473. new AxiosError(
  474. 'Request body larger than maxBodyLength limit',
  475. AxiosError.ERR_BAD_REQUEST,
  476. config
  477. )
  478. );
  479. }
  480. }
  481. const contentLength = utils.toFiniteNumber(headers.getContentLength());
  482. if (utils.isArray(maxRate)) {
  483. maxUploadRate = maxRate[0];
  484. maxDownloadRate = maxRate[1];
  485. } else {
  486. maxUploadRate = maxDownloadRate = maxRate;
  487. }
  488. if (data && (onUploadProgress || maxUploadRate)) {
  489. if (!utils.isStream(data)) {
  490. data = stream.Readable.from(data, { objectMode: false });
  491. }
  492. data = stream.pipeline(
  493. [
  494. data,
  495. new AxiosTransformStream({
  496. maxRate: utils.toFiniteNumber(maxUploadRate),
  497. }),
  498. ],
  499. utils.noop
  500. );
  501. onUploadProgress &&
  502. data.on(
  503. 'progress',
  504. flushOnFinish(
  505. data,
  506. progressEventDecorator(
  507. contentLength,
  508. progressEventReducer(asyncDecorator(onUploadProgress), false, 3)
  509. )
  510. )
  511. );
  512. }
  513. // HTTP basic authentication
  514. let auth = undefined;
  515. const configAuth = own('auth');
  516. if (configAuth) {
  517. const username = configAuth.username || '';
  518. const password = configAuth.password || '';
  519. auth = username + ':' + password;
  520. }
  521. if (!auth && parsed.username) {
  522. const urlUsername = parsed.username;
  523. const urlPassword = parsed.password;
  524. auth = urlUsername + ':' + urlPassword;
  525. }
  526. auth && headers.delete('authorization');
  527. let path;
  528. try {
  529. path = buildURL(
  530. parsed.pathname + parsed.search,
  531. config.params,
  532. config.paramsSerializer
  533. ).replace(/^\?/, '');
  534. } catch (err) {
  535. const customErr = new Error(err.message);
  536. customErr.config = config;
  537. customErr.url = config.url;
  538. customErr.exists = true;
  539. return reject(customErr);
  540. }
  541. headers.set(
  542. 'Accept-Encoding',
  543. 'gzip, compress, deflate' + (isBrotliSupported ? ', br' : ''),
  544. false
  545. );
  546. // Null-prototype to block prototype pollution gadgets on properties read
  547. // directly by Node's http.request (e.g. insecureHTTPParser, lookup).
  548. // See GHSA-q8qp-cvcw-x6jj.
  549. const options = Object.assign(Object.create(null), {
  550. path,
  551. method: method,
  552. headers: headers.toJSON(),
  553. agents: { http: config.httpAgent, https: config.httpsAgent },
  554. auth,
  555. protocol,
  556. family,
  557. beforeRedirect: dispatchBeforeRedirect,
  558. beforeRedirects: Object.create(null),
  559. http2Options,
  560. });
  561. // cacheable-lookup integration hotfix
  562. !utils.isUndefined(lookup) && (options.lookup = lookup);
  563. if (config.socketPath) {
  564. if (typeof config.socketPath !== 'string') {
  565. return reject(new AxiosError(
  566. 'socketPath must be a string',
  567. AxiosError.ERR_BAD_OPTION_VALUE,
  568. config
  569. ));
  570. }
  571. if (config.allowedSocketPaths != null) {
  572. const allowed = Array.isArray(config.allowedSocketPaths)
  573. ? config.allowedSocketPaths
  574. : [config.allowedSocketPaths];
  575. const resolvedSocket = resolvePath(config.socketPath);
  576. const isAllowed = allowed.some(
  577. (entry) => typeof entry === 'string' && resolvePath(entry) === resolvedSocket
  578. );
  579. if (!isAllowed) {
  580. return reject(new AxiosError(
  581. `socketPath "${config.socketPath}" is not permitted by allowedSocketPaths`,
  582. AxiosError.ERR_BAD_OPTION_VALUE,
  583. config
  584. ));
  585. }
  586. }
  587. options.socketPath = config.socketPath;
  588. } else {
  589. options.hostname = parsed.hostname.startsWith('[')
  590. ? parsed.hostname.slice(1, -1)
  591. : parsed.hostname;
  592. options.port = parsed.port;
  593. setProxy(
  594. options,
  595. config.proxy,
  596. protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path
  597. );
  598. }
  599. let transport;
  600. const isHttpsRequest = isHttps.test(options.protocol);
  601. options.agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
  602. if (isHttp2) {
  603. transport = http2Transport;
  604. } else {
  605. const configTransport = own('transport');
  606. if (configTransport) {
  607. transport = configTransport;
  608. } else if (config.maxRedirects === 0) {
  609. transport = isHttpsRequest ? https : http;
  610. } else {
  611. if (config.maxRedirects) {
  612. options.maxRedirects = config.maxRedirects;
  613. }
  614. const configBeforeRedirect = own('beforeRedirect');
  615. if (configBeforeRedirect) {
  616. options.beforeRedirects.config = configBeforeRedirect;
  617. }
  618. transport = isHttpsRequest ? httpsFollow : httpFollow;
  619. }
  620. }
  621. if (config.maxBodyLength > -1) {
  622. options.maxBodyLength = config.maxBodyLength;
  623. } else {
  624. // follow-redirects does not skip comparison, so it should always succeed for axios -1 unlimited
  625. options.maxBodyLength = Infinity;
  626. }
  627. // Always set an explicit own value so a polluted
  628. // Object.prototype.insecureHTTPParser cannot enable the lenient parser
  629. // through Node's internal options copy (GHSA-q8qp-cvcw-x6jj).
  630. options.insecureHTTPParser = Boolean(own('insecureHTTPParser'));
  631. // Create the request
  632. req = transport.request(options, function handleResponse(res) {
  633. if (req.destroyed) return;
  634. const streams = [res];
  635. const responseLength = utils.toFiniteNumber(res.headers['content-length']);
  636. if (onDownloadProgress || maxDownloadRate) {
  637. const transformStream = new AxiosTransformStream({
  638. maxRate: utils.toFiniteNumber(maxDownloadRate),
  639. });
  640. onDownloadProgress &&
  641. transformStream.on(
  642. 'progress',
  643. flushOnFinish(
  644. transformStream,
  645. progressEventDecorator(
  646. responseLength,
  647. progressEventReducer(asyncDecorator(onDownloadProgress), true, 3)
  648. )
  649. )
  650. );
  651. streams.push(transformStream);
  652. }
  653. // decompress the response body transparently if required
  654. let responseStream = res;
  655. // return the last request in case of redirects
  656. const lastRequest = res.req || req;
  657. // if decompress disabled we should not decompress
  658. if (config.decompress !== false && res.headers['content-encoding']) {
  659. // if no content, but headers still say that it is encoded,
  660. // remove the header not confuse downstream operations
  661. if (method === 'HEAD' || res.statusCode === 204) {
  662. delete res.headers['content-encoding'];
  663. }
  664. switch ((res.headers['content-encoding'] || '').toLowerCase()) {
  665. /*eslint default-case:0*/
  666. case 'gzip':
  667. case 'x-gzip':
  668. case 'compress':
  669. case 'x-compress':
  670. // add the unzipper to the body stream processing pipeline
  671. streams.push(zlib.createUnzip(zlibOptions));
  672. // remove the content-encoding in order to not confuse downstream operations
  673. delete res.headers['content-encoding'];
  674. break;
  675. case 'deflate':
  676. streams.push(new ZlibHeaderTransformStream());
  677. // add the unzipper to the body stream processing pipeline
  678. streams.push(zlib.createUnzip(zlibOptions));
  679. // remove the content-encoding in order to not confuse downstream operations
  680. delete res.headers['content-encoding'];
  681. break;
  682. case 'br':
  683. if (isBrotliSupported) {
  684. streams.push(zlib.createBrotliDecompress(brotliOptions));
  685. delete res.headers['content-encoding'];
  686. }
  687. }
  688. }
  689. responseStream = streams.length > 1 ? stream.pipeline(streams, utils.noop) : streams[0];
  690. const response = {
  691. status: res.statusCode,
  692. statusText: res.statusMessage,
  693. headers: new AxiosHeaders(res.headers),
  694. config,
  695. request: lastRequest,
  696. };
  697. if (responseType === 'stream') {
  698. // Enforce maxContentLength on streamed responses; previously this
  699. // was applied only to buffered responses. See GHSA-vf2m-468p-8v99.
  700. if (config.maxContentLength > -1) {
  701. const limit = config.maxContentLength;
  702. const source = responseStream;
  703. async function* enforceMaxContentLength() {
  704. let totalResponseBytes = 0;
  705. for await (const chunk of source) {
  706. totalResponseBytes += chunk.length;
  707. if (totalResponseBytes > limit) {
  708. throw new AxiosError(
  709. 'maxContentLength size of ' + limit + ' exceeded',
  710. AxiosError.ERR_BAD_RESPONSE,
  711. config,
  712. lastRequest
  713. );
  714. }
  715. yield chunk;
  716. }
  717. }
  718. responseStream = stream.Readable.from(enforceMaxContentLength(), {
  719. objectMode: false,
  720. });
  721. }
  722. response.data = responseStream;
  723. settle(resolve, reject, response);
  724. } else {
  725. const responseBuffer = [];
  726. let totalResponseBytes = 0;
  727. responseStream.on('data', function handleStreamData(chunk) {
  728. responseBuffer.push(chunk);
  729. totalResponseBytes += chunk.length;
  730. // make sure the content length is not over the maxContentLength if specified
  731. if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
  732. // stream.destroy() emit aborted event before calling reject() on Node.js v16
  733. rejected = true;
  734. responseStream.destroy();
  735. abort(
  736. new AxiosError(
  737. 'maxContentLength size of ' + config.maxContentLength + ' exceeded',
  738. AxiosError.ERR_BAD_RESPONSE,
  739. config,
  740. lastRequest
  741. )
  742. );
  743. }
  744. });
  745. responseStream.on('aborted', function handlerStreamAborted() {
  746. if (rejected) {
  747. return;
  748. }
  749. const err = new AxiosError(
  750. 'stream has been aborted',
  751. AxiosError.ERR_BAD_RESPONSE,
  752. config,
  753. lastRequest
  754. );
  755. responseStream.destroy(err);
  756. reject(err);
  757. });
  758. responseStream.on('error', function handleStreamError(err) {
  759. if (req.destroyed) return;
  760. reject(AxiosError.from(err, null, config, lastRequest));
  761. });
  762. responseStream.on('end', function handleStreamEnd() {
  763. try {
  764. let responseData =
  765. responseBuffer.length === 1 ? responseBuffer[0] : Buffer.concat(responseBuffer);
  766. if (responseType !== 'arraybuffer') {
  767. responseData = responseData.toString(responseEncoding);
  768. if (!responseEncoding || responseEncoding === 'utf8') {
  769. responseData = utils.stripBOM(responseData);
  770. }
  771. }
  772. response.data = responseData;
  773. } catch (err) {
  774. return reject(AxiosError.from(err, null, config, response.request, response));
  775. }
  776. settle(resolve, reject, response);
  777. });
  778. }
  779. abortEmitter.once('abort', (err) => {
  780. if (!responseStream.destroyed) {
  781. responseStream.emit('error', err);
  782. responseStream.destroy();
  783. }
  784. });
  785. });
  786. abortEmitter.once('abort', (err) => {
  787. if (req.close) {
  788. req.close();
  789. } else {
  790. req.destroy(err);
  791. }
  792. });
  793. // Handle errors
  794. req.on('error', function handleRequestError(err) {
  795. reject(AxiosError.from(err, null, config, req));
  796. });
  797. // set tcp keep alive to prevent drop connection by peer
  798. req.on('socket', function handleRequestSocket(socket) {
  799. // default interval of sending ack packet is 1 minute
  800. socket.setKeepAlive(true, 1000 * 60);
  801. // Install a single 'error' listener per socket (not per request) to avoid
  802. // accumulating listeners on pooled keep-alive sockets that get reassigned
  803. // to new requests before the previous request's 'close' fires (issue #10780).
  804. // The listener is bound to the socket's currently-active request via a
  805. // symbol, which is swapped as the socket is reassigned.
  806. if (!socket[kAxiosSocketListener]) {
  807. socket.on('error', function handleSocketError(err) {
  808. const current = socket[kAxiosCurrentReq];
  809. if (current && !current.destroyed) {
  810. current.destroy(err);
  811. }
  812. });
  813. socket[kAxiosSocketListener] = true;
  814. }
  815. socket[kAxiosCurrentReq] = req;
  816. req.once('close', function clearCurrentReq() {
  817. if (socket[kAxiosCurrentReq] === req) {
  818. socket[kAxiosCurrentReq] = null;
  819. }
  820. });
  821. });
  822. // Handle request timeout
  823. if (config.timeout) {
  824. // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
  825. const timeout = parseInt(config.timeout, 10);
  826. if (Number.isNaN(timeout)) {
  827. abort(
  828. new AxiosError(
  829. 'error trying to parse `config.timeout` to int',
  830. AxiosError.ERR_BAD_OPTION_VALUE,
  831. config,
  832. req
  833. )
  834. );
  835. return;
  836. }
  837. // Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
  838. // And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
  839. // 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.
  840. // And then these socket which be hang up will devouring CPU little by little.
  841. // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
  842. req.setTimeout(timeout, function handleRequestTimeout() {
  843. if (isDone) return;
  844. let timeoutErrorMessage = config.timeout
  845. ? 'timeout of ' + config.timeout + 'ms exceeded'
  846. : 'timeout exceeded';
  847. const transitional = config.transitional || transitionalDefaults;
  848. if (config.timeoutErrorMessage) {
  849. timeoutErrorMessage = config.timeoutErrorMessage;
  850. }
  851. abort(
  852. new AxiosError(
  853. timeoutErrorMessage,
  854. transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED,
  855. config,
  856. req
  857. )
  858. );
  859. });
  860. } else {
  861. // explicitly reset the socket timeout value for a possible `keep-alive` request
  862. req.setTimeout(0);
  863. }
  864. // Send the request
  865. if (utils.isStream(data)) {
  866. let ended = false;
  867. let errored = false;
  868. data.on('end', () => {
  869. ended = true;
  870. });
  871. data.once('error', (err) => {
  872. errored = true;
  873. req.destroy(err);
  874. });
  875. data.on('close', () => {
  876. if (!ended && !errored) {
  877. abort(new CanceledError('Request stream has been aborted', config, req));
  878. }
  879. });
  880. // Enforce maxBodyLength for streamed uploads on the native http/https
  881. // transport (maxRedirects === 0); follow-redirects enforces it on the
  882. // other path. See GHSA-5c9x-8gcm-mpgx.
  883. let uploadStream = data;
  884. if (config.maxBodyLength > -1 && config.maxRedirects === 0) {
  885. const limit = config.maxBodyLength;
  886. let bytesSent = 0;
  887. uploadStream = stream.pipeline(
  888. [
  889. data,
  890. new stream.Transform({
  891. transform(chunk, _enc, cb) {
  892. bytesSent += chunk.length;
  893. if (bytesSent > limit) {
  894. return cb(
  895. new AxiosError(
  896. 'Request body larger than maxBodyLength limit',
  897. AxiosError.ERR_BAD_REQUEST,
  898. config,
  899. req
  900. )
  901. );
  902. }
  903. cb(null, chunk);
  904. },
  905. }),
  906. ],
  907. utils.noop
  908. );
  909. uploadStream.on('error', (err) => {
  910. if (!req.destroyed) req.destroy(err);
  911. });
  912. }
  913. uploadStream.pipe(req);
  914. } else {
  915. data && req.write(data);
  916. req.end();
  917. }
  918. });
  919. };
  920. export const __setProxy = setProxy;