http.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 reject = function reject(value) {
  57. done();
  58. rejectPromise(value);
  59. };
  60. var data = config.data;
  61. var headers = config.headers;
  62. var headerNames = {};
  63. Object.keys(headers).forEach(function storeLowerName(name) {
  64. headerNames[name.toLowerCase()] = name;
  65. });
  66. // Set User-Agent (required by some servers)
  67. // See https://github.com/axios/axios/issues/69
  68. if ('user-agent' in headerNames) {
  69. // User-Agent is specified; handle case where no UA header is desired
  70. if (!headers[headerNames['user-agent']]) {
  71. delete headers[headerNames['user-agent']];
  72. }
  73. // Otherwise, use specified value
  74. } else {
  75. // Only set header if it hasn't been set in config
  76. headers['User-Agent'] = 'axios/' + VERSION;
  77. }
  78. if (data && !utils.isStream(data)) {
  79. if (Buffer.isBuffer(data)) {
  80. // Nothing to do...
  81. } else if (utils.isArrayBuffer(data)) {
  82. data = Buffer.from(new Uint8Array(data));
  83. } else if (utils.isString(data)) {
  84. data = Buffer.from(data, 'utf-8');
  85. } else {
  86. return reject(createError(
  87. 'Data after transformation must be a string, an ArrayBuffer, a Buffer, or a Stream',
  88. config
  89. ));
  90. }
  91. // Add Content-Length header if data exists
  92. if (!headerNames['content-length']) {
  93. headers['Content-Length'] = data.length;
  94. }
  95. }
  96. // HTTP basic authentication
  97. var auth = undefined;
  98. if (config.auth) {
  99. var username = config.auth.username || '';
  100. var password = config.auth.password || '';
  101. auth = username + ':' + password;
  102. }
  103. // Parse url
  104. var fullPath = buildFullPath(config.baseURL, config.url);
  105. var parsed = url.parse(fullPath);
  106. var protocol = parsed.protocol || 'http:';
  107. if (!auth && parsed.auth) {
  108. var urlAuth = parsed.auth.split(':');
  109. var urlUsername = urlAuth[0] || '';
  110. var urlPassword = urlAuth[1] || '';
  111. auth = urlUsername + ':' + urlPassword;
  112. }
  113. if (auth && headerNames.authorization) {
  114. delete headers[headerNames.authorization];
  115. }
  116. var isHttpsRequest = isHttps.test(protocol);
  117. var agent = isHttpsRequest ? config.httpsAgent : config.httpAgent;
  118. var options = {
  119. path: buildURL(parsed.path, config.params, config.paramsSerializer).replace(/^\?/, ''),
  120. method: config.method.toUpperCase(),
  121. headers: headers,
  122. agent: agent,
  123. agents: { http: config.httpAgent, https: config.httpsAgent },
  124. auth: auth
  125. };
  126. if (config.socketPath) {
  127. options.socketPath = config.socketPath;
  128. } else {
  129. options.hostname = parsed.hostname;
  130. options.port = parsed.port;
  131. }
  132. var proxy = config.proxy;
  133. if (!proxy && proxy !== false) {
  134. var proxyEnv = protocol.slice(0, -1) + '_proxy';
  135. var proxyUrl = process.env[proxyEnv] || process.env[proxyEnv.toUpperCase()];
  136. if (proxyUrl) {
  137. var parsedProxyUrl = url.parse(proxyUrl);
  138. var noProxyEnv = process.env.no_proxy || process.env.NO_PROXY;
  139. var shouldProxy = true;
  140. if (noProxyEnv) {
  141. var noProxy = noProxyEnv.split(',').map(function trim(s) {
  142. return s.trim();
  143. });
  144. shouldProxy = !noProxy.some(function proxyMatch(proxyElement) {
  145. if (!proxyElement) {
  146. return false;
  147. }
  148. if (proxyElement === '*') {
  149. return true;
  150. }
  151. if (proxyElement[0] === '.' &&
  152. parsed.hostname.substr(parsed.hostname.length - proxyElement.length) === proxyElement) {
  153. return true;
  154. }
  155. return parsed.hostname === proxyElement;
  156. });
  157. }
  158. if (shouldProxy) {
  159. proxy = {
  160. host: parsedProxyUrl.hostname,
  161. port: parsedProxyUrl.port,
  162. protocol: parsedProxyUrl.protocol
  163. };
  164. if (parsedProxyUrl.auth) {
  165. var proxyUrlAuth = parsedProxyUrl.auth.split(':');
  166. proxy.auth = {
  167. username: proxyUrlAuth[0],
  168. password: proxyUrlAuth[1]
  169. };
  170. }
  171. }
  172. }
  173. }
  174. if (proxy) {
  175. options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
  176. setProxy(options, proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path);
  177. }
  178. var transport;
  179. var isHttpsProxy = isHttpsRequest && (proxy ? isHttps.test(proxy.protocol) : true);
  180. if (config.transport) {
  181. transport = config.transport;
  182. } else if (config.maxRedirects === 0) {
  183. transport = isHttpsProxy ? https : http;
  184. } else {
  185. if (config.maxRedirects) {
  186. options.maxRedirects = config.maxRedirects;
  187. }
  188. transport = isHttpsProxy ? httpsFollow : httpFollow;
  189. }
  190. if (config.maxBodyLength > -1) {
  191. options.maxBodyLength = config.maxBodyLength;
  192. }
  193. if (config.insecureHTTPParser) {
  194. options.insecureHTTPParser = config.insecureHTTPParser;
  195. }
  196. // Create the request
  197. var req = transport.request(options, function handleResponse(res) {
  198. if (req.aborted) return;
  199. // uncompress the response body transparently if required
  200. var stream = res;
  201. // return the last request in case of redirects
  202. var lastRequest = res.req || req;
  203. // if no content, is HEAD request or decompress disabled we should not decompress
  204. if (res.statusCode !== 204 && lastRequest.method !== 'HEAD' && config.decompress !== false) {
  205. switch (res.headers['content-encoding']) {
  206. /*eslint default-case:0*/
  207. case 'gzip':
  208. case 'compress':
  209. case 'deflate':
  210. // add the unzipper to the body stream processing pipeline
  211. stream = stream.pipe(zlib.createUnzip());
  212. // remove the content-encoding in order to not confuse downstream operations
  213. delete res.headers['content-encoding'];
  214. break;
  215. }
  216. }
  217. var response = {
  218. status: res.statusCode,
  219. statusText: res.statusMessage,
  220. headers: res.headers,
  221. config: config,
  222. request: lastRequest
  223. };
  224. if (config.responseType === 'stream') {
  225. response.data = stream;
  226. settle(resolve, reject, response);
  227. } else {
  228. var responseBuffer = [];
  229. var totalResponseBytes = 0;
  230. stream.on('data', function handleStreamData(chunk) {
  231. responseBuffer.push(chunk);
  232. totalResponseBytes += chunk.length;
  233. // make sure the content length is not over the maxContentLength if specified
  234. if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
  235. stream.destroy();
  236. reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
  237. config, null, lastRequest));
  238. }
  239. });
  240. stream.on('error', function handleStreamError(err) {
  241. if (req.aborted) return;
  242. reject(enhanceError(err, config, null, lastRequest));
  243. });
  244. stream.on('end', function handleStreamEnd() {
  245. var responseData = Buffer.concat(responseBuffer);
  246. if (config.responseType !== 'arraybuffer') {
  247. responseData = responseData.toString(config.responseEncoding);
  248. if (!config.responseEncoding || config.responseEncoding === 'utf8') {
  249. responseData = utils.stripBOM(responseData);
  250. }
  251. }
  252. response.data = responseData;
  253. settle(resolve, reject, response);
  254. });
  255. }
  256. });
  257. // Handle errors
  258. req.on('error', function handleRequestError(err) {
  259. if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') return;
  260. reject(enhanceError(err, config, null, req));
  261. });
  262. // Handle request timeout
  263. if (config.timeout) {
  264. // This is forcing a int timeout to avoid problems if the `req` interface doesn't handle other types.
  265. var timeout = parseInt(config.timeout, 10);
  266. if (isNaN(timeout)) {
  267. reject(createError(
  268. 'error trying to parse `config.timeout` to int',
  269. config,
  270. 'ERR_PARSE_TIMEOUT',
  271. req
  272. ));
  273. return;
  274. }
  275. // Sometime, the response will be very slow, and does not respond, the connect event will be block by event loop system.
  276. // And timer callback will be fired, and abort() will be invoked before connection, then get "socket hang up" and code ECONNRESET.
  277. // 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.
  278. // And then these socket which be hang up will devoring CPU little by little.
  279. // ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
  280. req.setTimeout(timeout, function handleRequestTimeout() {
  281. req.abort();
  282. var transitional = config.transitional || defaults.transitional;
  283. reject(createError(
  284. 'timeout of ' + timeout + 'ms exceeded',
  285. config,
  286. transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
  287. req
  288. ));
  289. });
  290. }
  291. if (config.cancelToken || config.signal) {
  292. // Handle cancellation
  293. // eslint-disable-next-line func-names
  294. onCanceled = function(cancel) {
  295. if (req.aborted) return;
  296. req.abort();
  297. reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
  298. };
  299. config.cancelToken && config.cancelToken.subscribe(onCanceled);
  300. if (config.signal) {
  301. config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
  302. }
  303. }
  304. // Send the request
  305. if (utils.isStream(data)) {
  306. data.on('error', function handleStreamError(err) {
  307. reject(enhanceError(err, config, null, req));
  308. }).pipe(req);
  309. } else {
  310. req.end(data);
  311. }
  312. });
  313. };