http.js 21 KB

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