refactor(network): Create Request class right away from payload (#3194)

Similarly to Responses, we now always create Request classes from
requestWillBeSent events.
This commit is contained in:
Andrey Lushnikov 2018-09-05 21:02:28 +01:00 committed by GitHub
parent 7db4f0f798
commit 84c2621dd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -196,10 +196,13 @@ class NetworkManager extends EventEmitter {
redirectChain = request._redirectChain; redirectChain = request._redirectChain;
} }
} }
const isNavigationRequest = event.requestId === event.loaderId && event.type === 'Document'; const frame = event.frameId ? this._frameManager.frame(event.frameId) : null;
this._handleRequestStart(event.requestId, interceptionId, event.request.url, isNavigationRequest, event.type, event.request, event.frameId, redirectChain); const request = new Request(this._client, frame, interceptionId, this._userRequestInterceptionEnabled, event, redirectChain);
this._requestIdToRequest.set(event.requestId, request);
this.emit(NetworkManager.Events.Request, request);
} }
/** /**
* @param {!Protocol.Network.requestServedFromCachePayload} event * @param {!Protocol.Network.requestServedFromCachePayload} event
*/ */
@ -224,25 +227,6 @@ class NetworkManager extends EventEmitter {
this.emit(NetworkManager.Events.RequestFinished, request); this.emit(NetworkManager.Events.RequestFinished, request);
} }
/**
* @param {string} requestId
* @param {?string} interceptionId
* @param {string} url
* @param {boolean} isNavigationRequest
* @param {string} resourceType
* @param {!Protocol.Network.Request} requestPayload
* @param {?string} frameId
* @param {!Array<!Request>} redirectChain
*/
_handleRequestStart(requestId, interceptionId, url, isNavigationRequest, resourceType, requestPayload, frameId, redirectChain) {
let frame = null;
if (frameId)
frame = this._frameManager.frame(frameId);
const request = new Request(this._client, requestId, interceptionId, isNavigationRequest, this._userRequestInterceptionEnabled, url, resourceType, requestPayload, frame, redirectChain);
this._requestIdToRequest.set(requestId, request);
this.emit(NetworkManager.Events.Request, request);
}
/** /**
* @param {!Protocol.Network.responseReceivedPayload} event * @param {!Protocol.Network.responseReceivedPayload} event
*/ */
@ -293,35 +277,31 @@ class NetworkManager extends EventEmitter {
class Request { class Request {
/** /**
* @param {!Puppeteer.CDPSession} client * @param {!Puppeteer.CDPSession} client
* @param {?string} requestId
* @param {string} interceptionId
* @param {boolean} isNavigationRequest
* @param {boolean} allowInterception
* @param {string} url
* @param {string} resourceType
* @param {!Protocol.Network.Request} payload
* @param {?Puppeteer.Frame} frame * @param {?Puppeteer.Frame} frame
* @param {string} interceptionId
* @param {boolean} allowInterception
* @param {!Protocol.Network.requestWillBeSentPayload} event
* @param {!Array<!Request>} redirectChain * @param {!Array<!Request>} redirectChain
*/ */
constructor(client, requestId, interceptionId, isNavigationRequest, allowInterception, url, resourceType, payload, frame, redirectChain) { constructor(client, frame, interceptionId, allowInterception, event, redirectChain) {
this._client = client; this._client = client;
this._requestId = requestId; this._requestId = event.requestId;
this._isNavigationRequest = isNavigationRequest; this._isNavigationRequest = event.requestId === event.loaderId && event.type === 'Document';
this._interceptionId = interceptionId; this._interceptionId = interceptionId;
this._allowInterception = allowInterception; this._allowInterception = allowInterception;
this._interceptionHandled = false; this._interceptionHandled = false;
this._response = null; this._response = null;
this._failureText = null; this._failureText = null;
this._url = url; this._url = event.request.url;
this._resourceType = resourceType.toLowerCase(); this._resourceType = event.type.toLowerCase();
this._method = payload.method; this._method = event.request.method;
this._postData = payload.postData; this._postData = event.request.postData;
this._headers = {}; this._headers = {};
this._frame = frame; this._frame = frame;
this._redirectChain = redirectChain; this._redirectChain = redirectChain;
for (const key of Object.keys(payload.headers)) for (const key of Object.keys(event.request.headers))
this._headers[key.toLowerCase()] = payload.headers[key]; this._headers[key.toLowerCase()] = event.request.headers[key];
this._fromMemoryCache = false; this._fromMemoryCache = false;
} }