Repository navigation
Expand file tree
/
Copy pathhttpClient.ts
More file actions
347 lines (302 loc) · 14.3 KB
/
Copy pathhttpClient.ts
File metadata and controls
347 lines (302 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import * as fs from 'fs-extra';
import * as iconv from 'iconv-lite';
import * as path from 'path';
import { Cookie, CookieJar, Store } from 'tough-cookie';
import * as url from 'url';
import { Uri, window } from 'vscode';
import { RequestHeaders, ResponseHeaders } from '../models/base';
import { IRestClientSettings, SystemSettings } from '../models/configurationSettings';
import { HttpRequest } from '../models/httpRequest';
import { HttpResponse } from '../models/httpResponse';
import { awsSignature } from './auth/awsSignature';
import { digest } from './auth/digest';
import { MimeUtility } from './mimeUtility';
import { getHeader, hasHeader, removeHeader } from './misc';
import { convertBufferToStream, convertStreamToBuffer } from './streamUtility';
import { UserDataManager } from './userDataManager';
import { getCurrentHttpFileName, getWorkspaceRootPath } from './workspaceUtility';
import got = require('got');
const encodeUrl = require('encodeurl');
const cookieStore = require('tough-cookie-file-store-bugfix');
type SetCookieCallback = (err: Error | null, cookie: Cookie) => void;
type SetCookieCallbackWithoutOptions = (err: Error, cookie: Cookie) => void;
type GetCookieStringCallback = (err: Error | null, cookies: string) => void;
type Certificate = {
cert?: Buffer;
key?: Buffer;
pfx?: Buffer;
passphrase?: string;
};
export class HttpClient {
private readonly cookieStore: Store;
public constructor() {
const cookieFilePath = UserDataManager.cookieFilePath;
this.cookieStore = new cookieStore(cookieFilePath) as Store;
}
public async send(httpRequest: HttpRequest, settings?: IRestClientSettings): Promise<HttpResponse> {
settings = settings || SystemSettings.Instance;
const options = await this.prepareOptions(httpRequest, settings);
let bodySize = 0;
let headersSize = 0;
const requestUrl = encodeUrl(httpRequest.url);
const request: got.GotPromise<Buffer> = got(requestUrl, options);
httpRequest.setUnderlyingRequest(request);
(request as any).on('response', res => {
if (res.rawHeaders) {
headersSize += res.rawHeaders.map(h => h.length).reduce((a, b) => a + b, 0);
headersSize += (res.rawHeaders.length) / 2;
}
res.on('data', chunk => {
bodySize += chunk.length;
});
});
const response = await request;
const contentType = response.headers['content-type'];
let encoding: string | undefined;
if (contentType) {
encoding = MimeUtility.parse(contentType).charset;
}
if (!encoding) {
encoding = "utf8";
}
const bodyBuffer = response.body;
let bodyString = iconv.encodingExists(encoding) ? iconv.decode(bodyBuffer, encoding) : bodyBuffer.toString();
if (settings.decodeEscapedUnicodeCharacters) {
bodyString = this.decodeEscapedUnicodeCharacters(bodyString);
}
// adjust response header case, due to the response headers in nodejs http module is in lowercase
const responseHeaders: ResponseHeaders = HttpClient.normalizeHeaderNames(response.headers, response.rawHeaders);
const requestBody = options.body;
return new HttpResponse(
response.statusCode,
response.statusMessage,
response.httpVersion,
responseHeaders,
bodyString,
bodySize,
headersSize,
bodyBuffer,
response.timings.phases,
new HttpRequest(
options.method!,
requestUrl,
HttpClient.normalizeHeaderNames(
(response as any).request.gotOptions.headers as RequestHeaders,
Object.keys(httpRequest.headers)),
Buffer.isBuffer(requestBody) ? convertBufferToStream(requestBody) : requestBody,
httpRequest.rawBody,
httpRequest.name
));
}
private async prepareOptions(httpRequest: HttpRequest, settings: IRestClientSettings): Promise<got.GotBodyOptions<null>> {
const originalRequestBody = httpRequest.body;
let requestBody: string | Buffer | undefined;
if (originalRequestBody) {
if (typeof originalRequestBody !== 'string') {
requestBody = await convertStreamToBuffer(originalRequestBody);
} else {
requestBody = originalRequestBody;
}
}
// Fix #682 Do not touch original headers in httpRequest, which may be used for retry later
// Simply do a shadow copy here
const clonedHeaders = Object.assign({}, httpRequest.headers);
const options: got.GotBodyOptions<null> = {
headers: clonedHeaders,
method: httpRequest.method,
body: requestBody,
encoding: null,
decompress: true,
followRedirect: settings.followRedirect,
rejectUnauthorized: false,
throwHttpErrors: false,
cookieJar: settings.rememberCookiesForSubsequentRequests ? new CookieJar(this.cookieStore, { rejectPublicSuffixes: false }) : undefined,
retry: 0,
hooks: {
afterResponse: [],
// Following port reset on redirect can be removed after upgrade got to version 10.0
// https://github.com/sindresorhus/got/issues/719
beforeRedirect: [
opts => {
const redirectHost = ((opts as any).href as string).split('/')[2];
if (!redirectHost.includes(':')) {
delete opts.port;
}
}
],
beforeRequest: [],
}
};
if (settings.timeoutInMilliseconds > 0) {
options.timeout = settings.timeoutInMilliseconds;
}
// TODO: refactor auth
const authorization = getHeader(options.headers!, 'Authorization') as string | undefined;
if (authorization) {
const [scheme, user, ...args] = authorization.split(/\s+/);
const normalizedScheme = scheme.toLowerCase();
if (args.length > 0) {
const pass = args.join(' ');
if (normalizedScheme === 'basic') {
removeHeader(options.headers!, 'Authorization');
options.auth = `${user}:${pass}`;
} else if (normalizedScheme === 'digest') {
removeHeader(options.headers!, 'Authorization');
options.hooks!.afterResponse!.push(digest(user, pass));
} else if (normalizedScheme === 'aws') {
removeHeader(options.headers!, 'Authorization');
options.hooks!.beforeRequest!.push(awsSignature(authorization));
}
} else if (normalizedScheme === 'basic' && user.includes(':')) {
removeHeader(options.headers!, 'Authorization');
options.auth = user;
}
}
// set certificate
const certificate = this.getRequestCertificate(httpRequest.url, settings);
Object.assign(options, certificate);
// set proxy
if (settings.proxy && !HttpClient.ignoreProxy(httpRequest.url, settings.excludeHostsForProxy)) {
const proxyEndpoint = url.parse(settings.proxy);
if (/^https?:$/.test(proxyEndpoint.protocol || '')) {
const proxyOptions = {
host: proxyEndpoint.hostname,
port: Number(proxyEndpoint.port),
rejectUnauthorized: settings.proxyStrictSSL
};
const ctor = (httpRequest.url.startsWith('http:')
? await import('http-proxy-agent')
: await import('https-proxy-agent')).default;
options.agent = new ctor(proxyOptions);
}
}
// set cookie jar
if (options.cookieJar) {
const { getCookieString: originalGetCookieString, setCookie: originalSetCookie } = options.cookieJar;
function _setCookie(cookieOrString: Cookie | string, currentUrl: string, opts: CookieJar.SetCookieOptions, cb: SetCookieCallback): void;
function _setCookie(cookieOrString: Cookie | string, currentUrl: string, cb: SetCookieCallbackWithoutOptions): void;
function _setCookie(cookieOrString: Cookie | string, currentUrl: string, opts: CookieJar.SetCookieOptions | SetCookieCallbackWithoutOptions, cb?: SetCookieCallback): void {
if (opts instanceof Function) {
cb = opts;
opts = {};
}
opts.ignoreError = true;
originalSetCookie.call(options.cookieJar, cookieOrString, currentUrl, opts, cb!);
}
options.cookieJar.setCookie = _setCookie;
if (hasHeader(options.headers!, 'cookie')) {
let count = 0;
function _getCookieString(currentUrl: string, opts: CookieJar.GetCookiesOptions, cb: GetCookieStringCallback): void;
function _getCookieString(currentUrl: string, cb: GetCookieStringCallback): void;
function _getCookieString(currentUrl: string, opts: CookieJar.GetCookiesOptions | GetCookieStringCallback, cb?: GetCookieStringCallback): void {
if (opts instanceof Function) {
cb = opts;
opts = {};
}
originalGetCookieString.call(options.cookieJar, currentUrl, opts, (err, cookies) => {
if (err || count > 0 || !cookies) {
cb!(err, cookies);
}
count++;
cb!(null, [cookies, getHeader(options.headers!, 'cookie')].filter(Boolean).join('; '));
});
}
options.cookieJar.getCookieString = _getCookieString;
}
}
return options;
}
private decodeEscapedUnicodeCharacters(body: string): string {
return body.replace(/\\u([0-9a-fA-F]{4})/gi, (_, g) => {
const char = String.fromCharCode(parseInt(g, 16));
return char === '"' ? '\\"' : char;
});
}
private getRequestCertificate(requestUrl: string, settings: IRestClientSettings): Certificate | null {
const host = url.parse(requestUrl).host;
if (!host || !(host in settings.hostCertificates)) {
return null;
}
const { cert: certPath, key: keyPath, pfx: pfxPath, passphrase } = settings.hostCertificates[host];
const cert = this.resolveCertificate(certPath);
const key = this.resolveCertificate(keyPath);
const pfx = this.resolveCertificate(pfxPath);
return { cert, key, pfx, passphrase };
}
private static ignoreProxy(requestUrl: string, excludeHostsForProxy: string[]): Boolean {
if (!excludeHostsForProxy || excludeHostsForProxy.length === 0) {
return false;
}
const resolvedUrl = url.parse(requestUrl);
const hostName = resolvedUrl.hostname?.toLowerCase();
const port = resolvedUrl.port;
const excludeHostsProxyList = Array.from(new Set(excludeHostsForProxy.map(eh => eh.toLowerCase())));
for (const eh of excludeHostsProxyList) {
const urlParts = eh.split(":");
if (!port) {
// if no port specified in request url, host name must exactly match
if (urlParts.length === 1 && urlParts[0] === hostName) {
return true;
}
} else {
// if port specified, match host without port or hostname:port exactly match
const [ph, pp] = urlParts;
if (ph === hostName && (!pp || pp === port)) {
return true;
}
}
}
return false;
}
private resolveCertificate(absoluteOrRelativePath: string | undefined): Buffer | undefined {
if (absoluteOrRelativePath === undefined) {
return undefined;
}
if (path.isAbsolute(absoluteOrRelativePath)) {
if (!fs.existsSync(absoluteOrRelativePath)) {
window.showWarningMessage(`Certificate path ${absoluteOrRelativePath} doesn't exist, please make sure it exists.`);
return undefined;
} else {
return fs.readFileSync(absoluteOrRelativePath);
}
}
// the path should be relative path
const rootPath = getWorkspaceRootPath();
let absolutePath = '';
if (rootPath) {
absolutePath = path.join(Uri.parse(rootPath).fsPath, absoluteOrRelativePath);
if (fs.existsSync(absolutePath)) {
return fs.readFileSync(absolutePath);
} else {
window.showWarningMessage(`Certificate path ${absoluteOrRelativePath} doesn't exist, please make sure it exists.`);
return undefined;
}
}
const currentFilePath = getCurrentHttpFileName();
if (!currentFilePath) {
return undefined;
}
absolutePath = path.join(path.dirname(currentFilePath), absoluteOrRelativePath);
if (fs.existsSync(absolutePath)) {
return fs.readFileSync(absolutePath);
} else {
window.showWarningMessage(`Certificate path ${absoluteOrRelativePath} doesn't exist, please make sure it exists.`);
return undefined;
}
}
private static normalizeHeaderNames<T extends RequestHeaders | ResponseHeaders>(headers: T, rawHeaders: string[]): T {
const headersDic: { [key: string]: string } = rawHeaders.reduce(
(prev, cur) => {
if (!(cur.toLowerCase() in prev)) {
prev[cur.toLowerCase()] = cur;
}
return prev;
}, {});
const adjustedResponseHeaders = {} as RequestHeaders | ResponseHeaders;
for (const header in headers) {
const adjustedHeaderName = headersDic[header] || header;
adjustedResponseHeaders[adjustedHeaderName] = headers[header];
}
return adjustedResponseHeaders as T;
}
}