1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const EventEmitter = require('events');
|
17 | const {helper, debugError} = require('./helper');
|
18 | const Multimap = require('./Multimap');
|
19 |
|
20 | class NetworkManager extends EventEmitter {
|
21 | |
22 |
|
23 |
|
24 |
|
25 | constructor(client, frameManager) {
|
26 | super();
|
27 | this._client = client;
|
28 | this._frameManager = frameManager;
|
29 |
|
30 | this._requestIdToRequest = new Map();
|
31 |
|
32 | this._interceptionIdToRequest = new Map();
|
33 |
|
34 | this._extraHTTPHeaders = {};
|
35 |
|
36 | this._offline = false;
|
37 |
|
38 |
|
39 | this._credentials = null;
|
40 |
|
41 | this._attemptedAuthentications = new Set();
|
42 | this._userRequestInterceptionEnabled = false;
|
43 | this._protocolRequestInterceptionEnabled = false;
|
44 |
|
45 | this._requestHashToRequestIds = new Multimap();
|
46 |
|
47 | this._requestHashToInterceptionIds = new Multimap();
|
48 |
|
49 | this._client.on('Network.requestWillBeSent', this._onRequestWillBeSent.bind(this));
|
50 | this._client.on('Network.requestIntercepted', this._onRequestIntercepted.bind(this));
|
51 | this._client.on('Network.responseReceived', this._onResponseReceived.bind(this));
|
52 | this._client.on('Network.loadingFinished', this._onLoadingFinished.bind(this));
|
53 | this._client.on('Network.loadingFailed', this._onLoadingFailed.bind(this));
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 | async authenticate(credentials) {
|
60 | this._credentials = credentials;
|
61 | await this._updateProtocolRequestInterception();
|
62 | }
|
63 |
|
64 | |
65 |
|
66 |
|
67 | async setExtraHTTPHeaders(extraHTTPHeaders) {
|
68 | this._extraHTTPHeaders = {};
|
69 | for (const key of Object.keys(extraHTTPHeaders)) {
|
70 | const value = extraHTTPHeaders[key];
|
71 | console.assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
|
72 | this._extraHTTPHeaders[key.toLowerCase()] = value;
|
73 | }
|
74 | await this._client.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 | extraHTTPHeaders() {
|
81 | return Object.assign({}, this._extraHTTPHeaders);
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 | async setOfflineMode(value) {
|
88 | if (this._offline === value)
|
89 | return;
|
90 | this._offline = value;
|
91 | await this._client.send('Network.emulateNetworkConditions', {
|
92 | offline: this._offline,
|
93 |
|
94 | latency: 0,
|
95 | downloadThroughput: -1,
|
96 | uploadThroughput: -1
|
97 | });
|
98 | }
|
99 |
|
100 | |
101 |
|
102 |
|
103 | async setUserAgent(userAgent) {
|
104 | await this._client.send('Network.setUserAgentOverride', { userAgent });
|
105 | }
|
106 |
|
107 | |
108 |
|
109 |
|
110 | async setRequestInterception(value) {
|
111 | this._userRequestInterceptionEnabled = value;
|
112 | await this._updateProtocolRequestInterception();
|
113 | }
|
114 |
|
115 | async _updateProtocolRequestInterception() {
|
116 | const enabled = this._userRequestInterceptionEnabled || !!this._credentials;
|
117 | if (enabled === this._protocolRequestInterceptionEnabled)
|
118 | return;
|
119 | this._protocolRequestInterceptionEnabled = enabled;
|
120 | const patterns = enabled ? [{urlPattern: '*'}] : [];
|
121 | await Promise.all([
|
122 | this._client.send('Network.setCacheDisabled', {cacheDisabled: enabled}),
|
123 | this._client.send('Network.setRequestInterception', {patterns})
|
124 | ]);
|
125 | }
|
126 |
|
127 | |
128 |
|
129 |
|
130 | _onRequestIntercepted(event) {
|
131 | if (event.authChallenge) {
|
132 | let response = 'Default';
|
133 | if (this._attemptedAuthentications.has(event.interceptionId)) {
|
134 | response = 'CancelAuth';
|
135 | } else if (this._credentials) {
|
136 | response = 'ProvideCredentials';
|
137 | this._attemptedAuthentications.add(event.interceptionId);
|
138 | }
|
139 | const {username, password} = this._credentials || {username: undefined, password: undefined};
|
140 | this._client.send('Network.continueInterceptedRequest', {
|
141 | interceptionId: event.interceptionId,
|
142 | authChallengeResponse: { response, username, password }
|
143 | }).catch(debugError);
|
144 | return;
|
145 | }
|
146 | if (!this._userRequestInterceptionEnabled && this._protocolRequestInterceptionEnabled) {
|
147 | this._client.send('Network.continueInterceptedRequest', {
|
148 | interceptionId: event.interceptionId
|
149 | }).catch(debugError);
|
150 | }
|
151 |
|
152 | if (event.redirectUrl) {
|
153 | const request = this._interceptionIdToRequest.get(event.interceptionId);
|
154 | if (request) {
|
155 | this._handleRequestRedirect(request, event.responseStatusCode, event.responseHeaders);
|
156 | this._handleRequestStart(request._requestId, event.interceptionId, event.redirectUrl, event.resourceType, event.request, event.frameId);
|
157 | }
|
158 | return;
|
159 | }
|
160 | const requestHash = generateRequestHash(event.request);
|
161 | const requestId = this._requestHashToRequestIds.firstValue(requestHash);
|
162 | if (requestId) {
|
163 | this._requestHashToRequestIds.delete(requestHash, requestId);
|
164 | this._handleRequestStart(requestId, event.interceptionId, event.request.url, event.resourceType, event.request, event.frameId);
|
165 | } else {
|
166 | this._requestHashToInterceptionIds.set(requestHash, event.interceptionId);
|
167 | this._handleRequestStart(null, event.interceptionId, event.request.url, event.resourceType, event.request, event.frameId);
|
168 | }
|
169 | }
|
170 |
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 | _handleRequestRedirect(request, redirectStatus, redirectHeaders) {
|
177 | const response = new Response(this._client, request, redirectStatus, redirectHeaders);
|
178 | request._response = response;
|
179 | this._requestIdToRequest.delete(request._requestId);
|
180 | this._interceptionIdToRequest.delete(request._interceptionId);
|
181 | this._attemptedAuthentications.delete(request._interceptionId);
|
182 | this.emit(NetworkManager.Events.Response, response);
|
183 | this.emit(NetworkManager.Events.RequestFinished, request);
|
184 | }
|
185 |
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | _handleRequestStart(requestId, interceptionId, url, resourceType, requestPayload, frameId) {
|
195 | let frame = null;
|
196 | if (frameId)
|
197 | frame = this._frameManager.frame(frameId);
|
198 | const request = new Request(this._client, requestId, interceptionId, this._userRequestInterceptionEnabled, url, resourceType, requestPayload, frame);
|
199 | if (requestId)
|
200 | this._requestIdToRequest.set(requestId, request);
|
201 | if (interceptionId)
|
202 | this._interceptionIdToRequest.set(interceptionId, request);
|
203 | this.emit(NetworkManager.Events.Request, request);
|
204 | }
|
205 |
|
206 | |
207 |
|
208 |
|
209 | _onRequestWillBeSent(event) {
|
210 | if (this._protocolRequestInterceptionEnabled) {
|
211 |
|
212 | if (event.redirectResponse)
|
213 | return;
|
214 | const requestHash = generateRequestHash(event.request);
|
215 | const interceptionId = this._requestHashToInterceptionIds.firstValue(requestHash);
|
216 | const request = interceptionId ? this._interceptionIdToRequest.get(interceptionId) : null;
|
217 | if (request) {
|
218 | request._requestId = event.requestId;
|
219 | this._requestIdToRequest.set(event.requestId, request);
|
220 | this._requestHashToInterceptionIds.delete(requestHash, interceptionId);
|
221 | } else {
|
222 | this._requestHashToRequestIds.set(requestHash, event.requestId);
|
223 | }
|
224 | return;
|
225 | }
|
226 | if (event.redirectResponse) {
|
227 | const request = this._requestIdToRequest.get(event.requestId);
|
228 |
|
229 | if (request)
|
230 | this._handleRequestRedirect(request, event.redirectResponse.status, event.redirectResponse.headers);
|
231 | }
|
232 | this._handleRequestStart(event.requestId, null, event.request.url, event.type, event.request, event.frameId);
|
233 | }
|
234 |
|
235 | |
236 |
|
237 |
|
238 | _onResponseReceived(event) {
|
239 | const request = this._requestIdToRequest.get(event.requestId);
|
240 |
|
241 | if (!request)
|
242 | return;
|
243 | const response = new Response(this._client, request, event.response.status, event.response.headers);
|
244 | request._response = response;
|
245 | this.emit(NetworkManager.Events.Response, response);
|
246 | }
|
247 |
|
248 | |
249 |
|
250 |
|
251 | _onLoadingFinished(event) {
|
252 | const request = this._requestIdToRequest.get(event.requestId);
|
253 |
|
254 |
|
255 | if (!request)
|
256 | return;
|
257 | request._completePromiseFulfill.call(null);
|
258 | this._requestIdToRequest.delete(request._requestId);
|
259 | this._interceptionIdToRequest.delete(request._interceptionId);
|
260 | this._attemptedAuthentications.delete(request._interceptionId);
|
261 | this.emit(NetworkManager.Events.RequestFinished, request);
|
262 | }
|
263 |
|
264 | |
265 |
|
266 |
|
267 | _onLoadingFailed(event) {
|
268 | const request = this._requestIdToRequest.get(event.requestId);
|
269 |
|
270 |
|
271 | if (!request)
|
272 | return;
|
273 | request._failureText = event.errorText;
|
274 | request._completePromiseFulfill.call(null);
|
275 | this._requestIdToRequest.delete(request._requestId);
|
276 | this._interceptionIdToRequest.delete(request._interceptionId);
|
277 | this._attemptedAuthentications.delete(request._interceptionId);
|
278 | this.emit(NetworkManager.Events.RequestFailed, request);
|
279 | }
|
280 | }
|
281 |
|
282 | class Request {
|
283 | |
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | constructor(client, requestId, interceptionId, allowInterception, url, resourceType, payload, frame) {
|
294 | this._client = client;
|
295 | this._requestId = requestId;
|
296 | this._interceptionId = interceptionId;
|
297 | this._allowInterception = allowInterception;
|
298 | this._interceptionHandled = false;
|
299 | this._response = null;
|
300 | this._failureText = null;
|
301 | this._completePromise = new Promise(fulfill => {
|
302 | this._completePromiseFulfill = fulfill;
|
303 | });
|
304 |
|
305 | this._url = url;
|
306 | this._resourceType = resourceType.toLowerCase();
|
307 | this._method = payload.method;
|
308 | this._postData = payload.postData;
|
309 | this._headers = {};
|
310 | this._frame = frame;
|
311 | for (const key of Object.keys(payload.headers))
|
312 | this._headers[key.toLowerCase()] = payload.headers[key];
|
313 | }
|
314 |
|
315 | |
316 |
|
317 |
|
318 | url() {
|
319 | return this._url;
|
320 | }
|
321 |
|
322 | |
323 |
|
324 |
|
325 | resourceType() {
|
326 | return this._resourceType;
|
327 | }
|
328 |
|
329 | |
330 |
|
331 |
|
332 | method() {
|
333 | return this._method;
|
334 | }
|
335 |
|
336 | |
337 |
|
338 |
|
339 | postData() {
|
340 | return this._postData;
|
341 | }
|
342 |
|
343 | |
344 |
|
345 |
|
346 | headers() {
|
347 | return this._headers;
|
348 | }
|
349 |
|
350 | |
351 |
|
352 |
|
353 | response() {
|
354 | return this._response;
|
355 | }
|
356 |
|
357 | |
358 |
|
359 |
|
360 | frame() {
|
361 | return this._frame;
|
362 | }
|
363 |
|
364 | |
365 |
|
366 |
|
367 | failure() {
|
368 | if (!this._failureText)
|
369 | return null;
|
370 | return {
|
371 | errorText: this._failureText
|
372 | };
|
373 | }
|
374 |
|
375 | |
376 |
|
377 |
|
378 | async continue(overrides = {}) {
|
379 | console.assert(this._allowInterception, 'Request Interception is not enabled!');
|
380 | console.assert(!this._interceptionHandled, 'Request is already handled!');
|
381 | this._interceptionHandled = true;
|
382 | await this._client.send('Network.continueInterceptedRequest', {
|
383 | interceptionId: this._interceptionId,
|
384 | url: overrides.url,
|
385 | method: overrides.method,
|
386 | postData: overrides.postData,
|
387 | headers: overrides.headers,
|
388 | }).catch(error => {
|
389 |
|
390 |
|
391 | debugError(error);
|
392 | });
|
393 | }
|
394 |
|
395 | |
396 |
|
397 |
|
398 | async respond(response) {
|
399 |
|
400 | if (this._url.startsWith('data:'))
|
401 | return;
|
402 | console.assert(this._allowInterception, 'Request Interception is not enabled!');
|
403 | console.assert(!this._interceptionHandled, 'Request is already handled!');
|
404 | this._interceptionHandled = true;
|
405 |
|
406 | const responseBody = response.body && helper.isString(response.body) ? Buffer.from((response.body)) : (response.body || null);
|
407 |
|
408 | const responseHeaders = {};
|
409 | if (response.headers) {
|
410 | for (const header of Object.keys(response.headers))
|
411 | responseHeaders[header.toLowerCase()] = response.headers[header];
|
412 | }
|
413 | if (response.contentType)
|
414 | responseHeaders['content-type'] = response.contentType;
|
415 | if (responseBody && !('content-length' in responseHeaders)) {
|
416 |
|
417 | responseHeaders['content-length'] = Buffer.byteLength(responseBody);
|
418 | }
|
419 |
|
420 | const statusCode = response.status || 200;
|
421 | const statusText = statusTexts[statusCode] || '';
|
422 | const statusLine = `HTTP/1.1 ${statusCode} ${statusText}`;
|
423 |
|
424 | const CRLF = '\r\n';
|
425 | let text = statusLine + CRLF;
|
426 | for (const header of Object.keys(responseHeaders))
|
427 | text += header + ': ' + responseHeaders[header] + CRLF;
|
428 | text += CRLF;
|
429 | let responseBuffer = Buffer.from(text, 'utf8');
|
430 | if (responseBody)
|
431 | responseBuffer = Buffer.concat([responseBuffer, responseBody]);
|
432 |
|
433 | await this._client.send('Network.continueInterceptedRequest', {
|
434 | interceptionId: this._interceptionId,
|
435 | rawResponse: responseBuffer.toString('base64')
|
436 | }).catch(error => {
|
437 |
|
438 |
|
439 | debugError(error);
|
440 | });
|
441 | }
|
442 |
|
443 | |
444 |
|
445 |
|
446 | async abort(errorCode = 'failed') {
|
447 | const errorReason = errorReasons[errorCode];
|
448 | console.assert(errorReason, 'Unknown error code: ' + errorCode);
|
449 | console.assert(this._allowInterception, 'Request Interception is not enabled!');
|
450 | console.assert(!this._interceptionHandled, 'Request is already handled!');
|
451 | this._interceptionHandled = true;
|
452 | await this._client.send('Network.continueInterceptedRequest', {
|
453 | interceptionId: this._interceptionId,
|
454 | errorReason
|
455 | }).catch(error => {
|
456 |
|
457 |
|
458 | debugError(error);
|
459 | });
|
460 | }
|
461 | }
|
462 |
|
463 | const errorReasons = {
|
464 | 'aborted': 'Aborted',
|
465 | 'accessdenied': 'AccessDenied',
|
466 | 'addressunreachable': 'AddressUnreachable',
|
467 | 'connectionaborted': 'ConnectionAborted',
|
468 | 'connectionclosed': 'ConnectionClosed',
|
469 | 'connectionfailed': 'ConnectionFailed',
|
470 | 'connectionrefused': 'ConnectionRefused',
|
471 | 'connectionreset': 'ConnectionReset',
|
472 | 'internetdisconnected': 'InternetDisconnected',
|
473 | 'namenotresolved': 'NameNotResolved',
|
474 | 'timedout': 'TimedOut',
|
475 | 'failed': 'Failed',
|
476 | };
|
477 |
|
478 | helper.tracePublicAPI(Request);
|
479 |
|
480 | class Response {
|
481 | |
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 | constructor(client, request, status, headers) {
|
488 | this._client = client;
|
489 | this._request = request;
|
490 | this._contentPromise = null;
|
491 |
|
492 | this._status = status;
|
493 | this._url = request.url();
|
494 | this._headers = {};
|
495 | for (const key of Object.keys(headers))
|
496 | this._headers[key.toLowerCase()] = headers[key];
|
497 | }
|
498 |
|
499 | |
500 |
|
501 |
|
502 | url() {
|
503 | return this._url;
|
504 | }
|
505 |
|
506 | |
507 |
|
508 |
|
509 | ok() {
|
510 | return this._status >= 200 && this._status <= 299;
|
511 | }
|
512 |
|
513 | |
514 |
|
515 |
|
516 | status() {
|
517 | return this._status;
|
518 | }
|
519 |
|
520 | |
521 |
|
522 |
|
523 | headers() {
|
524 | return this._headers;
|
525 | }
|
526 |
|
527 | |
528 |
|
529 |
|
530 | buffer() {
|
531 | if (!this._contentPromise) {
|
532 | this._contentPromise = this._request._completePromise.then(async() => {
|
533 | const response = await this._client.send('Network.getResponseBody', {
|
534 | requestId: this._request._requestId
|
535 | });
|
536 | return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
|
537 | });
|
538 | }
|
539 | return this._contentPromise;
|
540 | }
|
541 |
|
542 | |
543 |
|
544 |
|
545 | async text() {
|
546 | const content = await this.buffer();
|
547 | return content.toString('utf8');
|
548 | }
|
549 |
|
550 | |
551 |
|
552 |
|
553 | async json() {
|
554 | const content = await this.text();
|
555 | return JSON.parse(content);
|
556 | }
|
557 |
|
558 | |
559 |
|
560 |
|
561 | request() {
|
562 | return this._request;
|
563 | }
|
564 | }
|
565 | helper.tracePublicAPI(Response);
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 | function generateRequestHash(request) {
|
572 | let normalizedURL = request.url;
|
573 | try {
|
574 |
|
575 |
|
576 |
|
577 | normalizedURL = decodeURI(request.url);
|
578 | } catch (e) {
|
579 | }
|
580 | const hash = {
|
581 | url: normalizedURL,
|
582 | method: request.method,
|
583 | postData: request.postData,
|
584 | headers: {},
|
585 | };
|
586 |
|
587 | if (!normalizedURL.startsWith('data:')) {
|
588 | const headers = Object.keys(request.headers);
|
589 | headers.sort();
|
590 | for (let header of headers) {
|
591 | const headerValue = request.headers[header];
|
592 | header = header.toLowerCase();
|
593 | if (header === 'accept' || header === 'referer' || header === 'x-devtools-emulate-network-conditions-client-id')
|
594 | continue;
|
595 | hash.headers[header] = headerValue;
|
596 | }
|
597 | }
|
598 | return JSON.stringify(hash);
|
599 | }
|
600 |
|
601 | NetworkManager.Events = {
|
602 | Request: 'request',
|
603 | Response: 'response',
|
604 | RequestFailed: 'requestfailed',
|
605 | RequestFinished: 'requestfinished',
|
606 | };
|
607 |
|
608 | const statusTexts = {
|
609 | '100': 'Continue',
|
610 | '101': 'Switching Protocols',
|
611 | '102': 'Processing',
|
612 | '200': 'OK',
|
613 | '201': 'Created',
|
614 | '202': 'Accepted',
|
615 | '203': 'Non-Authoritative Information',
|
616 | '204': 'No Content',
|
617 | '206': 'Partial Content',
|
618 | '207': 'Multi-Status',
|
619 | '208': 'Already Reported',
|
620 | '209': 'IM Used',
|
621 | '300': 'Multiple Choices',
|
622 | '301': 'Moved Permanently',
|
623 | '302': 'Found',
|
624 | '303': 'See Other',
|
625 | '304': 'Not Modified',
|
626 | '305': 'Use Proxy',
|
627 | '306': 'Switch Proxy',
|
628 | '307': 'Temporary Redirect',
|
629 | '308': 'Permanent Redirect',
|
630 | '400': 'Bad Request',
|
631 | '401': 'Unauthorized',
|
632 | '402': 'Payment Required',
|
633 | '403': 'Forbidden',
|
634 | '404': 'Not Found',
|
635 | '405': 'Method Not Allowed',
|
636 | '406': 'Not Acceptable',
|
637 | '407': 'Proxy Authentication Required',
|
638 | '408': 'Request Timeout',
|
639 | '409': 'Conflict',
|
640 | '410': 'Gone',
|
641 | '411': 'Length Required',
|
642 | '412': 'Precondition Failed',
|
643 | '413': 'Payload Too Large',
|
644 | '414': 'URI Too Long',
|
645 | '415': 'Unsupported Media Type',
|
646 | '416': 'Range Not Satisfiable',
|
647 | '417': 'Expectation Failed',
|
648 | '418': 'I\'m a teapot',
|
649 | '421': 'Misdirected Request',
|
650 | '422': 'Unprocessable Entity',
|
651 | '423': 'Locked',
|
652 | '424': 'Failed Dependency',
|
653 | '426': 'Upgrade Required',
|
654 | '428': 'Precondition Required',
|
655 | '429': 'Too Many Requests',
|
656 | '431': 'Request Header Fields Too Large',
|
657 | '451': 'Unavailable For Legal Reasons',
|
658 | '500': 'Internal Server Error',
|
659 | '501': 'Not Implemented',
|
660 | '502': 'Bad Gateway',
|
661 | '503': 'Service Unavailable',
|
662 | '504': 'Gateway Timeout',
|
663 | '505': 'HTTP Version Not Supported',
|
664 | '506': 'Variant Also Negotiates',
|
665 | '507': 'Insufficient Storage',
|
666 | '508': 'Loop Detected',
|
667 | '510': 'Not Extended',
|
668 | '511': 'Network Authentication Required',
|
669 | };
|
670 |
|
671 | module.exports = NetworkManager;
|