puppeteer/src/common/NetworkManager.ts

613 lines
19 KiB
TypeScript
Raw Normal View History

/**
* Copyright 2017 Google Inc. All rights reserved.
*
* 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 { Protocol } from 'devtools-protocol';
import { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping.js';
import { assert } from './assert.js';
import { EventEmitter } from './EventEmitter.js';
import { Frame } from './FrameManager.js';
import { HTTPRequest } from './HTTPRequest.js';
import { HTTPResponse } from './HTTPResponse.js';
import { FetchRequestId, NetworkEventManager } from './NetworkEventManager.js';
import { debugError, isString } from './util.js';
/**
* @public
*/
export interface Credentials {
username: string;
password: string;
}
/**
* @public
*/
export interface NetworkConditions {
// Download speed (bytes/s)
download: number;
// Upload speed (bytes/s)
upload: number;
// Latency (ms)
latency: number;
}
/**
* @public
*/
export interface InternalNetworkConditions extends NetworkConditions {
offline: boolean;
}
/**
* We use symbols to prevent any external parties listening to these events.
* They are internal to Puppeteer.
*
* @internal
*/
export const NetworkManagerEmittedEvents = {
Request: Symbol('NetworkManager.Request'),
RequestServedFromCache: Symbol('NetworkManager.RequestServedFromCache'),
Response: Symbol('NetworkManager.Response'),
RequestFailed: Symbol('NetworkManager.RequestFailed'),
RequestFinished: Symbol('NetworkManager.RequestFinished'),
} as const;
interface CDPSession extends EventEmitter {
send<T extends keyof ProtocolMapping.Commands>(
method: T,
...paramArgs: ProtocolMapping.Commands[T]['paramsType']
): Promise<ProtocolMapping.Commands[T]['returnType']>;
}
interface FrameManager {
frame(frameId: string): Frame | null;
}
/**
* @internal
*/
export class NetworkManager extends EventEmitter {
2022-06-13 09:16:25 +00:00
#client: CDPSession;
#ignoreHTTPSErrors: boolean;
#frameManager: FrameManager;
#networkEventManager = new NetworkEventManager();
#extraHTTPHeaders: Record<string, string> = {};
#credentials?: Credentials;
#attemptedAuthentications = new Set<string>();
#userRequestInterceptionEnabled = false;
#protocolRequestInterceptionEnabled = false;
#userCacheDisabled = false;
#emulatedNetworkConditions: InternalNetworkConditions = {
offline: false,
upload: -1,
download: -1,
latency: 0,
};
2020-05-07 10:54:55 +00:00
constructor(
client: CDPSession,
ignoreHTTPSErrors: boolean,
frameManager: FrameManager
) {
super();
2022-06-13 09:16:25 +00:00
this.#client = client;
this.#ignoreHTTPSErrors = ignoreHTTPSErrors;
this.#frameManager = frameManager;
2022-06-13 09:16:25 +00:00
this.#client.on('Fetch.requestPaused', this.#onRequestPaused.bind(this));
this.#client.on('Fetch.authRequired', this.#onAuthRequired.bind(this));
this.#client.on(
2020-05-07 10:54:55 +00:00
'Network.requestWillBeSent',
2022-06-13 09:16:25 +00:00
this.#onRequestWillBeSent.bind(this)
2020-05-07 10:54:55 +00:00
);
2022-06-13 09:16:25 +00:00
this.#client.on(
2020-05-07 10:54:55 +00:00
'Network.requestServedFromCache',
2022-06-13 09:16:25 +00:00
this.#onRequestServedFromCache.bind(this)
2020-05-07 10:54:55 +00:00
);
2022-06-13 09:16:25 +00:00
this.#client.on(
2020-05-07 10:54:55 +00:00
'Network.responseReceived',
2022-06-13 09:16:25 +00:00
this.#onResponseReceived.bind(this)
2020-05-07 10:54:55 +00:00
);
2022-06-13 09:16:25 +00:00
this.#client.on(
2020-05-07 10:54:55 +00:00
'Network.loadingFinished',
2022-06-13 09:16:25 +00:00
this.#onLoadingFinished.bind(this)
2020-05-07 10:54:55 +00:00
);
2022-06-13 09:16:25 +00:00
this.#client.on('Network.loadingFailed', this.#onLoadingFailed.bind(this));
this.#client.on(
'Network.responseReceivedExtraInfo',
2022-06-13 09:16:25 +00:00
this.#onResponseReceivedExtraInfo.bind(this)
);
}
async initialize(): Promise<void> {
2022-06-13 09:16:25 +00:00
await this.#client.send('Network.enable');
2022-06-14 11:55:35 +00:00
if (this.#ignoreHTTPSErrors) {
2022-06-13 09:16:25 +00:00
await this.#client.send('Security.setIgnoreCertificateErrors', {
2020-05-07 10:54:55 +00:00
ignore: true,
});
2022-06-14 11:55:35 +00:00
}
}
async authenticate(credentials?: Credentials): Promise<void> {
2022-06-13 09:16:25 +00:00
this.#credentials = credentials;
await this.#updateProtocolRequestInterception();
}
2020-05-07 10:54:55 +00:00
async setExtraHTTPHeaders(
extraHTTPHeaders: Record<string, string>
): Promise<void> {
2022-06-13 09:16:25 +00:00
this.#extraHTTPHeaders = {};
for (const key of Object.keys(extraHTTPHeaders)) {
const value = extraHTTPHeaders[key];
2020-05-07 10:54:55 +00:00
assert(
isString(value),
2020-05-07 10:54:55 +00:00
`Expected value of header "${key}" to be String, but "${typeof value}" is found.`
);
2022-06-13 09:16:25 +00:00
this.#extraHTTPHeaders[key.toLowerCase()] = value;
}
2022-06-13 09:16:25 +00:00
await this.#client.send('Network.setExtraHTTPHeaders', {
headers: this.#extraHTTPHeaders,
2020-05-07 10:54:55 +00:00
});
}
extraHTTPHeaders(): Record<string, string> {
2022-06-13 09:16:25 +00:00
return Object.assign({}, this.#extraHTTPHeaders);
}
numRequestsInProgress(): number {
2022-06-13 09:16:25 +00:00
return this.#networkEventManager.numRequestsInProgress();
}
async setOfflineMode(value: boolean): Promise<void> {
2022-06-13 09:16:25 +00:00
this.#emulatedNetworkConditions.offline = value;
await this.#updateNetworkConditions();
}
async emulateNetworkConditions(
networkConditions: NetworkConditions | null
): Promise<void> {
2022-06-13 09:16:25 +00:00
this.#emulatedNetworkConditions.upload = networkConditions
? networkConditions.upload
: -1;
2022-06-13 09:16:25 +00:00
this.#emulatedNetworkConditions.download = networkConditions
? networkConditions.download
: -1;
2022-06-13 09:16:25 +00:00
this.#emulatedNetworkConditions.latency = networkConditions
? networkConditions.latency
: 0;
2022-06-13 09:16:25 +00:00
await this.#updateNetworkConditions();
}
2022-06-13 09:16:25 +00:00
async #updateNetworkConditions(): Promise<void> {
await this.#client.send('Network.emulateNetworkConditions', {
offline: this.#emulatedNetworkConditions.offline,
latency: this.#emulatedNetworkConditions.latency,
uploadThroughput: this.#emulatedNetworkConditions.upload,
downloadThroughput: this.#emulatedNetworkConditions.download,
});
}
async setUserAgent(
userAgent: string,
userAgentMetadata?: Protocol.Emulation.UserAgentMetadata
): Promise<void> {
2022-06-13 09:16:25 +00:00
await this.#client.send('Network.setUserAgentOverride', {
userAgent: userAgent,
userAgentMetadata: userAgentMetadata,
});
}
async setCacheEnabled(enabled: boolean): Promise<void> {
2022-06-13 09:16:25 +00:00
this.#userCacheDisabled = !enabled;
await this.#updateProtocolCacheDisabled();
}
async setRequestInterception(value: boolean): Promise<void> {
2022-06-13 09:16:25 +00:00
this.#userRequestInterceptionEnabled = value;
await this.#updateProtocolRequestInterception();
}
2022-06-13 09:16:25 +00:00
async #updateProtocolRequestInterception(): Promise<void> {
const enabled = this.#userRequestInterceptionEnabled || !!this.#credentials;
2022-06-14 11:55:35 +00:00
if (enabled === this.#protocolRequestInterceptionEnabled) {
return;
}
2022-06-13 09:16:25 +00:00
this.#protocolRequestInterceptionEnabled = enabled;
if (enabled) {
await Promise.all([
2022-06-13 09:16:25 +00:00
this.#updateProtocolCacheDisabled(),
this.#client.send('Fetch.enable', {
handleAuthRequests: true,
2020-05-07 10:54:55 +00:00
patterns: [{ urlPattern: '*' }],
}),
]);
} else {
await Promise.all([
2022-06-13 09:16:25 +00:00
this.#updateProtocolCacheDisabled(),
this.#client.send('Fetch.disable'),
]);
}
}
2022-06-13 09:16:25 +00:00
#cacheDisabled(): boolean {
return this.#userCacheDisabled;
}
2022-06-13 09:16:25 +00:00
async #updateProtocolCacheDisabled(): Promise<void> {
await this.#client.send('Network.setCacheDisabled', {
cacheDisabled: this.#cacheDisabled(),
});
}
2022-06-13 09:16:25 +00:00
#onRequestWillBeSent(event: Protocol.Network.RequestWillBeSentEvent): void {
// Request interception doesn't happen for data URLs with Network Service.
2020-05-07 10:54:55 +00:00
if (
2022-06-13 09:16:25 +00:00
this.#userRequestInterceptionEnabled &&
2020-05-07 10:54:55 +00:00
!event.request.url.startsWith('data:')
) {
const { requestId: networkRequestId } = event;
2022-06-13 09:16:25 +00:00
this.#networkEventManager.storeRequestWillBeSent(networkRequestId, event);
/**
* CDP may have sent a Fetch.requestPaused event already. Check for it.
*/
const requestPausedEvent =
2022-06-13 09:16:25 +00:00
this.#networkEventManager.getRequestPaused(networkRequestId);
if (requestPausedEvent) {
const { requestId: fetchRequestId } = requestPausedEvent;
2022-06-13 09:16:25 +00:00
this.#patchRequestEventHeaders(event, requestPausedEvent);
this.#onRequest(event, fetchRequestId);
this.#networkEventManager.forgetRequestPaused(networkRequestId);
}
return;
}
2022-06-13 09:16:25 +00:00
this.#onRequest(event, undefined);
}
2022-06-13 09:16:25 +00:00
#onAuthRequired(event: Protocol.Fetch.AuthRequiredEvent): void {
/* TODO(jacktfranklin): This is defined in protocol.d.ts but not
2020-05-07 10:54:55 +00:00
* in an easily referrable way - we should look at exposing it.
*/
type AuthResponse = 'Default' | 'CancelAuth' | 'ProvideCredentials';
let response: AuthResponse = 'Default';
2022-06-13 09:16:25 +00:00
if (this.#attemptedAuthentications.has(event.requestId)) {
response = 'CancelAuth';
2022-06-13 09:16:25 +00:00
} else if (this.#credentials) {
response = 'ProvideCredentials';
2022-06-13 09:16:25 +00:00
this.#attemptedAuthentications.add(event.requestId);
}
2022-06-13 09:16:25 +00:00
const { username, password } = this.#credentials || {
2020-05-07 10:54:55 +00:00
username: undefined,
password: undefined,
};
2022-06-13 09:16:25 +00:00
this.#client
2020-05-07 10:54:55 +00:00
.send('Fetch.continueWithAuth', {
requestId: event.requestId,
authChallengeResponse: { response, username, password },
})
.catch(debugError);
}
/**
* CDP may send a Fetch.requestPaused without or before a
* Network.requestWillBeSent
*
* CDP may send multiple Fetch.requestPaused
* for the same Network.requestWillBeSent.
*/
2022-06-13 09:16:25 +00:00
#onRequestPaused(event: Protocol.Fetch.RequestPausedEvent): void {
2020-05-07 10:54:55 +00:00
if (
2022-06-13 09:16:25 +00:00
!this.#userRequestInterceptionEnabled &&
this.#protocolRequestInterceptionEnabled
2020-05-07 10:54:55 +00:00
) {
2022-06-13 09:16:25 +00:00
this.#client
2020-05-07 10:54:55 +00:00
.send('Fetch.continueRequest', {
requestId: event.requestId,
})
.catch(debugError);
}
const { networkId: networkRequestId, requestId: fetchRequestId } = event;
if (!networkRequestId) {
return;
}
const requestWillBeSentEvent = (() => {
const requestWillBeSentEvent =
2022-06-13 09:16:25 +00:00
this.#networkEventManager.getRequestWillBeSent(networkRequestId);
// redirect requests have the same `requestId`,
if (
requestWillBeSentEvent &&
(requestWillBeSentEvent.request.url !== event.request.url ||
requestWillBeSentEvent.request.method !== event.request.method)
) {
2022-06-13 09:16:25 +00:00
this.#networkEventManager.forgetRequestWillBeSent(networkRequestId);
return;
}
return requestWillBeSentEvent;
})();
if (requestWillBeSentEvent) {
2022-06-13 09:16:25 +00:00
this.#patchRequestEventHeaders(requestWillBeSentEvent, event);
this.#onRequest(requestWillBeSentEvent, fetchRequestId);
} else {
2022-06-13 09:16:25 +00:00
this.#networkEventManager.storeRequestPaused(networkRequestId, event);
}
}
2022-06-13 09:16:25 +00:00
#patchRequestEventHeaders(
requestWillBeSentEvent: Protocol.Network.RequestWillBeSentEvent,
requestPausedEvent: Protocol.Fetch.RequestPausedEvent
): void {
requestWillBeSentEvent.request.headers = {
...requestWillBeSentEvent.request.headers,
// includes extra headers, like: Accept, Origin
...requestPausedEvent.request.headers,
};
}
2022-06-13 09:16:25 +00:00
#onRequest(
event: Protocol.Network.RequestWillBeSentEvent,
fetchRequestId?: FetchRequestId
2020-05-07 10:54:55 +00:00
): void {
let redirectChain: HTTPRequest[] = [];
if (event.redirectResponse) {
// We want to emit a response and requestfinished for the
// redirectResponse, but we can't do so unless we have a
// responseExtraInfo ready to pair it up with. If we don't have any
// responseExtraInfos saved in our queue, they we have to wait until
// the next one to emit response and requestfinished, *and* we should
// also wait to emit this Request too because it should come after the
// response/requestfinished.
let redirectResponseExtraInfo = null;
if (event.redirectHasExtraInfo) {
2022-06-13 09:16:25 +00:00
redirectResponseExtraInfo = this.#networkEventManager
.responseExtraInfo(event.requestId)
.shift();
if (!redirectResponseExtraInfo) {
2022-06-13 09:16:25 +00:00
this.#networkEventManager.queueRedirectInfo(event.requestId, {
event,
fetchRequestId,
});
return;
}
}
2022-06-13 09:16:25 +00:00
const request = this.#networkEventManager.getRequest(event.requestId);
// If we connect late to the target, we could have missed the
// requestWillBeSent event.
if (request) {
2022-06-13 09:16:25 +00:00
this.#handleRequestRedirect(
request,
event.redirectResponse,
redirectResponseExtraInfo
);
redirectChain = request._redirectChain;
}
}
2020-05-07 10:54:55 +00:00
const frame = event.frameId
2022-06-13 09:16:25 +00:00
? this.#frameManager.frame(event.frameId)
2020-05-07 10:54:55 +00:00
: null;
const request = new HTTPRequest(
2022-06-13 09:16:25 +00:00
this.#client,
2020-05-07 10:54:55 +00:00
frame,
fetchRequestId,
2022-06-13 09:16:25 +00:00
this.#userRequestInterceptionEnabled,
2020-05-07 10:54:55 +00:00
event,
redirectChain
);
2022-06-13 09:16:25 +00:00
this.#networkEventManager.storeRequest(event.requestId, request);
this.emit(NetworkManagerEmittedEvents.Request, request);
request.finalizeInterceptions();
}
2022-06-13 09:16:25 +00:00
#onRequestServedFromCache(
event: Protocol.Network.RequestServedFromCacheEvent
2020-05-07 10:54:55 +00:00
): void {
2022-06-13 09:16:25 +00:00
const request = this.#networkEventManager.getRequest(event.requestId);
2022-06-14 11:55:35 +00:00
if (request) {
request._fromMemoryCache = true;
}
this.emit(NetworkManagerEmittedEvents.RequestServedFromCache, request);
}
2022-06-13 09:16:25 +00:00
#handleRequestRedirect(
request: HTTPRequest,
responsePayload: Protocol.Network.Response,
extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null
2020-05-07 10:54:55 +00:00
): void {
const response = new HTTPResponse(
2022-06-13 09:16:25 +00:00
this.#client,
request,
responsePayload,
extraInfo
);
request._response = response;
request._redirectChain.push(request);
response._resolveBody(
2020-05-07 10:54:55 +00:00
new Error('Response body is unavailable for redirect responses')
);
2022-06-13 09:16:25 +00:00
this.#forgetRequest(request, false);
this.emit(NetworkManagerEmittedEvents.Response, response);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
}
2022-06-13 09:16:25 +00:00
#emitResponseEvent(
responseReceived: Protocol.Network.ResponseReceivedEvent,
extraInfo: Protocol.Network.ResponseReceivedExtraInfoEvent | null
): void {
2022-06-13 09:16:25 +00:00
const request = this.#networkEventManager.getRequest(
responseReceived.requestId
);
// FileUpload sends a response without a matching request.
2022-06-14 11:55:35 +00:00
if (!request) {
return;
}
2022-06-13 09:16:25 +00:00
const extraInfos = this.#networkEventManager.responseExtraInfo(
responseReceived.requestId
);
if (extraInfos.length) {
debugError(
new Error(
'Unexpected extraInfo events for request ' +
responseReceived.requestId
)
);
}
const response = new HTTPResponse(
2022-06-13 09:16:25 +00:00
this.#client,
request,
responseReceived.response,
extraInfo
);
request._response = response;
this.emit(NetworkManagerEmittedEvents.Response, response);
}
2022-06-13 09:16:25 +00:00
#onResponseReceived(event: Protocol.Network.ResponseReceivedEvent): void {
const request = this.#networkEventManager.getRequest(event.requestId);
let extraInfo = null;
if (request && !request._fromMemoryCache && event.hasExtraInfo) {
2022-06-13 09:16:25 +00:00
extraInfo = this.#networkEventManager
.responseExtraInfo(event.requestId)
.shift();
if (!extraInfo) {
// Wait until we get the corresponding ExtraInfo event.
2022-06-13 09:16:25 +00:00
this.#networkEventManager.queueEventGroup(event.requestId, {
responseReceivedEvent: event,
});
return;
}
}
2022-06-13 09:16:25 +00:00
this.#emitResponseEvent(event, extraInfo);
}
2022-06-13 09:16:25 +00:00
#onResponseReceivedExtraInfo(
event: Protocol.Network.ResponseReceivedExtraInfoEvent
): void {
// We may have skipped a redirect response/request pair due to waiting for
// this ExtraInfo event. If so, continue that work now that we have the
// request.
2022-06-13 09:16:25 +00:00
const redirectInfo = this.#networkEventManager.takeQueuedRedirectInfo(
event.requestId
);
if (redirectInfo) {
2022-06-13 09:16:25 +00:00
this.#networkEventManager.responseExtraInfo(event.requestId).push(event);
this.#onRequest(redirectInfo.event, redirectInfo.fetchRequestId);
return;
}
// We may have skipped response and loading events because we didn't have
// this ExtraInfo event yet. If so, emit those events now.
2022-06-13 09:16:25 +00:00
const queuedEvents = this.#networkEventManager.getQueuedEventGroup(
event.requestId
);
if (queuedEvents) {
2022-06-13 09:16:25 +00:00
this.#networkEventManager.forgetQueuedEventGroup(event.requestId);
this.#emitResponseEvent(queuedEvents.responseReceivedEvent, event);
if (queuedEvents.loadingFinishedEvent) {
2022-06-13 09:16:25 +00:00
this.#emitLoadingFinished(queuedEvents.loadingFinishedEvent);
}
if (queuedEvents.loadingFailedEvent) {
2022-06-13 09:16:25 +00:00
this.#emitLoadingFailed(queuedEvents.loadingFailedEvent);
}
return;
}
// Wait until we get another event that can use this ExtraInfo event.
2022-06-13 09:16:25 +00:00
this.#networkEventManager.responseExtraInfo(event.requestId).push(event);
}
2022-06-13 09:16:25 +00:00
#forgetRequest(request: HTTPRequest, events: boolean): void {
const requestId = request._requestId;
const interceptionId = request._interceptionId;
2022-06-13 09:16:25 +00:00
this.#networkEventManager.forgetRequest(requestId);
interceptionId !== undefined &&
2022-06-13 09:16:25 +00:00
this.#attemptedAuthentications.delete(interceptionId);
if (events) {
2022-06-13 09:16:25 +00:00
this.#networkEventManager.forget(requestId);
}
}
2022-06-13 09:16:25 +00:00
#onLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void {
// If the response event for this request is still waiting on a
// corresponding ExtraInfo event, then wait to emit this event too.
2022-06-13 09:16:25 +00:00
const queuedEvents = this.#networkEventManager.getQueuedEventGroup(
event.requestId
);
if (queuedEvents) {
queuedEvents.loadingFinishedEvent = event;
} else {
2022-06-13 09:16:25 +00:00
this.#emitLoadingFinished(event);
}
}
2022-06-13 09:16:25 +00:00
#emitLoadingFinished(event: Protocol.Network.LoadingFinishedEvent): void {
const request = this.#networkEventManager.getRequest(event.requestId);
// For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469
2022-06-14 11:55:35 +00:00
if (!request) {
return;
}
// Under certain conditions we never get the Network.responseReceived
// event from protocol. @see https://crbug.com/883475
2022-06-14 11:55:35 +00:00
if (request.response()) {
request.response()?._resolveBody(null);
}
2022-06-13 09:16:25 +00:00
this.#forgetRequest(request, true);
this.emit(NetworkManagerEmittedEvents.RequestFinished, request);
}
2022-06-13 09:16:25 +00:00
#onLoadingFailed(event: Protocol.Network.LoadingFailedEvent): void {
// If the response event for this request is still waiting on a
// corresponding ExtraInfo event, then wait to emit this event too.
2022-06-13 09:16:25 +00:00
const queuedEvents = this.#networkEventManager.getQueuedEventGroup(
event.requestId
);
if (queuedEvents) {
queuedEvents.loadingFailedEvent = event;
} else {
2022-06-13 09:16:25 +00:00
this.#emitLoadingFailed(event);
}
}
2022-06-13 09:16:25 +00:00
#emitLoadingFailed(event: Protocol.Network.LoadingFailedEvent): void {
const request = this.#networkEventManager.getRequest(event.requestId);
// For certain requestIds we never receive requestWillBeSent event.
// @see https://crbug.com/750469
2022-06-14 11:55:35 +00:00
if (!request) {
return;
}
request._failureText = event.errorText;
const response = request.response();
2022-06-14 11:55:35 +00:00
if (response) {
response._resolveBody(null);
}
2022-06-13 09:16:25 +00:00
this.#forgetRequest(request, true);
this.emit(NetworkManagerEmittedEvents.RequestFailed, request);
}
}