http.js 13 KB

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