http.js 39 KB

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