2017-01-09 01:21:28 +03:00
|
|
|
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
import url = require("url");
|
|
|
|
import http = require("http");
|
|
|
|
import https = require("https");
|
|
|
|
import tunnel = require("tunnel");
|
|
|
|
import ifm = require('./Interfaces');
|
2017-08-29 17:34:32 +03:00
|
|
|
import fs = require('fs');
|
2017-01-09 01:21:28 +03:00
|
|
|
|
|
|
|
export enum HttpCodes {
|
|
|
|
OK = 200,
|
|
|
|
MultipleChoices = 300,
|
2017-07-19 00:58:14 +03:00
|
|
|
MovedPermanently = 301,
|
2017-01-09 01:21:28 +03:00
|
|
|
ResourceMoved = 302,
|
2018-08-24 05:53:38 +03:00
|
|
|
SeeOther = 303,
|
2017-01-09 01:21:28 +03:00
|
|
|
NotModified = 304,
|
|
|
|
UseProxy = 305,
|
|
|
|
SwitchProxy = 306,
|
|
|
|
TemporaryRedirect = 307,
|
|
|
|
PermanentRedirect = 308,
|
|
|
|
BadRequest = 400,
|
|
|
|
Unauthorized = 401,
|
|
|
|
PaymentRequired = 402,
|
|
|
|
Forbidden = 403,
|
|
|
|
NotFound = 404,
|
|
|
|
MethodNotAllowed = 405,
|
|
|
|
NotAcceptable = 406,
|
|
|
|
ProxyAuthenticationRequired = 407,
|
|
|
|
RequestTimeout = 408,
|
|
|
|
Conflict = 409,
|
|
|
|
Gone = 410,
|
|
|
|
InternalServerError = 500,
|
|
|
|
NotImplemented = 501,
|
|
|
|
BadGateway = 502,
|
|
|
|
ServiceUnavailable = 503,
|
|
|
|
GatewayTimeout = 504,
|
|
|
|
}
|
|
|
|
|
2018-08-24 05:53:38 +03:00
|
|
|
const HttpRedirectCodes: number[] = [HttpCodes.MovedPermanently, HttpCodes.ResourceMoved, HttpCodes.SeeOther, HttpCodes.TemporaryRedirect, HttpCodes.PermanentRedirect];
|
2017-11-18 15:07:22 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
export class HttpClientResponse implements ifm.IHttpClientResponse {
|
2017-01-09 01:21:28 +03:00
|
|
|
constructor(message: http.IncomingMessage) {
|
|
|
|
this.message = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public message: http.IncomingMessage;
|
|
|
|
readBody(): Promise<string> {
|
2017-07-19 00:58:14 +03:00
|
|
|
return new Promise<string>(async (resolve, reject) => {
|
2017-01-09 01:21:28 +03:00
|
|
|
let output: string = '';
|
|
|
|
|
|
|
|
this.message.on('data', (chunk: string) => {
|
|
|
|
output += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.message.on('end', () => {
|
|
|
|
resolve(output);
|
2017-07-19 00:58:14 +03:00
|
|
|
});
|
|
|
|
});
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface RequestInfo {
|
|
|
|
options: http.RequestOptions;
|
|
|
|
parsedUrl: url.Url;
|
|
|
|
httpModule: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isHttps(requestUrl: string) {
|
|
|
|
let parsedUrl: url.Url = url.parse(requestUrl);
|
2017-07-19 00:58:14 +03:00
|
|
|
return parsedUrl.protocol === 'https:';
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
|
2017-11-18 15:07:22 +03:00
|
|
|
enum EnvironmentVariables {
|
2018-01-18 01:30:25 +03:00
|
|
|
HTTP_PROXY = "HTTP_PROXY",
|
2017-11-18 15:07:22 +03:00
|
|
|
HTTPS_PROXY = "HTTPS_PROXY",
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
export class HttpClient implements ifm.IHttpClient {
|
2017-01-09 01:21:28 +03:00
|
|
|
userAgent: string;
|
|
|
|
handlers: ifm.IRequestHandler[];
|
2017-07-19 00:58:14 +03:00
|
|
|
requestOptions: ifm.IRequestOptions;
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2017-07-19 00:58:14 +03:00
|
|
|
private _ignoreSslError: boolean = false;
|
|
|
|
private _socketTimeout: number;
|
|
|
|
private _httpProxy: ifm.IProxyConfiguration;
|
|
|
|
private _httpProxyBypassHosts: RegExp[];
|
2017-11-18 15:07:22 +03:00
|
|
|
private _allowRedirects: boolean = true;
|
|
|
|
private _maxRedirects: number = 50
|
2017-12-06 00:34:57 +03:00
|
|
|
private _agent;
|
|
|
|
private _proxyAgent;
|
|
|
|
private _keepAlive: boolean = false;
|
|
|
|
private _disposed: boolean = false;
|
2017-08-29 17:34:32 +03:00
|
|
|
private _certConfig: ifm.ICertConfiguration;
|
|
|
|
private _ca: string;
|
|
|
|
private _cert: string;
|
|
|
|
private _key: string;
|
|
|
|
|
2017-07-19 00:58:14 +03:00
|
|
|
constructor(userAgent: string, handlers?: ifm.IRequestHandler[], requestOptions?: ifm.IRequestOptions) {
|
2017-01-09 01:21:28 +03:00
|
|
|
this.userAgent = userAgent;
|
2018-01-18 01:30:25 +03:00
|
|
|
this.handlers = handlers || [];
|
2017-07-19 00:58:14 +03:00
|
|
|
this.requestOptions = requestOptions;
|
|
|
|
if (requestOptions) {
|
|
|
|
if (requestOptions.ignoreSslError != null) {
|
|
|
|
this._ignoreSslError = requestOptions.ignoreSslError;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._socketTimeout = requestOptions.socketTimeout;
|
|
|
|
this._httpProxy = requestOptions.proxy;
|
|
|
|
if (requestOptions.proxy && requestOptions.proxy.proxyBypassHosts) {
|
|
|
|
this._httpProxyBypassHosts = [];
|
|
|
|
requestOptions.proxy.proxyBypassHosts.forEach(bypass => {
|
|
|
|
this._httpProxyBypassHosts.push(new RegExp(bypass, 'i'));
|
|
|
|
});
|
|
|
|
}
|
2017-08-29 17:34:32 +03:00
|
|
|
|
|
|
|
this._certConfig = requestOptions.cert;
|
|
|
|
|
|
|
|
// cache the cert content into memory, so we don't have to read it from disk every time
|
|
|
|
if (this._certConfig && this._certConfig.caFile && fs.existsSync(this._certConfig.caFile)) {
|
|
|
|
this._ca = fs.readFileSync(this._certConfig.caFile, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._certConfig && this._certConfig.certFile && fs.existsSync(this._certConfig.certFile)) {
|
|
|
|
this._cert = fs.readFileSync(this._certConfig.certFile, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._certConfig && this._certConfig.keyFile && fs.existsSync(this._certConfig.keyFile)) {
|
|
|
|
this._key = fs.readFileSync(this._certConfig.keyFile, 'utf8');
|
|
|
|
}
|
2017-11-18 15:07:22 +03:00
|
|
|
|
|
|
|
if (requestOptions.allowRedirects != null) {
|
|
|
|
this._allowRedirects = requestOptions.allowRedirects;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestOptions.maxRedirects != null) {
|
|
|
|
this._maxRedirects = Math.max(requestOptions.maxRedirects, 0);
|
|
|
|
}
|
2017-12-06 00:34:57 +03:00
|
|
|
|
|
|
|
if (requestOptions.keepAlive != null) {
|
|
|
|
this._keepAlive = requestOptions.keepAlive;
|
|
|
|
}
|
2017-07-19 00:58:14 +03:00
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public options(requestUrl: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-03-28 23:30:56 +03:00
|
|
|
return this.request('OPTIONS', requestUrl, null, additionalHeaders || {});
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public get(requestUrl: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-01-09 01:21:28 +03:00
|
|
|
return this.request('GET', requestUrl, null, additionalHeaders || {});
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public del(requestUrl: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-01-09 01:21:28 +03:00
|
|
|
return this.request('DELETE', requestUrl, null, additionalHeaders || {});
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public post(requestUrl: string, data: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-01-09 01:21:28 +03:00
|
|
|
return this.request('POST', requestUrl, data, additionalHeaders || {});
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public patch(requestUrl: string, data: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-01-09 01:21:28 +03:00
|
|
|
return this.request('PATCH', requestUrl, data, additionalHeaders || {});
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public put(requestUrl: string, data: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-01-09 01:21:28 +03:00
|
|
|
return this.request('PUT', requestUrl, data, additionalHeaders || {});
|
2017-07-19 00:58:14 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public head(requestUrl: string, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-07-19 00:58:14 +03:00
|
|
|
return this.request('HEAD', requestUrl, null, additionalHeaders || {});
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
public sendStream(verb: string, requestUrl: string, stream: NodeJS.ReadableStream, additionalHeaders?: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-01-09 01:21:28 +03:00
|
|
|
return this.request(verb, requestUrl, stream, additionalHeaders);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a raw http request.
|
|
|
|
* All other methods such as get, post, patch, and request ultimately call this.
|
|
|
|
* Prefer get, del, post and patch
|
|
|
|
*/
|
2018-01-18 01:30:25 +03:00
|
|
|
public async request(verb: string, requestUrl: string, data: string | NodeJS.ReadableStream, headers: ifm.IHeaders): Promise<ifm.IHttpClientResponse> {
|
2017-12-06 00:34:57 +03:00
|
|
|
if (this._disposed) {
|
2018-01-18 01:30:25 +03:00
|
|
|
throw new Error("Client has already been disposed.");
|
2017-12-06 00:34:57 +03:00
|
|
|
}
|
|
|
|
|
2017-11-18 15:07:22 +03:00
|
|
|
let info: RequestInfo = this._prepareRequest(verb, requestUrl, headers);
|
2018-01-18 01:30:25 +03:00
|
|
|
let response: HttpClientResponse = await this.requestRaw(info, data);
|
|
|
|
|
|
|
|
// Check if it's an authentication challenge
|
|
|
|
if (response && response.message && response.message.statusCode === HttpCodes.Unauthorized) {
|
|
|
|
let authenticationHandler: ifm.IRequestHandler;
|
|
|
|
|
|
|
|
for (let i = 0; i < this.handlers.length; i++) {
|
|
|
|
if (this.handlers[i].canHandleAuthentication(response)) {
|
|
|
|
authenticationHandler = this.handlers[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authenticationHandler) {
|
|
|
|
return authenticationHandler.handleAuthentication(this, info, data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We have received an unauthorized response but have no handlers to handle it.
|
|
|
|
// Let the response return to the caller.
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 14:02:44 +03:00
|
|
|
|
2017-11-18 15:07:22 +03:00
|
|
|
let redirectsRemaining: number = this._maxRedirects;
|
|
|
|
while (HttpRedirectCodes.indexOf(response.message.statusCode) != -1
|
|
|
|
&& this._allowRedirects
|
|
|
|
&& redirectsRemaining > 0) {
|
|
|
|
|
2017-11-20 14:02:44 +03:00
|
|
|
const redirectUrl: any = response.message.headers["location"];
|
|
|
|
if (!redirectUrl) {
|
|
|
|
// if there's no location to redirect to, we won't
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need to finish reading the response before reassigning response
|
|
|
|
// which will leak the open socket.
|
|
|
|
await response.readBody();
|
2017-11-18 15:07:22 +03:00
|
|
|
|
2017-11-20 14:02:44 +03:00
|
|
|
// let's make the request with the new redirectUrl
|
|
|
|
info = this._prepareRequest(verb, redirectUrl, headers);
|
2018-01-18 01:30:25 +03:00
|
|
|
response = await this.requestRaw(info, data);
|
2017-11-20 14:02:44 +03:00
|
|
|
redirectsRemaining--;
|
2017-11-18 15:07:22 +03:00
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2017-11-18 15:07:22 +03:00
|
|
|
return response;
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
/**
|
|
|
|
* Needs to be called if keepAlive is set to true in request options.
|
|
|
|
*/
|
|
|
|
public dispose() {
|
|
|
|
if (this._agent) {
|
|
|
|
this._agent.destroy();
|
|
|
|
}
|
2018-01-30 03:14:16 +03:00
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
this._disposed = true;
|
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
/**
|
|
|
|
* Raw request.
|
|
|
|
* @param info
|
|
|
|
* @param data
|
|
|
|
*/
|
|
|
|
public requestRaw(info: ifm.IRequestInfo, data: string | NodeJS.ReadableStream): Promise<ifm.IHttpClientResponse> {
|
|
|
|
return new Promise<ifm.IHttpClientResponse>((resolve, reject) => {
|
|
|
|
let callbackForResult = function (err: any, res: ifm.IHttpClientResponse) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
resolve(res);
|
|
|
|
};
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
this.requestRawWithCallback(info, data, callbackForResult);
|
|
|
|
});
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
/**
|
|
|
|
* Raw request with callback.
|
|
|
|
* @param info
|
|
|
|
* @param data
|
|
|
|
* @param onResult
|
|
|
|
*/
|
|
|
|
public requestRawWithCallback(info: ifm.IRequestInfo, data: string | NodeJS.ReadableStream, onResult: (err: any, res: ifm.IHttpClientResponse) => void): void {
|
|
|
|
let socket;
|
|
|
|
|
|
|
|
let isDataString = typeof (data) === 'string';
|
|
|
|
if (typeof (data) === 'string') {
|
|
|
|
info.options.headers["Content-Length"] = Buffer.byteLength(data, 'utf8');
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
let callbackCalled: boolean = false;
|
|
|
|
let handleResult = (err: any, res: HttpClientResponse) => {
|
|
|
|
if (!callbackCalled) {
|
|
|
|
callbackCalled = true;
|
|
|
|
onResult(err, res);
|
|
|
|
}
|
|
|
|
};
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
let req: http.ClientRequest = info.httpModule.request(info.options, (msg: http.IncomingMessage) => {
|
|
|
|
let res: HttpClientResponse = new HttpClientResponse(msg);
|
|
|
|
handleResult(null, res);
|
|
|
|
});
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
req.on('socket', (sock) => {
|
|
|
|
socket = sock;
|
|
|
|
});
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
// If we ever get disconnected, we want the socket to timeout eventually
|
|
|
|
req.setTimeout(this._socketTimeout || 3 * 60000, () => {
|
|
|
|
if (socket) {
|
|
|
|
socket.end();
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
2018-01-18 01:30:25 +03:00
|
|
|
handleResult(new Error('Request timeout: ' + info.options.path), null);
|
|
|
|
});
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
req.on('error', function (err) {
|
|
|
|
// err has statusCode property
|
|
|
|
// res should have headers
|
|
|
|
handleResult(err, null);
|
|
|
|
});
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
if (data && typeof (data) === 'string') {
|
|
|
|
req.write(data, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data && typeof (data) !== 'string') {
|
|
|
|
data.on('close', function () {
|
2017-01-09 01:21:28 +03:00
|
|
|
req.end();
|
2018-01-18 01:30:25 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
data.pipe(req);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
req.end();
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
|
2018-10-31 22:23:51 +03:00
|
|
|
private _prepareRequest(method: string, requestUrl: string, headers: ifm.IHeaders): ifm.IRequestInfo {
|
2018-01-18 01:30:25 +03:00
|
|
|
const info: ifm.IRequestInfo = <ifm.IRequestInfo>{};
|
2017-01-09 01:21:28 +03:00
|
|
|
|
|
|
|
info.parsedUrl = url.parse(requestUrl);
|
2018-01-18 01:30:25 +03:00
|
|
|
const usingSsl: boolean = info.parsedUrl.protocol === 'https:';
|
2017-01-09 01:21:28 +03:00
|
|
|
info.httpModule = usingSsl ? https : http;
|
2018-01-18 01:30:25 +03:00
|
|
|
const defaultPort: number = usingSsl ? 443 : 80;
|
2017-12-06 00:34:57 +03:00
|
|
|
info.options = <http.RequestOptions>{};
|
|
|
|
info.options.host = info.parsedUrl.hostname;
|
|
|
|
info.options.port = info.parsedUrl.port ? parseInt(info.parsedUrl.port) : defaultPort;
|
|
|
|
info.options.path = (info.parsedUrl.pathname || '') + (info.parsedUrl.search || '');
|
|
|
|
info.options.method = method;
|
2018-10-31 22:23:51 +03:00
|
|
|
info.options.headers = this._mergeHeaders(headers);
|
2018-11-01 06:07:54 +03:00
|
|
|
info.options.headers["user-agent"] = this.userAgent;
|
2017-12-06 00:34:57 +03:00
|
|
|
info.options.agent = this._getAgent(requestUrl);
|
2017-01-09 01:21:28 +03:00
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
// gives handlers an opportunity to participate
|
|
|
|
if (this.handlers) {
|
|
|
|
this.handlers.forEach((handler) => {
|
|
|
|
handler.prepareRequest(info.options);
|
|
|
|
});
|
|
|
|
}
|
2017-07-19 00:58:14 +03:00
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
return info;
|
|
|
|
}
|
2017-11-18 15:07:22 +03:00
|
|
|
|
2018-10-31 22:23:51 +03:00
|
|
|
private _mergeHeaders(headers: ifm.IHeaders) : ifm.IHeaders {
|
2018-11-01 06:07:54 +03:00
|
|
|
const lowercaseKeys = obj => Object.keys(obj).reduce((c, k) => (c[k.toLowerCase()] = obj[k], c), {});
|
|
|
|
|
2018-10-31 22:23:51 +03:00
|
|
|
if (this.requestOptions && this.requestOptions.headers) {
|
2018-11-01 06:07:54 +03:00
|
|
|
return Object.assign(
|
|
|
|
{},
|
|
|
|
lowercaseKeys(this.requestOptions.headers),
|
|
|
|
lowercaseKeys(headers)
|
|
|
|
);
|
2018-10-31 22:23:51 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 06:07:54 +03:00
|
|
|
return lowercaseKeys(headers || {});
|
2018-10-31 22:23:51 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
private _getAgent(requestUrl: string) {
|
|
|
|
let agent;
|
|
|
|
let proxy = this._getProxy(requestUrl);
|
|
|
|
let useProxy = proxy.proxyUrl && proxy.proxyUrl.hostname && !this._isBypassProxy(requestUrl);
|
|
|
|
|
|
|
|
if (this._keepAlive && useProxy) {
|
|
|
|
agent = this._proxyAgent;
|
2017-07-19 00:58:14 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
if (this._keepAlive && !useProxy) {
|
|
|
|
agent = this._agent;
|
|
|
|
}
|
2017-07-19 00:58:14 +03:00
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
// if agent is already assigned use that agent.
|
|
|
|
if (!!agent) {
|
|
|
|
return agent;
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
let parsedUrl = url.parse(requestUrl);
|
|
|
|
const usingSsl = parsedUrl.protocol === 'https:';
|
2017-12-06 00:34:57 +03:00
|
|
|
let maxSockets = 100;
|
|
|
|
if (!!this.requestOptions) {
|
|
|
|
maxSockets = this.requestOptions.maxSockets || http.globalAgent.maxSockets
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
|
|
|
|
if (useProxy) {
|
2018-01-18 01:30:25 +03:00
|
|
|
const agentOptions: tunnel.TunnelOptions = {
|
2017-12-06 00:34:57 +03:00
|
|
|
maxSockets: maxSockets,
|
|
|
|
keepAlive: this._keepAlive,
|
2017-01-09 01:21:28 +03:00
|
|
|
proxy: {
|
2017-12-06 00:34:57 +03:00
|
|
|
proxyAuth: proxy.proxyAuth,
|
|
|
|
host: proxy.proxyUrl.hostname,
|
|
|
|
port: proxy.proxyUrl.port
|
2017-07-19 00:58:14 +03:00
|
|
|
},
|
2017-01-09 01:21:28 +03:00
|
|
|
};
|
|
|
|
|
2018-01-18 01:30:25 +03:00
|
|
|
let tunnelAgent: Function;
|
|
|
|
const overHttps = proxy.proxyUrl.protocol === 'https:';
|
2017-01-09 01:21:28 +03:00
|
|
|
if (usingSsl) {
|
|
|
|
tunnelAgent = overHttps ? tunnel.httpsOverHttps : tunnel.httpsOverHttp;
|
|
|
|
} else {
|
|
|
|
tunnelAgent = overHttps ? tunnel.httpOverHttps : tunnel.httpOverHttp;
|
|
|
|
}
|
2017-07-19 00:58:14 +03:00
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
agent = tunnelAgent(agentOptions);
|
|
|
|
this._proxyAgent = agent;
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
// if reusing agent across request and tunneling agent isn't assigned create a new agent
|
|
|
|
if (this._keepAlive && !agent) {
|
2018-01-18 01:30:25 +03:00
|
|
|
const options = { keepAlive: this._keepAlive, maxSockets: maxSockets };
|
2017-12-06 00:34:57 +03:00
|
|
|
agent = usingSsl ? new https.Agent(options) : new http.Agent(options);
|
|
|
|
this._agent = agent;
|
|
|
|
}
|
2018-01-18 01:30:25 +03:00
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
// if not using private agent and tunnel agent isn't setup then use global agent
|
2018-01-18 01:30:25 +03:00
|
|
|
if (!agent) {
|
2017-12-06 00:34:57 +03:00
|
|
|
agent = usingSsl ? https.globalAgent : http.globalAgent;
|
|
|
|
}
|
2017-07-19 00:58:14 +03:00
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
if (usingSsl && this._ignoreSslError) {
|
2017-07-19 00:58:14 +03:00
|
|
|
// we don't want to set NODE_TLS_REJECT_UNAUTHORIZED=0 since that will affect request for entire process
|
|
|
|
// http.RequestOptions doesn't expose a way to modify RequestOptions.agent.options
|
|
|
|
// we have to cast it to any and change it directly
|
|
|
|
agent.options = Object.assign(agent.options || {}, { rejectUnauthorized: false });
|
|
|
|
}
|
|
|
|
|
2017-08-29 17:34:32 +03:00
|
|
|
if (usingSsl && this._certConfig) {
|
|
|
|
agent.options = Object.assign(agent.options || {}, { ca: this._ca, cert: this._cert, key: this._key, passphrase: this._certConfig.passphrase });
|
|
|
|
}
|
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
return agent;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getProxy(requestUrl) {
|
2018-01-18 01:30:25 +03:00
|
|
|
const parsedUrl = url.parse(requestUrl);
|
2017-12-06 00:34:57 +03:00
|
|
|
let usingSsl = parsedUrl.protocol === 'https:';
|
|
|
|
let proxyConfig: ifm.IProxyConfiguration = this._httpProxy;
|
|
|
|
|
|
|
|
// fallback to http_proxy and https_proxy env
|
|
|
|
let https_proxy: string = process.env[EnvironmentVariables.HTTPS_PROXY];
|
|
|
|
let http_proxy: string = process.env[EnvironmentVariables.HTTP_PROXY];
|
|
|
|
|
|
|
|
if (!proxyConfig) {
|
|
|
|
if (https_proxy && usingSsl) {
|
|
|
|
proxyConfig = {
|
|
|
|
proxyUrl: https_proxy
|
|
|
|
};
|
|
|
|
} else if (http_proxy) {
|
|
|
|
proxyConfig = {
|
|
|
|
proxyUrl: http_proxy
|
|
|
|
};
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 00:34:57 +03:00
|
|
|
let proxyUrl: url.Url;
|
|
|
|
let proxyAuth: string;
|
|
|
|
if (proxyConfig) {
|
|
|
|
if (proxyConfig.proxyUrl.length > 0) {
|
|
|
|
proxyUrl = url.parse(proxyConfig.proxyUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proxyConfig.proxyUsername || proxyConfig.proxyPassword) {
|
2018-01-02 20:46:24 +03:00
|
|
|
proxyAuth = proxyConfig.proxyUsername + ":" + proxyConfig.proxyPassword;
|
2017-12-06 00:34:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { proxyUrl: proxyUrl, proxyAuth: proxyAuth };
|
2017-07-19 00:58:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private _isBypassProxy(requestUrl: string): Boolean {
|
|
|
|
if (!this._httpProxyBypassHosts) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let bypass: boolean = false;
|
|
|
|
this._httpProxyBypassHosts.forEach(bypassHost => {
|
|
|
|
if (bypassHost.test(requestUrl)) {
|
|
|
|
bypass = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return bypass;
|
|
|
|
}
|
2017-01-09 01:21:28 +03:00
|
|
|
}
|