-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
httpclient.ts
437 lines (376 loc) · 12.7 KB
/
httpclient.ts
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import { Event as SentryEvent, EventProcessor, Hub, Integration } from '@sentry/types';
import { addExceptionMechanism, fill, GLOBAL_OBJ, logger, supportsNativeFetch } from '@sentry/utils';
export type HttpStatusCodeRange = [number, number] | number;
export type HttpRequestTarget = string | RegExp;
interface HttpClientOptions {
/**
* HTTP status codes that should be considered failed.
* This array can contain tuples of `[begin, end]` (both inclusive),
* single status codes, or a combinations of both
*
* Example: [[500, 505], 507]
* Default: [[500, 599]]
*/
failedRequestStatusCodes?: HttpStatusCodeRange[];
/**
* Targets to track for failed requests.
* This array can contain strings or regular expressions.
*
* Example: ['http://localhost', /api\/.*\/]
* Default: [/.*\/]
*/
failedRequestTargets?: HttpRequestTarget[];
}
/** HTTPClient integration creates events for failed client side HTTP requests. */
export class HttpClient implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'HttpClient';
/**
* @inheritDoc
*/
public name: string = HttpClient.id;
private readonly _options: HttpClientOptions;
/**
* Returns current hub.
*/
private _getCurrentHub?: () => Hub;
/**
* @inheritDoc
*
* @param options
*/
public constructor(options?: Partial<HttpClientOptions>) {
this._options = {
failedRequestStatusCodes: [[500, 599]],
failedRequestTargets: [/.*/],
...options,
};
}
/**
* @inheritDoc
*
* @param options
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
this._getCurrentHub = getCurrentHub;
this._wrapFetch();
this._wrapXHR();
}
/**
* Interceptor function for fetch requests
*
* @param requestInfo The Fetch API request info
* @param response The Fetch API response
* @param requestInit The request init object
*/
private _fetchResponseHandler(requestInfo: RequestInfo, response: Response, requestInit?: RequestInit): void {
if (this._getCurrentHub && this._shouldCaptureResponse(response.status, response.url)) {
const request = new Request(requestInfo, requestInit);
let requestHeaders, responseHeaders, requestCookies, responseCookies;
if (this._getCurrentHub().shouldSendDefaultPii()) {
[{ headers: requestHeaders, cookies: requestCookies }, { headers: responseHeaders, cookies: responseCookies }] =
[
{ cookieHeader: 'Cookie', obj: request },
{ cookieHeader: 'Set-Cookie', obj: response },
].map(({ cookieHeader, obj }) => {
const headers = this._extractFetchHeaders(obj.headers);
let cookies;
try {
const cookieString = headers[cookieHeader] || headers[cookieHeader.toLowerCase()] || undefined;
if (cookieString) {
cookies = this._parseCookieString(cookieString);
}
} catch (e) {
__DEBUG_BUILD__ && logger.log(`Could not extract cookies from header ${cookieHeader}`);
}
return {
headers,
cookies,
};
});
}
const event = this._createEvent({
url: request.url,
method: request.method,
status: response.status,
requestHeaders,
responseHeaders,
requestCookies,
responseCookies,
});
this._getCurrentHub().captureEvent(event);
}
}
/**
* Interceptor function for XHR requests
*
* @param xhr The XHR request
* @param method The HTTP method
* @param headers The HTTP headers
*/
private _xhrResponseHandler(xhr: XMLHttpRequest, method: string, headers: Record<string, string>): void {
if (this._getCurrentHub && this._shouldCaptureResponse(xhr.status, xhr.responseURL)) {
let requestHeaders, responseCookies, responseHeaders;
if (this._getCurrentHub().shouldSendDefaultPii()) {
try {
const cookieString = xhr.getResponseHeader('Set-Cookie') || xhr.getResponseHeader('set-cookie') || undefined;
if (cookieString) {
responseCookies = this._parseCookieString(cookieString);
}
} catch (e) {
__DEBUG_BUILD__ && logger.log('Could not extract cookies from response headers');
}
try {
responseHeaders = this._getXHRResponseHeaders(xhr);
} catch (e) {
__DEBUG_BUILD__ && logger.log('Could not extract headers from response');
}
requestHeaders = headers;
}
const event = this._createEvent({
url: xhr.responseURL,
method: method,
status: xhr.status,
requestHeaders,
// Can't access request cookies from XHR
responseHeaders,
responseCookies,
});
this._getCurrentHub().captureEvent(event);
}
}
/**
* Extracts response size from `Content-Length` header when possible
*
* @param headers
* @returns The response size in bytes or undefined
*/
private _getResponseSizeFromHeaders(headers?: Record<string, string>): number | undefined {
if (headers) {
const contentLength = headers['Content-Length'] || headers['content-length'];
if (contentLength) {
return parseInt(contentLength, 10);
}
}
return undefined;
}
/**
* Creates an object containing cookies from the given cookie string
*
* @param cookieString The cookie string to parse
* @returns The parsed cookies
*/
private _parseCookieString(cookieString: string): Record<string, string> {
return cookieString.split('; ').reduce((acc: Record<string, string>, cookie: string) => {
const [key, value] = cookie.split('=');
acc[key] = value;
return acc;
}, {});
}
/**
* Extracts the headers as an object from the given Fetch API request or response object
*
* @param headers The headers to extract
* @returns The extracted headers as an object
*/
private _extractFetchHeaders(headers: Headers): Record<string, string> {
const result: Record<string, string> = {};
headers.forEach((value, key) => {
result[key] = value;
});
return result;
}
/**
* Extracts the response headers as an object from the given XHR object
*
* @param xhr The XHR object to extract the response headers from
* @returns The response headers as an object
*/
private _getXHRResponseHeaders(xhr: XMLHttpRequest): Record<string, string> {
const headers = xhr.getAllResponseHeaders();
if (!headers) {
return {};
}
return headers.split('\r\n').reduce((acc: Record<string, string>, line: string) => {
const [key, value] = line.split(': ');
acc[key] = value;
return acc;
}, {});
}
/**
* Checks if the given target url is in the given list of targets
*
* @param target The target url to check
* @returns true if the target url is in the given list of targets, false otherwise
*/
private _isInGivenRequestTargets(target: string): boolean {
if (!this._options.failedRequestTargets) {
return false;
}
return this._options.failedRequestTargets.some((givenRequestTarget: HttpRequestTarget) => {
if (typeof givenRequestTarget === 'string') {
return target.includes(givenRequestTarget);
}
return givenRequestTarget.test(target);
});
}
/**
* Checks if the given status code is in the given range
*
* @param status The status code to check
* @returns true if the status code is in the given range, false otherwise
*/
private _isInGivenStatusRanges(status: number): boolean {
if (!this._options.failedRequestStatusCodes) {
return false;
}
return this._options.failedRequestStatusCodes.some((range: HttpStatusCodeRange) => {
if (typeof range === 'number') {
return range === status;
}
return status >= range[0] && status <= range[1];
});
}
/**
* Wraps `fetch` function to capture request and response data
*/
private _wrapFetch(): void {
if (!supportsNativeFetch()) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
fill(GLOBAL_OBJ, 'fetch', function (originalFetch: (...args: unknown[]) => Promise<Response>) {
return function (this: Window, ...args: unknown[]): Promise<Response> {
const [requestInfo, requestInit] = args as [RequestInfo, RequestInit | undefined];
const responsePromise: Promise<Response> = originalFetch.apply(this, args);
responsePromise
.then((response: Response) => {
self._fetchResponseHandler(requestInfo, response, requestInit);
return response;
})
.catch((error: Error) => {
throw error;
});
return responsePromise;
};
});
}
/**
* Wraps XMLHttpRequest to capture request and response data
*/
private _wrapXHR(): void {
if (!('XMLHttpRequest' in GLOBAL_OBJ)) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
fill(XMLHttpRequest.prototype, 'open', function (originalOpen: (...openArgs: unknown[]) => void): () => void {
return function (this: XMLHttpRequest, ...openArgs: unknown[]): void {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const xhr = this;
const method = openArgs[0] as string;
const headers: Record<string, string> = {};
// Intercepting `setRequestHeader` to access the request headers of XHR instance.
// This will only work for user/library defined headers, not for the default/browser-assigned headers.
// Request cookies are also unavailable for XHR, as `Cookie` header can't be defined by `setRequestHeader`.
fill(
xhr,
'setRequestHeader',
// eslint-disable-next-line @typescript-eslint/ban-types
function (originalSetRequestHeader: (...setRequestHeaderArgs: unknown[]) => void): Function {
return function (...setRequestHeaderArgs: unknown[]): void {
const [header, value] = setRequestHeaderArgs as [string, string];
headers[header] = value;
return originalSetRequestHeader.apply(xhr, setRequestHeaderArgs);
};
},
);
// eslint-disable-next-line @typescript-eslint/ban-types
fill(xhr, 'onloadend', function (original?: (...onloadendArgs: unknown[]) => void): Function {
return function (...onloadendArgs: unknown[]): void {
try {
self._xhrResponseHandler(xhr, method, headers);
} catch (e) {
__DEBUG_BUILD__ && logger.warn('Error while extracting response event form XHR response', e);
}
if (original) {
return original.apply(xhr, onloadendArgs);
}
};
});
return originalOpen.apply(this, openArgs);
};
});
}
/**
* Checks whether given url points to Sentry server
*
* @param url url to verify
*/
private _isSentryRequest(url: string): boolean {
const client = this._getCurrentHub && this._getCurrentHub().getClient();
if (!client) {
return false;
}
const dsn = client.getDsn();
return dsn ? url.includes(dsn.host) : false;
}
/**
* Checks whether to capture given response as an event
*
* @param status response status code
* @param url response url
*/
private _shouldCaptureResponse(status: number, url: string): boolean {
return this._isInGivenStatusRanges(status) && this._isInGivenRequestTargets(url) && !this._isSentryRequest(url);
}
/**
* Creates a synthetic Sentry event from given response data
*
* @param data response data
* @returns event
*/
private _createEvent(data: {
url: string;
method: string;
status: number;
responseHeaders?: Record<string, string>;
responseCookies?: Record<string, string>;
requestHeaders?: Record<string, string>;
requestCookies?: Record<string, string>;
}): SentryEvent {
const message = `HTTP Client Error with status code: ${data.status}`;
const event: SentryEvent = {
message,
exception: {
values: [
{
type: 'Error',
value: message,
},
],
},
request: {
url: data.url,
method: data.method,
headers: data.requestHeaders,
cookies: data.requestCookies,
},
contexts: {
response: {
status_code: data.status,
headers: data.responseHeaders,
cookies: data.responseCookies,
body_size: this._getResponseSizeFromHeaders(data.responseHeaders),
},
},
};
addExceptionMechanism(event, {
type: 'http.client',
});
return event;
}
}