playwright/src/server/chromium/crNetworkManager.ts

695 lines
30 KiB
TypeScript
Raw Normal View History

2019-12-19 16:53:24 -08:00
/**
* Copyright 2017 Google Inc. All rights reserved.
* Modifications copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CRSession } from './crConnection';
import { Page } from '../page';
import { helper } from '../helper';
import { eventsHelper, RegisteredListener } from '../../utils/eventsHelper';
2019-12-19 16:53:24 -08:00
import { Protocol } from './protocol';
import * as network from '../network';
import * as frames from '../frames';
import * as types from '../types';
import { CRPage } from './crPage';
import { assert, headersObjectToArray } from '../../utils/utils';
2019-12-19 16:53:24 -08:00
export class CRNetworkManager {
private _client: CRSession;
private _page: Page;
private _parentManager: CRNetworkManager | null;
2019-12-19 16:53:24 -08:00
private _requestIdToRequest = new Map<string, InterceptableRequest>();
private _requestIdToRequestWillBeSentEvent = new Map<string, Protocol.Network.requestWillBeSentPayload>();
private _credentials: {username: string, password: string} | null = null;
private _attemptedAuthentications = new Set<string>();
private _userRequestInterceptionEnabled = false;
2019-12-19 16:53:24 -08:00
private _protocolRequestInterceptionEnabled = false;
private _requestIdToRequestPausedEvent = new Map<string, Protocol.Fetch.requestPausedPayload>();
2019-12-19 16:53:24 -08:00
private _eventListeners: RegisteredListener[];
private _requestIdToExtraInfo = new Map<string, Protocol.Network.requestWillBeSentExtraInfoPayload>();
private _responseExtraInfoTracker = new ResponseExtraInfoTracker();
2019-12-19 16:53:24 -08:00
constructor(client: CRSession, page: Page, parentManager: CRNetworkManager | null) {
2019-12-19 16:53:24 -08:00
this._client = client;
this._page = page;
this._parentManager = parentManager;
this._eventListeners = this.instrumentNetworkEvents(client);
}
2019-12-19 16:53:24 -08:00
instrumentNetworkEvents(session: CRSession, workerFrame?: frames.Frame): RegisteredListener[] {
return [
eventsHelper.addEventListener(session, 'Fetch.requestPaused', this._onRequestPaused.bind(this, workerFrame)),
eventsHelper.addEventListener(session, 'Fetch.authRequired', this._onAuthRequired.bind(this)),
eventsHelper.addEventListener(session, 'Network.requestWillBeSent', this._onRequestWillBeSent.bind(this, workerFrame)),
eventsHelper.addEventListener(session, 'Network.requestWillBeSentExtraInfo', this._onRequestWillBeSentExtraInfo.bind(this)),
eventsHelper.addEventListener(session, 'Network.responseReceived', this._onResponseReceived.bind(this)),
eventsHelper.addEventListener(session, 'Network.responseReceivedExtraInfo', this._onResponseReceivedExtraInfo.bind(this)),
eventsHelper.addEventListener(session, 'Network.loadingFinished', this._onLoadingFinished.bind(this)),
eventsHelper.addEventListener(session, 'Network.loadingFailed', this._onLoadingFailed.bind(this)),
eventsHelper.addEventListener(session, 'Network.webSocketCreated', e => this._page._frameManager.onWebSocketCreated(e.requestId, e.url)),
eventsHelper.addEventListener(session, 'Network.webSocketWillSendHandshakeRequest', e => this._page._frameManager.onWebSocketRequest(e.requestId)),
eventsHelper.addEventListener(session, 'Network.webSocketHandshakeResponseReceived', e => this._page._frameManager.onWebSocketResponse(e.requestId, e.response.status, e.response.statusText)),
eventsHelper.addEventListener(session, 'Network.webSocketFrameSent', e => e.response.payloadData && this._page._frameManager.onWebSocketFrameSent(e.requestId, e.response.opcode, e.response.payloadData)),
eventsHelper.addEventListener(session, 'Network.webSocketFrameReceived', e => e.response.payloadData && this._page._frameManager.webSocketFrameReceived(e.requestId, e.response.opcode, e.response.payloadData)),
eventsHelper.addEventListener(session, 'Network.webSocketClosed', e => this._page._frameManager.webSocketClosed(e.requestId)),
eventsHelper.addEventListener(session, 'Network.webSocketFrameError', e => this._page._frameManager.webSocketError(e.requestId, e.errorMessage)),
2019-12-19 16:53:24 -08:00
];
}
async initialize() {
await this._client.send('Network.enable');
2019-12-19 16:53:24 -08:00
}
dispose() {
eventsHelper.removeEventListeners(this._eventListeners);
2019-12-19 16:53:24 -08:00
}
async authenticate(credentials: types.Credentials | null) {
2019-12-19 16:53:24 -08:00
this._credentials = credentials;
await this._updateProtocolRequestInterception();
}
async setOffline(offline: boolean) {
2019-12-19 16:53:24 -08:00
await this._client.send('Network.emulateNetworkConditions', {
offline,
2019-12-19 16:53:24 -08:00
// values of 0 remove any active throttling. crbug.com/456324#c9
latency: 0,
downloadThroughput: -1,
uploadThroughput: -1
});
}
async setRequestInterception(value: boolean) {
this._userRequestInterceptionEnabled = value;
2019-12-19 16:53:24 -08:00
await this._updateProtocolRequestInterception();
}
async _updateProtocolRequestInterception() {
const enabled = this._userRequestInterceptionEnabled || !!this._credentials;
2019-12-19 16:53:24 -08:00
if (enabled === this._protocolRequestInterceptionEnabled)
return;
this._protocolRequestInterceptionEnabled = enabled;
if (enabled) {
await Promise.all([
this._client.send('Network.setCacheDisabled', { cacheDisabled: true }),
2019-12-19 16:53:24 -08:00
this._client.send('Fetch.enable', {
handleAuthRequests: true,
2021-06-18 11:04:48 -07:00
patterns: [{urlPattern: '*', requestStage: 'Request'}, {urlPattern: '*', requestStage: 'Response'}],
2019-12-19 16:53:24 -08:00
}),
]);
} else {
await Promise.all([
this._client.send('Network.setCacheDisabled', { cacheDisabled: false }),
2019-12-19 16:53:24 -08:00
this._client.send('Fetch.disable')
]);
}
}
_onRequestWillBeSent(workerFrame: frames.Frame | undefined, event: Protocol.Network.requestWillBeSentPayload) {
this._responseExtraInfoTracker.requestWillBeSent(event);
2019-12-19 16:53:24 -08:00
// Request interception doesn't happen for data URLs with Network Service.
if (this._protocolRequestInterceptionEnabled && !event.request.url.startsWith('data:')) {
const requestId = event.requestId;
const requestPausedEvent = this._requestIdToRequestPausedEvent.get(requestId);
if (requestPausedEvent) {
this._onRequest(workerFrame, event, requestPausedEvent);
this._requestIdToRequestPausedEvent.delete(requestId);
2019-12-19 16:53:24 -08:00
} else {
this._requestIdToRequestWillBeSentEvent.set(event.requestId, event);
}
} else {
this._onRequest(workerFrame, event, null);
}
const extraInfo = this._requestIdToExtraInfo.get(event.requestId);
if (extraInfo)
this._onRequestWillBeSentExtraInfo(extraInfo);
}
_onRequestWillBeSentExtraInfo(event: Protocol.Network.requestWillBeSentExtraInfoPayload) {
const request = this._requestIdToRequest.get(event.requestId);
if (request) {
request.request.updateWithRawHeaders(headersObjectToArray(event.headers));
this._requestIdToExtraInfo.delete(event.requestId);
} else {
this._requestIdToExtraInfo.set(event.requestId, event);
2019-12-19 16:53:24 -08:00
}
}
_onAuthRequired(event: Protocol.Fetch.authRequiredPayload) {
2019-12-19 16:53:24 -08:00
let response: 'Default' | 'CancelAuth' | 'ProvideCredentials' = 'Default';
if (this._attemptedAuthentications.has(event.requestId)) {
response = 'CancelAuth';
} else if (this._credentials) {
response = 'ProvideCredentials';
this._attemptedAuthentications.add(event.requestId);
}
const {username, password} = this._credentials || {username: undefined, password: undefined};
this._client._sendMayFail('Fetch.continueWithAuth', {
2019-12-19 16:53:24 -08:00
requestId: event.requestId,
authChallengeResponse: { response, username, password },
});
2019-12-19 16:53:24 -08:00
}
_onRequestPaused(workerFrame: frames.Frame | undefined, event: Protocol.Fetch.requestPausedPayload) {
if (!this._userRequestInterceptionEnabled && this._protocolRequestInterceptionEnabled) {
this._client._sendMayFail('Fetch.continueRequest', {
2019-12-19 16:53:24 -08:00
requestId: event.requestId
});
2019-12-19 16:53:24 -08:00
}
if (!event.networkId) {
// Fetch without networkId means that request was not recongnized by inspector, and
// it will never receive Network.requestWillBeSent. Most likely, this is an internal request
// that we can safely fail.
this._client._sendMayFail('Fetch.failRequest', {
requestId: event.requestId,
errorReason: 'Aborted',
});
return;
}
if (event.request.url.startsWith('data:'))
return;
2019-12-19 16:53:24 -08:00
if (event.responseStatusCode || event.responseErrorReason) {
const isRedirect = event.responseStatusCode && event.responseStatusCode >= 300 && event.responseStatusCode < 400;
const request = this._requestIdToRequest.get(event.networkId!);
const route = request?._routeForRedirectChain();
if (isRedirect || !route || !route._interceptingResponse) {
2021-06-18 11:04:48 -07:00
this._client._sendMayFail('Fetch.continueRequest', {
requestId: event.requestId
});
return;
}
route._responseInterceptedCallback(event);
return;
2021-06-18 11:04:48 -07:00
}
2019-12-19 16:53:24 -08:00
const requestId = event.networkId;
const requestWillBeSentEvent = this._requestIdToRequestWillBeSentEvent.get(requestId);
if (requestWillBeSentEvent) {
this._onRequest(workerFrame, requestWillBeSentEvent, event);
2019-12-19 16:53:24 -08:00
this._requestIdToRequestWillBeSentEvent.delete(requestId);
} else {
this._requestIdToRequestPausedEvent.set(requestId, event);
2019-12-19 16:53:24 -08:00
}
}
_onRequest(workerFrame: frames.Frame | undefined, requestWillBeSentEvent: Protocol.Network.requestWillBeSentPayload, requestPausedEvent: Protocol.Fetch.requestPausedPayload | null) {
if (requestWillBeSentEvent.request.url.startsWith('data:'))
return;
let redirectedFrom: InterceptableRequest | null = null;
if (requestWillBeSentEvent.redirectResponse) {
const request = this._requestIdToRequest.get(requestWillBeSentEvent.requestId);
2019-12-19 16:53:24 -08:00
// If we connect late to the target, we could have missed the requestWillBeSent event.
if (request) {
this._handleRequestRedirect(request, requestWillBeSentEvent.redirectResponse, requestWillBeSentEvent.timestamp);
redirectedFrom = request;
2019-12-19 16:53:24 -08:00
}
}
let frame = requestWillBeSentEvent.frameId ? this._page._frameManager.frame(requestWillBeSentEvent.frameId) : workerFrame;
// Requests from workers lack frameId, because we receive Network.requestWillBeSent
// on the worker target. However, we receive Fetch.requestPaused on the page target,
// and lack workerFrame there. Luckily, Fetch.requestPaused provides a frameId.
if (!frame && requestPausedEvent && requestPausedEvent.frameId)
frame = this._page._frameManager.frame(requestPausedEvent.frameId);
// Check if it's main resource request interception (targetId === main frame id).
if (!frame && requestWillBeSentEvent.frameId === (this._page._delegate as CRPage)._targetId) {
// Main resource request for the page is being intercepted so the Frame is not created
// yet. Precreate it here for the purposes of request interception. It will be updated
// later as soon as the request continues and we receive frame tree from the page.
frame = this._page._frameManager.frameAttached(requestWillBeSentEvent.frameId, null);
}
// CORS options request is generated by the network stack. If interception is enabled,
// we accept all CORS options, assuming that this was intended when setting route.
//
// Note: it would be better to match the URL against interception patterns, but
// that information is only available to the client. Perhaps we can just route to the client?
if (requestPausedEvent && requestPausedEvent.request.method === 'OPTIONS' && this._page._needsRequestInterception()) {
const requestHeaders = requestPausedEvent.request.headers;
const responseHeaders: Protocol.Fetch.HeaderEntry[] = [
{ name: 'Access-Control-Allow-Origin', value: requestHeaders['Origin'] || '*' },
{ name: 'Access-Control-Allow-Methods', value: requestHeaders['Access-Control-Request-Method'] || 'GET, POST, OPTIONS, DELETE' },
{ name: 'Access-Control-Allow-Credentials', value: 'true' }
];
if (requestHeaders['Access-Control-Request-Headers'])
responseHeaders.push({ name: 'Access-Control-Allow-Headers', value: requestHeaders['Access-Control-Request-Headers'] });
this._client._sendMayFail('Fetch.fulfillRequest', {
requestId: requestPausedEvent.requestId,
responseCode: 204,
responsePhrase: network.STATUS_TEXTS['204'],
responseHeaders,
body: '',
});
return;
}
if (!frame) {
if (requestPausedEvent)
this._client._sendMayFail('Fetch.continueRequest', { requestId: requestPausedEvent.requestId });
return;
}
let route = null;
if (requestPausedEvent) {
// We do not support intercepting redirects.
if (redirectedFrom)
this._client._sendMayFail('Fetch.continueRequest', { requestId: requestPausedEvent.requestId });
else
route = new RouteImpl(this._client, requestPausedEvent.requestId);
}
const isNavigationRequest = requestWillBeSentEvent.requestId === requestWillBeSentEvent.loaderId && requestWillBeSentEvent.type === 'Document';
const documentId = isNavigationRequest ? requestWillBeSentEvent.loaderId : undefined;
const request = new InterceptableRequest({
frame,
documentId,
route,
requestWillBeSentEvent,
requestPausedEvent,
redirectedFrom
});
this._requestIdToRequest.set(requestWillBeSentEvent.requestId, request);
this._page._frameManager.requestStarted(request.request, route || undefined);
2019-12-19 16:53:24 -08:00
}
_createResponse(request: InterceptableRequest, responsePayload: Protocol.Network.Response): network.Response {
2019-12-19 16:53:24 -08:00
const getResponseBody = async () => {
const response = await this._client.send('Network.getResponseBody', { requestId: request._requestId });
2020-04-01 14:42:47 -07:00
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
2019-12-19 16:53:24 -08:00
};
const timingPayload = responsePayload.timing!;
let timing: network.ResourceTiming;
if (timingPayload) {
timing = {
startTime: (timingPayload.requestTime - request._timestamp + request._wallTime) * 1000,
domainLookupStart: timingPayload.dnsStart,
domainLookupEnd: timingPayload.dnsEnd,
connectStart: timingPayload.connectStart,
secureConnectionStart: timingPayload.sslStart,
connectEnd: timingPayload.connectEnd,
requestStart: timingPayload.sendStart,
responseStart: timingPayload.receiveHeadersEnd,
};
} else {
timing = {
startTime: request._wallTime * 1000,
domainLookupStart: -1,
domainLookupEnd: -1,
connectStart: -1,
secureConnectionStart: -1,
connectEnd: -1,
requestStart: -1,
responseStart: -1,
};
}
const response = new network.Response(request.request, responsePayload.status, responsePayload.statusText, headersObjectToArray(responsePayload.headers), timing, getResponseBody, responsePayload.protocol);
if (responsePayload?.remoteIPAddress && typeof responsePayload?.remotePort === 'number') {
response._serverAddrFinished({
ipAddress: responsePayload.remoteIPAddress,
port: responsePayload.remotePort,
});
} else {
response._serverAddrFinished();
}
response._securityDetailsFinished({
protocol: responsePayload?.securityDetails?.protocol,
subjectName: responsePayload?.securityDetails?.subjectName,
issuer: responsePayload?.securityDetails?.issuer,
validFrom: responsePayload?.securityDetails?.validFrom,
validTo: responsePayload?.securityDetails?.validTo,
});
this._responseExtraInfoTracker.processResponse(request._requestId, response, request.wasFulfilled());
return response;
2019-12-19 16:53:24 -08:00
}
_handleRequestRedirect(request: InterceptableRequest, responsePayload: Protocol.Network.Response, timestamp: number) {
2019-12-19 16:53:24 -08:00
const response = this._createResponse(request, responsePayload);
response._requestFinished((timestamp - request._timestamp) * 1000);
2019-12-19 16:53:24 -08:00
this._requestIdToRequest.delete(request._requestId);
if (request._interceptionId)
this._attemptedAuthentications.delete(request._interceptionId);
2019-12-19 16:53:24 -08:00
this._page._frameManager.requestReceivedResponse(response);
this._page._frameManager.reportRequestFinished(request.request, response);
}
_onResponseReceivedExtraInfo(event: Protocol.Network.responseReceivedExtraInfoPayload) {
this._responseExtraInfoTracker.responseReceivedExtraInfo(event);
2019-12-19 16:53:24 -08:00
}
_onResponseReceived(event: Protocol.Network.responseReceivedPayload) {
this._responseExtraInfoTracker.responseReceived(event);
2019-12-19 16:53:24 -08:00
const request = this._requestIdToRequest.get(event.requestId);
// FileUpload sends a response without a matching request.
if (!request)
return;
const response = this._createResponse(request, event.response);
this._page._frameManager.requestReceivedResponse(response);
}
_onLoadingFinished(event: Protocol.Network.loadingFinishedPayload) {
this._responseExtraInfoTracker.loadingFinished(event);
let request = this._requestIdToRequest.get(event.requestId);
if (!request)
request = this._maybeAdoptMainRequest(event.requestId);
2019-12-19 16:53:24 -08:00
// For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469
if (!request)
return;
// Under certain conditions we never get the Network.responseReceived
// event from protocol. @see https://crbug.com/883475
const response = request.request._existingResponse();
if (response) {
request.request._sizes.transferSize = event.encodedDataLength;
request.request._sizes.responseBodySize = event.encodedDataLength - response?.headersSize();
response._requestFinished(helper.secondsToRoundishMillis(event.timestamp - request._timestamp));
}
2019-12-19 16:53:24 -08:00
this._requestIdToRequest.delete(request._requestId);
if (request._interceptionId)
this._attemptedAuthentications.delete(request._interceptionId);
this._page._frameManager.reportRequestFinished(request.request, response);
2019-12-19 16:53:24 -08:00
}
_onLoadingFailed(event: Protocol.Network.loadingFailedPayload) {
this._responseExtraInfoTracker.loadingFailed(event);
let request = this._requestIdToRequest.get(event.requestId);
if (!request)
request = this._maybeAdoptMainRequest(event.requestId);
2019-12-19 16:53:24 -08:00
// For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469
if (!request)
return;
const response = request.request._existingResponse();
2019-12-19 16:53:24 -08:00
if (response)
response._requestFinished(helper.secondsToRoundishMillis(event.timestamp - request._timestamp));
2019-12-19 16:53:24 -08:00
this._requestIdToRequest.delete(request._requestId);
if (request._interceptionId)
this._attemptedAuthentications.delete(request._interceptionId);
2019-12-19 16:53:24 -08:00
request.request._setFailureText(event.errorText);
this._page._frameManager.requestFailed(request.request, !!event.canceled);
2019-12-19 16:53:24 -08:00
}
private _maybeAdoptMainRequest(requestId: Protocol.Network.RequestId): InterceptableRequest | undefined {
// OOPIF has a main request that starts in the parent session but finishes in the child session.
if (!this._parentManager)
return;
const request = this._parentManager._requestIdToRequest.get(requestId);
// Main requests have matching loaderId and requestId.
if (!request || request._documentId !== requestId)
return;
this._requestIdToRequest.set(requestId, request);
this._parentManager._requestIdToRequest.delete(requestId);
if (request._interceptionId && this._parentManager._attemptedAuthentications.has(request._interceptionId)) {
this._parentManager._attemptedAuthentications.delete(request._interceptionId);
this._attemptedAuthentications.add(request._interceptionId);
}
return request;
}
2019-12-19 16:53:24 -08:00
}
class InterceptableRequest {
2019-12-19 16:53:24 -08:00
readonly request: network.Request;
readonly _requestId: string;
readonly _interceptionId: string | null;
readonly _documentId: string | undefined;
readonly _timestamp: number;
readonly _wallTime: number;
private _route: RouteImpl | null;
private _redirectedFrom: InterceptableRequest | null;
2019-12-19 16:53:24 -08:00
constructor(options: {
frame: frames.Frame;
documentId?: string;
route: RouteImpl | null;
requestWillBeSentEvent: Protocol.Network.requestWillBeSentPayload;
requestPausedEvent: Protocol.Fetch.requestPausedPayload | null;
redirectedFrom: InterceptableRequest | null;
}) {
const { frame, documentId, route, requestWillBeSentEvent, requestPausedEvent, redirectedFrom } = options;
this._timestamp = requestWillBeSentEvent.timestamp;
this._wallTime = requestWillBeSentEvent.wallTime;
this._requestId = requestWillBeSentEvent.requestId;
this._interceptionId = requestPausedEvent && requestPausedEvent.requestId;
2019-12-19 16:53:24 -08:00
this._documentId = documentId;
this._route = route;
this._redirectedFrom = redirectedFrom;
2019-12-19 16:53:24 -08:00
const {
headers,
method,
url,
postDataEntries = null,
} = requestPausedEvent ? requestPausedEvent.request : requestWillBeSentEvent.request;
const type = (requestWillBeSentEvent.type || '').toLowerCase();
let postDataBuffer = null;
if (postDataEntries && postDataEntries.length && postDataEntries[0].bytes)
postDataBuffer = Buffer.from(postDataEntries[0].bytes, 'base64');
this.request = new network.Request(frame, redirectedFrom?.request || null, documentId, url, type, method, postDataBuffer, headersObjectToArray(headers));
}
_routeForRedirectChain(): RouteImpl | null {
let request: InterceptableRequest = this;
while (request._redirectedFrom)
request = request._redirectedFrom;
return request._route;
}
wasFulfilled() {
return this._routeForRedirectChain()?._wasFulfilled || false;
}
}
class RouteImpl implements network.RouteDelegate {
private readonly _client: CRSession;
private _interceptionId: string;
private _responseInterceptedPromise: Promise<Protocol.Fetch.requestPausedPayload>;
_responseInterceptedCallback: ((event: Protocol.Fetch.requestPausedPayload) => void) = () => {};
_interceptingResponse: boolean = false;
_wasFulfilled = false;
constructor(client: CRSession, interceptionId: string) {
this._client = client;
this._interceptionId = interceptionId;
this._responseInterceptedPromise = new Promise(resolve => this._responseInterceptedCallback = resolve);
2019-12-19 16:53:24 -08:00
}
2021-06-18 11:04:48 -07:00
async responseBody(): Promise<Buffer> {
const response = await this._client.send('Fetch.getResponseBody', { requestId: this._interceptionId! });
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
}
async continue(request: network.Request, overrides: types.NormalizedContinueOverrides): Promise<network.InterceptedResponse|null> {
this._interceptingResponse = !!overrides.interceptResponse;
// In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors.
await this._client._sendMayFail('Fetch.continueRequest', {
requestId: this._interceptionId!,
url: overrides.url,
headers: overrides.headers,
method: overrides.method,
postData: overrides.postData ? overrides.postData.toString('base64') : undefined
2019-12-19 16:53:24 -08:00
});
if (!this._interceptingResponse)
2021-06-18 11:04:48 -07:00
return null;
const event = await this._responseInterceptedPromise;
this._interceptionId = event.requestId;
// FIXME: plumb status text from browser
if (event.responseErrorReason) {
this._client._sendMayFail('Fetch.continueRequest', {
requestId: event.requestId
});
throw new Error(`Request failed: ${event.responseErrorReason}`);
}
return new network.InterceptedResponse(request, event.responseStatusCode!, '', event.responseHeaders!);
2019-12-19 16:53:24 -08:00
}
async fulfill(response: types.NormalizedFulfillResponse) {
this._wasFulfilled = true;
const body = response.isBase64 ? response.body : Buffer.from(response.body).toString('base64');
2019-12-19 16:53:24 -08:00
// In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors.
await this._client._sendMayFail('Fetch.fulfillRequest', {
requestId: this._interceptionId!,
responseCode: response.status,
responsePhrase: network.STATUS_TEXTS[String(response.status)],
responseHeaders: response.headers,
body,
2019-12-19 16:53:24 -08:00
});
}
async abort(errorCode: string = 'failed') {
const errorReason = errorReasons[errorCode];
assert(errorReason, 'Unknown error code: ' + errorCode);
// In certain cases, protocol will return error if the request was already canceled
// or the page was closed. We should tolerate these errors.
await this._client._sendMayFail('Fetch.failRequest', {
requestId: this._interceptionId!,
2019-12-19 16:53:24 -08:00
errorReason
});
}
}
const errorReasons: { [reason: string]: Protocol.Network.ErrorReason } = {
'aborted': 'Aborted',
'accessdenied': 'AccessDenied',
'addressunreachable': 'AddressUnreachable',
'blockedbyclient': 'BlockedByClient',
'blockedbyresponse': 'BlockedByResponse',
'connectionaborted': 'ConnectionAborted',
'connectionclosed': 'ConnectionClosed',
'connectionfailed': 'ConnectionFailed',
'connectionrefused': 'ConnectionRefused',
'connectionreset': 'ConnectionReset',
'internetdisconnected': 'InternetDisconnected',
'namenotresolved': 'NameNotResolved',
'timedout': 'TimedOut',
'failed': 'Failed',
};
type RequestInfo = {
requestId: string,
responseReceivedExtraInfo: Protocol.Network.responseReceivedExtraInfoPayload[],
responses: network.Response[],
loadingFinished?: Protocol.Network.loadingFinishedPayload,
loadingFailed?: Protocol.Network.loadingFailedPayload,
sawResponseWithoutConnectionId: boolean
};
// This class aligns responses with response headers from extra info:
// - Network.requestWillBeSent, Network.responseReceived, Network.loadingFinished/loadingFailed are
// dispatched using one channel.
// - Network.requestWillBeSentExtraInfo and Network.responseReceivedExtraInfo are dispatches on
// another channel. Those channels are not associated, so events come in random order.
//
// This class will associate responses with the new headers. These extra info headers will become
// available to client reliably upon requestfinished event only. It consumes CDP
// signals on one end and processResponse(network.Response) signals on the other hands. It then makes
// sure that responses have all the extra headers in place by the time request finises.
//
// The shape of the instrumentation API is deliberately following the CDP, so that it
// what clear what is called when and what this means to the tracker without extra
// documentation.
class ResponseExtraInfoTracker {
private _requests = new Map<string, RequestInfo>();
requestWillBeSent(event: Protocol.Network.requestWillBeSentPayload) {
const info = this._requests.get(event.requestId);
if (info) {
// This is redirect.
this._innerResponseReceived(info, event.redirectResponse);
} else {
this._getOrCreateEntry(event.requestId);
}
}
_getOrCreateEntry(requestId: string): RequestInfo {
let info = this._requests.get(requestId);
if (!info) {
info = {
requestId: requestId,
responseReceivedExtraInfo: [],
responses: [],
sawResponseWithoutConnectionId: false
};
this._requests.set(requestId, info);
}
return info;
}
responseReceived(event: Protocol.Network.responseReceivedPayload) {
const info = this._requests.get(event.requestId);
if (!info)
return;
this._innerResponseReceived(info, event.response);
}
private _innerResponseReceived(info: RequestInfo, response: Protocol.Network.Response | undefined) {
if (!response?.connectionId) {
// Starting with this response we no longer can guarantee that response and extra info correspond to the same index.
info.sawResponseWithoutConnectionId = true;
}
}
responseReceivedExtraInfo(event: Protocol.Network.responseReceivedExtraInfoPayload) {
const info = this._getOrCreateEntry(event.requestId);
info.responseReceivedExtraInfo.push(event);
this._patchResponseHeaders(info, info.responseReceivedExtraInfo.length - 1);
this._checkFinished(info);
}
processResponse(requestId: string, response: network.Response, wasFulfilled: boolean) {
// We are not interested in ExtraInfo tracking for fulfilled requests, our Blink
// headers are the ones that contain fulfilled headers.
if (wasFulfilled) {
this._stopTracking(requestId);
return;
}
const info = this._requests.get(requestId);
if (!info || info.sawResponseWithoutConnectionId)
return;
response.setWillReceiveExtraHeaders();
info.responses.push(response);
this._patchResponseHeaders(info, info.responses.length - 1);
}
loadingFinished(event: Protocol.Network.loadingFinishedPayload) {
const info = this._requests.get(event.requestId);
if (!info)
return;
info.loadingFinished = event;
this._checkFinished(info);
}
loadingFailed(event: Protocol.Network.loadingFailedPayload) {
const info = this._requests.get(event.requestId);
if (!info)
return;
info.loadingFailed = event;
this._checkFinished(info);
}
private _patchResponseHeaders(info: RequestInfo, index: number) {
const response = info.responses[index];
const extraInfo = info.responseReceivedExtraInfo[index];
if (response && extraInfo)
response.extraHeadersReceived(headersObjectToArray(extraInfo.headers));
}
private _checkFinished(info: RequestInfo) {
if (!info.loadingFinished && !info.loadingFailed)
return;
if (info.responses.length <= info.responseReceivedExtraInfo.length) {
// We have extra info for each response.
// We could have more extra infos because we stopped collecting responses at some point.
this._stopTracking(info.requestId);
return;
}
// We are not done yet.
}
private _stopTracking(requestId: string) {
this._requests.delete(requestId);
}
}