in src/node/main.ts [149:205]
function request(options: XHROptions): Promise<RequestResult> {
let req: http.ClientRequest;
return new Promise<RequestResult>((c, e) => {
const endpoint = parseUrl(options.url);
const opts: https.RequestOptions = {
hostname: endpoint.hostname,
agent: options.agent ? options.agent : false,
port: endpoint.port ? parseInt(endpoint.port) : (endpoint.protocol === 'https:' ? 443 : 80),
path: endpoint.path,
method: options.type || 'GET',
headers: options.headers,
rejectUnauthorized: (typeof options.strictSSL === 'boolean') ? options.strictSSL : true
};
if (options.user && options.password) {
opts.auth = options.user + ':' + options.password;
}
const handler = (res: http.IncomingMessage) => {
if (res.statusCode >= 300 && res.statusCode < 400 && options.followRedirects && options.followRedirects > 0 && res.headers['location']) {
let location = res.headers['location'];
if (location.startsWith('/')) {
location = format({
protocol: endpoint.protocol,
hostname: endpoint.hostname,
port: endpoint.port,
pathname: location
});
}
c(<any>request(assign({}, options, {
url: location,
followRedirects: options.followRedirects - 1
})));
} else {
c({ req, res });
}
}
if (endpoint.protocol === 'https:') {
req = https.request(opts, handler);
} else {
req = http.request(opts, handler);
}
req.on('error', e);
if (options.timeout) {
req.setTimeout(options.timeout);
}
if (options.data) {
req.write(options.data);
}
req.end();
});
}