499fe0e1d3cc9fec5454198f2108108e9f42501ca79ae1054a89a94060f2a92e91a92307e460586b3542efa654e91255a09e6885cbaba1bf8ee65bfaa72483-exec 15 KB

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