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