diff --git a/docs/api.md b/docs/api.md index 513f05d6244b8..4d396b28dda13 100644 --- a/docs/api.md +++ b/docs/api.md @@ -5208,8 +5208,8 @@ Contains a boolean stating whether the response was successful (status in the ra #### httpResponse.remoteAddress() - returns: <[Object]> - - `ip` <[string]> the IP address of the remote server - - `port` <[number]> the port used to connect to the remote server + - `ip` <[string]> the IP address of the remote server if known and `undefined` otherwise. + - `port` <[number]> the port used to connect to the remote server if known and `undefined` otherwise. #### httpResponse.request() diff --git a/puppeteer-13.7.0-post.tgz b/puppeteer-13.7.0-post.tgz new file mode 100644 index 0000000000000..4b080a872a667 Binary files /dev/null and b/puppeteer-13.7.0-post.tgz differ diff --git a/src/common/HTTPRequest.ts b/src/common/HTTPRequest.ts index 496230f000130..70cd96508f966 100644 --- a/src/common/HTTPRequest.ts +++ b/src/common/HTTPRequest.ts @@ -150,9 +150,11 @@ export class HTTPRequest { private _headers: Record = {}; private _frame: Frame; private _continueRequestOverrides: ContinueRequestOverrides; - private _responseForRequest: Partial; - private _abortErrorReason: Protocol.Network.ErrorReason; - private _interceptResolutionState: InterceptResolutionState; + private _responseForRequest: Partial | null = null; + private _abortErrorReason: Protocol.Network.ErrorReason | null = null; + private _interceptResolutionState: InterceptResolutionState = { + action: InterceptResolutionAction.None, + }; private _interceptHandlers: Array<() => void | PromiseLike>; private _initiator: Protocol.Network.Initiator; @@ -174,13 +176,12 @@ export class HTTPRequest { this._interceptionId = interceptionId; this._allowInterception = allowInterception; this._url = event.request.url; - this._resourceType = event.type.toLowerCase() as ResourceType; + this._resourceType = (event.type || 'other').toLowerCase() as ResourceType; this._method = event.request.method; this._postData = event.request.postData; this._frame = frame; this._redirectChain = redirectChain; this._continueRequestOverrides = {}; - this._interceptResolutionState = { action: InterceptResolutionAction.None }; this._interceptHandlers = []; this._initiator = event.initiator; @@ -209,7 +210,7 @@ export class HTTPRequest { * @returns The `ResponseForRequest` that gets used if the * interception is allowed to respond (ie, `abort()` is not called). */ - responseForRequest(): Partial { + responseForRequest(): Partial | null { assert(this._allowInterception, 'Request Interception is not enabled!'); return this._responseForRequest; } @@ -217,7 +218,7 @@ export class HTTPRequest { /** * @returns the most recent reason for aborting the request */ - abortErrorReason(): Protocol.Network.ErrorReason { + abortErrorReason(): Protocol.Network.ErrorReason | null { assert(this._allowInterception, 'Request Interception is not enabled!'); return this._abortErrorReason; } @@ -275,6 +276,9 @@ export class HTTPRequest { case 'abort': return this._abort(this._abortErrorReason); case 'respond': + if (this._responseForRequest === null) { + throw new Error('Response is missing for the interception'); + } return this._respond(this._responseForRequest); case 'continue': return this._continue(this._continueRequestOverrides); @@ -440,8 +444,8 @@ export class HTTPRequest { } this._continueRequestOverrides = overrides; if ( - priority > this._interceptResolutionState.priority || - this._interceptResolutionState.priority === undefined + this._interceptResolutionState.priority === undefined || + priority > this._interceptResolutionState.priority ) { this._interceptResolutionState = { action: InterceptResolutionAction.Continue, @@ -530,8 +534,8 @@ export class HTTPRequest { } this._responseForRequest = response; if ( - priority > this._interceptResolutionState.priority || - this._interceptResolutionState.priority === undefined + this._interceptResolutionState.priority === undefined || + priority > this._interceptResolutionState.priority ) { this._interceptResolutionState = { action: InterceptResolutionAction.Respond, @@ -572,11 +576,12 @@ export class HTTPRequest { Buffer.byteLength(responseBody) ); + const status = response.status || 200; await this._client .send('Fetch.fulfillRequest', { requestId: this._interceptionId, - responseCode: response.status || 200, - responsePhrase: STATUS_TEXTS[response.status || 200], + responseCode: status, + responsePhrase: STATUS_TEXTS[status], responseHeaders: headersArray(responseHeaders), body: responseBody ? responseBody.toString('base64') : undefined, }) @@ -614,8 +619,8 @@ export class HTTPRequest { } this._abortErrorReason = errorReason; if ( - priority >= this._interceptResolutionState.priority || - this._interceptResolutionState.priority === undefined + this._interceptResolutionState.priority === undefined || + priority >= this._interceptResolutionState.priority ) { this._interceptResolutionState = { action: InterceptResolutionAction.Abort, @@ -626,13 +631,13 @@ export class HTTPRequest { } private async _abort( - errorReason: Protocol.Network.ErrorReason + errorReason: Protocol.Network.ErrorReason | null ): Promise { this._interceptionHandled = true; await this._client .send('Fetch.failRequest', { requestId: this._interceptionId, - errorReason, + errorReason: errorReason || 'Failed', }) .catch(handleError); } @@ -727,7 +732,7 @@ async function handleError(error: ProtocolError) { // List taken from // https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml // with extra 306 and 418 codes. -const STATUS_TEXTS = { +const STATUS_TEXTS: { [key: string]: string | undefined } = { '100': 'Continue', '101': 'Switching Protocols', '102': 'Processing', diff --git a/src/common/HTTPResponse.ts b/src/common/HTTPResponse.ts index b528ac0813d56..ea785afb00a23 100644 --- a/src/common/HTTPResponse.ts +++ b/src/common/HTTPResponse.ts @@ -26,8 +26,8 @@ import { ProtocolError } from './Errors.js'; * @public */ export interface RemoteAddress { - ip: string; - port: number; + ip?: string; + port?: number; } interface CDPSession extends EventEmitter { @@ -48,7 +48,7 @@ export class HTTPResponse { private _request: HTTPRequest; private _contentPromise: Promise | null = null; private _bodyLoadedPromise: Promise; - private _bodyLoadedPromiseFulfill: (err: Error | void) => void; + private _bodyLoadedPromiseFulfill: (err: Error | void) => void = () => {}; private _remoteAddress: RemoteAddress; private _status: number; private _statusText: string; @@ -94,7 +94,7 @@ export class HTTPResponse { this._securityDetails = responsePayload.securityDetails ? new SecurityDetails(responsePayload.securityDetails) : null; - this._timing = responsePayload.timing; + this._timing = responsePayload.timing || null; } /** @@ -117,7 +117,10 @@ export class HTTPResponse { * @internal */ _resolveBody(err: Error | null): void { - return this._bodyLoadedPromiseFulfill(err); + if (err) { + return this._bodyLoadedPromiseFulfill(err); + } + return this._bodyLoadedPromiseFulfill(); } /**