2017-06-29 06:09:28 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-07-19 03:53:00 +00:00
|
|
|
const EventEmitter = require('events');
|
2018-05-31 23:53:51 +00:00
|
|
|
const {helper, assert, debugError} = require('./helper');
|
2017-08-12 00:24:31 +00:00
|
|
|
const Multimap = require('./Multimap');
|
2017-06-29 06:09:28 +00:00
|
|
|
|
|
|
|
class NetworkManager extends EventEmitter {
|
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2018-01-10 02:47:21 +00:00
|
|
|
* @param {!Puppeteer.FrameManager} frameManager
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2018-01-10 02:47:21 +00:00
|
|
|
constructor(client, frameManager) {
|
2017-06-29 06:09:28 +00:00
|
|
|
super();
|
|
|
|
this._client = client;
|
2018-01-10 02:47:21 +00:00
|
|
|
this._frameManager = frameManager;
|
2017-07-27 19:25:52 +00:00
|
|
|
/** @type {!Map<string, !Request>} */
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestIdToRequest = new Map();
|
2018-07-31 02:09:10 +00:00
|
|
|
/** @type {!Map<string, !Protocol.Network.requestWillBeSentPayload>} */
|
|
|
|
this._requestIdToRequestWillBeSentEvent = new Map();
|
2017-08-28 19:09:24 +00:00
|
|
|
/** @type {!Object<string, string>} */
|
|
|
|
this._extraHTTPHeaders = {};
|
2017-06-29 06:09:28 +00:00
|
|
|
|
2017-10-13 21:41:39 +00:00
|
|
|
this._offline = false;
|
|
|
|
|
2017-09-11 23:32:13 +00:00
|
|
|
/** @type {?{username: string, password: string}} */
|
|
|
|
this._credentials = null;
|
|
|
|
/** @type {!Set<string>} */
|
|
|
|
this._attemptedAuthentications = new Set();
|
|
|
|
this._userRequestInterceptionEnabled = false;
|
|
|
|
this._protocolRequestInterceptionEnabled = false;
|
2018-08-06 18:31:33 +00:00
|
|
|
/** @type {!Multimap<string, string>} */
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestHashToRequestIds = new Multimap();
|
2018-08-06 18:31:33 +00:00
|
|
|
/** @type {!Multimap<string, string>} */
|
2017-11-01 21:04:10 +00:00
|
|
|
this._requestHashToInterceptionIds = new Multimap();
|
2017-08-12 00:24:31 +00:00
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
this._client.on('Network.requestWillBeSent', this._onRequestWillBeSent.bind(this));
|
|
|
|
this._client.on('Network.requestIntercepted', this._onRequestIntercepted.bind(this));
|
2018-02-05 22:59:07 +00:00
|
|
|
this._client.on('Network.requestServedFromCache', this._onRequestServedFromCache.bind(this));
|
2017-06-29 06:09:28 +00:00
|
|
|
this._client.on('Network.responseReceived', this._onResponseReceived.bind(this));
|
|
|
|
this._client.on('Network.loadingFinished', this._onLoadingFinished.bind(this));
|
|
|
|
this._client.on('Network.loadingFailed', this._onLoadingFailed.bind(this));
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:32:13 +00:00
|
|
|
/**
|
|
|
|
* @param {?{username: string, password: string}} credentials
|
|
|
|
*/
|
|
|
|
async authenticate(credentials) {
|
|
|
|
this._credentials = credentials;
|
|
|
|
await this._updateProtocolRequestInterception();
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
2017-08-28 19:09:24 +00:00
|
|
|
* @param {!Object<string, string>} extraHTTPHeaders
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2017-07-27 19:25:52 +00:00
|
|
|
async setExtraHTTPHeaders(extraHTTPHeaders) {
|
2017-08-28 19:09:24 +00:00
|
|
|
this._extraHTTPHeaders = {};
|
2017-09-15 02:08:48 +00:00
|
|
|
for (const key of Object.keys(extraHTTPHeaders)) {
|
|
|
|
const value = extraHTTPHeaders[key];
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
|
2017-09-15 02:08:48 +00:00
|
|
|
this._extraHTTPHeaders[key.toLowerCase()] = value;
|
|
|
|
}
|
2017-08-28 19:09:24 +00:00
|
|
|
await this._client.send('Network.setExtraHTTPHeaders', { headers: this._extraHTTPHeaders });
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-28 19:09:24 +00:00
|
|
|
* @return {!Object<string, string>}
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2017-07-27 19:25:52 +00:00
|
|
|
extraHTTPHeaders() {
|
2017-08-28 19:09:24 +00:00
|
|
|
return Object.assign({}, this._extraHTTPHeaders);
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 21:41:39 +00:00
|
|
|
/**
|
|
|
|
* @param {boolean} value
|
|
|
|
*/
|
|
|
|
async setOfflineMode(value) {
|
|
|
|
if (this._offline === value)
|
|
|
|
return;
|
|
|
|
this._offline = value;
|
|
|
|
await this._client.send('Network.emulateNetworkConditions', {
|
|
|
|
offline: this._offline,
|
|
|
|
// values of 0 remove any active throttling. crbug.com/456324#c9
|
|
|
|
latency: 0,
|
|
|
|
downloadThroughput: -1,
|
|
|
|
uploadThroughput: -1
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
|
|
|
* @param {string} userAgent
|
|
|
|
*/
|
|
|
|
async setUserAgent(userAgent) {
|
2017-09-11 23:21:51 +00:00
|
|
|
await this._client.send('Network.setUserAgentOverride', { userAgent });
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {boolean} value
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2017-10-24 21:45:03 +00:00
|
|
|
async setRequestInterception(value) {
|
2017-09-11 23:32:13 +00:00
|
|
|
this._userRequestInterceptionEnabled = value;
|
|
|
|
await this._updateProtocolRequestInterception();
|
|
|
|
}
|
|
|
|
|
|
|
|
async _updateProtocolRequestInterception() {
|
|
|
|
const enabled = this._userRequestInterceptionEnabled || !!this._credentials;
|
|
|
|
if (enabled === this._protocolRequestInterceptionEnabled)
|
|
|
|
return;
|
|
|
|
this._protocolRequestInterceptionEnabled = enabled;
|
2017-10-24 21:45:03 +00:00
|
|
|
const patterns = enabled ? [{urlPattern: '*'}] : [];
|
2017-10-24 22:14:25 +00:00
|
|
|
await Promise.all([
|
|
|
|
this._client.send('Network.setCacheDisabled', {cacheDisabled: enabled}),
|
|
|
|
this._client.send('Network.setRequestInterception', {patterns})
|
|
|
|
]);
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 02:09:10 +00:00
|
|
|
/**
|
|
|
|
* @param {!Protocol.Network.requestWillBeSentPayload} event
|
|
|
|
*/
|
|
|
|
_onRequestWillBeSent(event) {
|
|
|
|
if (this._protocolRequestInterceptionEnabled) {
|
|
|
|
const requestHash = generateRequestHash(event.request);
|
|
|
|
const interceptionId = this._requestHashToInterceptionIds.firstValue(requestHash);
|
|
|
|
if (interceptionId) {
|
|
|
|
this._onRequest(event, interceptionId);
|
|
|
|
this._requestHashToInterceptionIds.delete(requestHash, interceptionId);
|
|
|
|
} else {
|
|
|
|
this._requestHashToRequestIds.set(requestHash, event.requestId);
|
|
|
|
this._requestIdToRequestWillBeSentEvent.set(event.requestId, event);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._onRequest(event, null);
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Network.requestInterceptedPayload} event
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
|
|
|
_onRequestIntercepted(event) {
|
2017-09-11 23:32:13 +00:00
|
|
|
if (event.authChallenge) {
|
2018-04-08 00:58:52 +00:00
|
|
|
/** @type {"Default"|"CancelAuth"|"ProvideCredentials"} */
|
2017-09-11 23:32:13 +00:00
|
|
|
let response = 'Default';
|
|
|
|
if (this._attemptedAuthentications.has(event.interceptionId)) {
|
|
|
|
response = 'CancelAuth';
|
|
|
|
} else if (this._credentials) {
|
|
|
|
response = 'ProvideCredentials';
|
|
|
|
this._attemptedAuthentications.add(event.interceptionId);
|
|
|
|
}
|
2017-10-10 05:31:40 +00:00
|
|
|
const {username, password} = this._credentials || {username: undefined, password: undefined};
|
2017-09-11 23:32:13 +00:00
|
|
|
this._client.send('Network.continueInterceptedRequest', {
|
|
|
|
interceptionId: event.interceptionId,
|
|
|
|
authChallengeResponse: { response, username, password }
|
|
|
|
}).catch(debugError);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this._userRequestInterceptionEnabled && this._protocolRequestInterceptionEnabled) {
|
|
|
|
this._client.send('Network.continueInterceptedRequest', {
|
|
|
|
interceptionId: event.interceptionId
|
|
|
|
}).catch(debugError);
|
|
|
|
}
|
|
|
|
|
2017-08-21 23:39:04 +00:00
|
|
|
const requestHash = generateRequestHash(event.request);
|
2017-11-01 21:04:10 +00:00
|
|
|
const requestId = this._requestHashToRequestIds.firstValue(requestHash);
|
|
|
|
if (requestId) {
|
2018-07-31 02:09:10 +00:00
|
|
|
const requestWillBeSentEvent = this._requestIdToRequestWillBeSentEvent.get(requestId);
|
|
|
|
this._onRequest(requestWillBeSentEvent, event.interceptionId);
|
2017-11-01 21:04:10 +00:00
|
|
|
this._requestHashToRequestIds.delete(requestHash, requestId);
|
2018-07-31 02:09:10 +00:00
|
|
|
this._requestIdToRequestWillBeSentEvent.delete(requestId);
|
2017-11-01 21:04:10 +00:00
|
|
|
} else {
|
|
|
|
this._requestHashToInterceptionIds.set(requestHash, event.interceptionId);
|
|
|
|
}
|
2017-08-12 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 02:09:10 +00:00
|
|
|
/**
|
|
|
|
* @param {!Protocol.Network.requestWillBeSentPayload} event
|
|
|
|
* @param {?string} interceptionId
|
|
|
|
*/
|
|
|
|
_onRequest(event, interceptionId) {
|
|
|
|
let redirectChain = [];
|
|
|
|
if (event.redirectResponse) {
|
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
|
|
|
// If we connect late to the target, we could have missed the requestWillBeSent event.
|
|
|
|
if (request) {
|
|
|
|
this._handleRequestRedirect(request, event.redirectResponse.status, event.redirectResponse.headers, event.redirectResponse.fromDiskCache, event.redirectResponse.fromServiceWorker, event.redirectResponse.securityDetails);
|
|
|
|
redirectChain = request._redirectChain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const isNavigationRequest = event.requestId === event.loaderId && event.type === 'Document';
|
|
|
|
this._handleRequestStart(event.requestId, interceptionId, event.request.url, isNavigationRequest, event.type, event.request, event.frameId, redirectChain);
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:59:07 +00:00
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Network.requestServedFromCachePayload} event
|
2018-02-05 22:59:07 +00:00
|
|
|
*/
|
|
|
|
_onRequestServedFromCache(event) {
|
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
|
|
|
if (request)
|
|
|
|
request._fromMemoryCache = true;
|
|
|
|
}
|
|
|
|
|
2017-08-12 00:24:31 +00:00
|
|
|
/**
|
|
|
|
* @param {!Request} request
|
|
|
|
* @param {number} redirectStatus
|
|
|
|
* @param {!Object} redirectHeaders
|
2018-02-05 22:59:07 +00:00
|
|
|
* @param {boolean} fromDiskCache
|
|
|
|
* @param {boolean} fromServiceWorker
|
2018-02-15 00:08:20 +00:00
|
|
|
* @param {?Object} securityDetails
|
2017-08-12 00:24:31 +00:00
|
|
|
*/
|
2018-02-15 00:08:20 +00:00
|
|
|
_handleRequestRedirect(request, redirectStatus, redirectHeaders, fromDiskCache, fromServiceWorker, securityDetails) {
|
|
|
|
const response = new Response(this._client, request, redirectStatus, redirectHeaders, fromDiskCache, fromServiceWorker, securityDetails);
|
2017-08-12 00:24:31 +00:00
|
|
|
request._response = response;
|
2018-03-16 00:17:38 +00:00
|
|
|
request._redirectChain.push(request);
|
2018-04-11 03:22:18 +00:00
|
|
|
response._bodyLoadedPromiseFulfill.call(null, new Error('Response body is unavailable for redirect responses'));
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
2017-09-11 23:32:13 +00:00
|
|
|
this._attemptedAuthentications.delete(request._interceptionId);
|
2017-08-12 00:24:31 +00:00
|
|
|
this.emit(NetworkManager.Events.Response, response);
|
|
|
|
this.emit(NetworkManager.Events.RequestFinished, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-31 02:09:10 +00:00
|
|
|
* @param {string} requestId
|
2017-11-01 21:04:10 +00:00
|
|
|
* @param {?string} interceptionId
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {string} url
|
2018-06-01 00:38:30 +00:00
|
|
|
* @param {boolean} isNavigationRequest
|
2017-08-29 23:27:59 +00:00
|
|
|
* @param {string} resourceType
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Network.Request} requestPayload
|
2018-01-10 02:47:21 +00:00
|
|
|
* @param {?string} frameId
|
2018-03-16 00:17:38 +00:00
|
|
|
* @param {!Array<!Request>} redirectChain
|
2017-08-12 00:24:31 +00:00
|
|
|
*/
|
2018-06-01 00:38:30 +00:00
|
|
|
_handleRequestStart(requestId, interceptionId, url, isNavigationRequest, resourceType, requestPayload, frameId, redirectChain) {
|
2018-01-10 02:47:21 +00:00
|
|
|
let frame = null;
|
|
|
|
if (frameId)
|
|
|
|
frame = this._frameManager.frame(frameId);
|
2018-06-01 00:38:30 +00:00
|
|
|
const request = new Request(this._client, requestId, interceptionId, isNavigationRequest, this._userRequestInterceptionEnabled, url, resourceType, requestPayload, frame, redirectChain);
|
2018-07-31 02:09:10 +00:00
|
|
|
this._requestIdToRequest.set(requestId, request);
|
2017-08-12 00:24:31 +00:00
|
|
|
this.emit(NetworkManager.Events.Request, request);
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Network.responseReceivedPayload} event
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
|
|
|
_onResponseReceived(event) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
2017-07-10 18:21:46 +00:00
|
|
|
// FileUpload sends a response without a matching request.
|
|
|
|
if (!request)
|
|
|
|
return;
|
2018-02-05 22:59:07 +00:00
|
|
|
const response = new Response(this._client, request, event.response.status, event.response.headers,
|
2018-08-24 22:17:36 +00:00
|
|
|
!!event.response.fromDiskCache, !!event.response.fromServiceWorker, event.response.securityDetails);
|
2017-06-30 01:18:06 +00:00
|
|
|
request._response = response;
|
2017-06-29 06:09:28 +00:00
|
|
|
this.emit(NetworkManager.Events.Response, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Network.loadingFinishedPayload} event
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
|
|
|
_onLoadingFinished(event) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
2017-07-30 01:16:15 +00:00
|
|
|
// For certain requestIds we never receive requestWillBeSent event.
|
2017-08-11 08:07:33 +00:00
|
|
|
// @see https://crbug.com/750469
|
2017-07-30 01:16:15 +00:00
|
|
|
if (!request)
|
|
|
|
return;
|
2018-04-11 03:22:18 +00:00
|
|
|
request.response()._bodyLoadedPromiseFulfill.call(null);
|
2017-09-11 23:32:13 +00:00
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
this._attemptedAuthentications.delete(request._interceptionId);
|
2017-06-29 06:09:28 +00:00
|
|
|
this.emit(NetworkManager.Events.RequestFinished, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-08 00:58:52 +00:00
|
|
|
* @param {!Protocol.Network.loadingFailedPayload} event
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
|
|
|
_onLoadingFailed(event) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
2017-07-30 01:16:15 +00:00
|
|
|
// For certain requestIds we never receive requestWillBeSent event.
|
2017-08-11 08:07:33 +00:00
|
|
|
// @see https://crbug.com/750469
|
2017-07-30 01:16:15 +00:00
|
|
|
if (!request)
|
|
|
|
return;
|
2017-10-18 00:48:04 +00:00
|
|
|
request._failureText = event.errorText;
|
2018-04-11 03:22:18 +00:00
|
|
|
const response = request.response();
|
|
|
|
if (response)
|
|
|
|
response._bodyLoadedPromiseFulfill.call(null);
|
2017-09-11 23:32:13 +00:00
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
this._attemptedAuthentications.delete(request._interceptionId);
|
2017-06-29 06:09:28 +00:00
|
|
|
this.emit(NetworkManager.Events.RequestFailed, request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 06:11:24 +00:00
|
|
|
class Request {
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-11-01 21:04:10 +00:00
|
|
|
* @param {?string} requestId
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {string} interceptionId
|
2018-06-01 00:38:30 +00:00
|
|
|
* @param {boolean} isNavigationRequest
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {boolean} allowInterception
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {string} url
|
2017-08-29 23:27:59 +00:00
|
|
|
* @param {string} resourceType
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Network.Request} payload
|
2018-01-10 02:47:21 +00:00
|
|
|
* @param {?Puppeteer.Frame} frame
|
2018-03-16 00:17:38 +00:00
|
|
|
* @param {!Array<!Request>} redirectChain
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2018-06-01 00:38:30 +00:00
|
|
|
constructor(client, requestId, interceptionId, isNavigationRequest, allowInterception, url, resourceType, payload, frame, redirectChain) {
|
2017-08-12 00:24:31 +00:00
|
|
|
this._client = client;
|
2017-07-30 00:42:00 +00:00
|
|
|
this._requestId = requestId;
|
2018-06-01 00:38:30 +00:00
|
|
|
this._isNavigationRequest = isNavigationRequest;
|
2017-08-12 00:24:31 +00:00
|
|
|
this._interceptionId = interceptionId;
|
2017-09-11 23:32:13 +00:00
|
|
|
this._allowInterception = allowInterception;
|
2017-08-12 00:24:31 +00:00
|
|
|
this._interceptionHandled = false;
|
2017-06-30 01:18:06 +00:00
|
|
|
this._response = null;
|
2017-10-18 00:48:04 +00:00
|
|
|
this._failureText = null;
|
2017-08-12 00:24:31 +00:00
|
|
|
|
2017-12-19 01:05:57 +00:00
|
|
|
this._url = url;
|
|
|
|
this._resourceType = resourceType.toLowerCase();
|
|
|
|
this._method = payload.method;
|
|
|
|
this._postData = payload.postData;
|
|
|
|
this._headers = {};
|
2018-01-10 02:47:21 +00:00
|
|
|
this._frame = frame;
|
2018-03-16 00:17:38 +00:00
|
|
|
this._redirectChain = redirectChain;
|
2017-08-28 19:09:24 +00:00
|
|
|
for (const key of Object.keys(payload.headers))
|
2017-12-19 01:05:57 +00:00
|
|
|
this._headers[key.toLowerCase()] = payload.headers[key];
|
2018-02-05 22:59:07 +00:00
|
|
|
|
|
|
|
this._fromMemoryCache = false;
|
2017-12-19 01:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
url() {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
resourceType() {
|
|
|
|
return this._resourceType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
method() {
|
|
|
|
return this._method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-24 22:17:36 +00:00
|
|
|
* @return {string|undefined}
|
2017-12-19 01:05:57 +00:00
|
|
|
*/
|
|
|
|
postData() {
|
|
|
|
return this._postData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Object}
|
|
|
|
*/
|
|
|
|
headers() {
|
|
|
|
return this._headers;
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {?Response}
|
|
|
|
*/
|
|
|
|
response() {
|
2017-06-30 01:18:06 +00:00
|
|
|
return this._response;
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
2017-08-12 00:24:31 +00:00
|
|
|
|
2018-01-10 02:47:21 +00:00
|
|
|
/**
|
|
|
|
* @return {?Puppeteer.Frame}
|
|
|
|
*/
|
|
|
|
frame() {
|
|
|
|
return this._frame;
|
|
|
|
}
|
|
|
|
|
2018-06-01 00:38:30 +00:00
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
isNavigationRequest() {
|
|
|
|
return this._isNavigationRequest;
|
|
|
|
}
|
|
|
|
|
2018-03-16 00:17:38 +00:00
|
|
|
/**
|
|
|
|
* @return {!Array<!Request>}
|
|
|
|
*/
|
|
|
|
redirectChain() {
|
|
|
|
return this._redirectChain.slice();
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:48:04 +00:00
|
|
|
/**
|
|
|
|
* @return {?{errorText: string}}
|
|
|
|
*/
|
|
|
|
failure() {
|
|
|
|
if (!this._failureText)
|
|
|
|
return null;
|
|
|
|
return {
|
|
|
|
errorText: this._failureText
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-12 00:24:31 +00:00
|
|
|
/**
|
|
|
|
* @param {!Object=} overrides
|
|
|
|
*/
|
2017-09-11 23:21:51 +00:00
|
|
|
async continue(overrides = {}) {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(this._allowInterception, 'Request Interception is not enabled!');
|
|
|
|
assert(!this._interceptionHandled, 'Request is already handled!');
|
2017-08-12 00:24:31 +00:00
|
|
|
this._interceptionHandled = true;
|
2017-09-11 23:21:51 +00:00
|
|
|
await this._client.send('Network.continueInterceptedRequest', {
|
2017-08-12 00:24:31 +00:00
|
|
|
interceptionId: this._interceptionId,
|
|
|
|
url: overrides.url,
|
|
|
|
method: overrides.method,
|
|
|
|
postData: overrides.postData,
|
2017-08-28 19:09:24 +00:00
|
|
|
headers: overrides.headers,
|
2017-09-11 23:21:51 +00:00
|
|
|
}).catch(error => {
|
|
|
|
// In certain cases, protocol will return error if the request was already canceled
|
|
|
|
// or the page was closed. We should tolerate these errors.
|
|
|
|
debugError(error);
|
2017-08-12 00:24:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-20 23:55:15 +00:00
|
|
|
/**
|
|
|
|
* @param {!{status: number, headers: Object, contentType: string, body: (string|Buffer)}} response
|
|
|
|
*/
|
|
|
|
async respond(response) {
|
2017-11-01 21:04:10 +00:00
|
|
|
// Mocking responses for dataURL requests is not currently supported.
|
2017-12-19 01:05:57 +00:00
|
|
|
if (this._url.startsWith('data:'))
|
2017-10-20 23:55:15 +00:00
|
|
|
return;
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(this._allowInterception, 'Request Interception is not enabled!');
|
|
|
|
assert(!this._interceptionHandled, 'Request is already handled!');
|
2017-10-20 23:55:15 +00:00
|
|
|
this._interceptionHandled = true;
|
|
|
|
|
|
|
|
const responseBody = response.body && helper.isString(response.body) ? Buffer.from(/** @type {string} */(response.body)) : /** @type {?Buffer} */(response.body || null);
|
|
|
|
|
|
|
|
const responseHeaders = {};
|
|
|
|
if (response.headers) {
|
|
|
|
for (const header of Object.keys(response.headers))
|
|
|
|
responseHeaders[header.toLowerCase()] = response.headers[header];
|
|
|
|
}
|
|
|
|
if (response.contentType)
|
|
|
|
responseHeaders['content-type'] = response.contentType;
|
|
|
|
if (responseBody && !('content-length' in responseHeaders)) {
|
|
|
|
// @ts-ignore
|
|
|
|
responseHeaders['content-length'] = Buffer.byteLength(responseBody);
|
|
|
|
}
|
|
|
|
|
|
|
|
const statusCode = response.status || 200;
|
|
|
|
const statusText = statusTexts[statusCode] || '';
|
|
|
|
const statusLine = `HTTP/1.1 ${statusCode} ${statusText}`;
|
|
|
|
|
|
|
|
const CRLF = '\r\n';
|
|
|
|
let text = statusLine + CRLF;
|
|
|
|
for (const header of Object.keys(responseHeaders))
|
|
|
|
text += header + ': ' + responseHeaders[header] + CRLF;
|
|
|
|
text += CRLF;
|
|
|
|
let responseBuffer = Buffer.from(text, 'utf8');
|
|
|
|
if (responseBody)
|
|
|
|
responseBuffer = Buffer.concat([responseBuffer, responseBody]);
|
|
|
|
|
|
|
|
await this._client.send('Network.continueInterceptedRequest', {
|
|
|
|
interceptionId: this._interceptionId,
|
|
|
|
rawResponse: responseBuffer.toString('base64')
|
|
|
|
}).catch(error => {
|
|
|
|
// In certain cases, protocol will return error if the request was already canceled
|
|
|
|
// or the page was closed. We should tolerate these errors.
|
|
|
|
debugError(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-18 07:26:48 +00:00
|
|
|
/**
|
|
|
|
* @param {string=} errorCode
|
|
|
|
*/
|
|
|
|
async abort(errorCode = 'failed') {
|
|
|
|
const errorReason = errorReasons[errorCode];
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(errorReason, 'Unknown error code: ' + errorCode);
|
|
|
|
assert(this._allowInterception, 'Request Interception is not enabled!');
|
|
|
|
assert(!this._interceptionHandled, 'Request is already handled!');
|
2017-08-12 00:24:31 +00:00
|
|
|
this._interceptionHandled = true;
|
2017-09-11 23:21:51 +00:00
|
|
|
await this._client.send('Network.continueInterceptedRequest', {
|
2017-08-12 00:24:31 +00:00
|
|
|
interceptionId: this._interceptionId,
|
2017-10-18 07:26:48 +00:00
|
|
|
errorReason
|
2017-09-11 23:21:51 +00:00
|
|
|
}).catch(error => {
|
|
|
|
// In certain cases, protocol will return error if the request was already canceled
|
|
|
|
// or the page was closed. We should tolerate these errors.
|
|
|
|
debugError(error);
|
2017-08-12 00:24:31 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
2017-10-18 07:26:48 +00:00
|
|
|
|
|
|
|
const errorReasons = {
|
|
|
|
'aborted': 'Aborted',
|
|
|
|
'accessdenied': 'AccessDenied',
|
|
|
|
'addressunreachable': 'AddressUnreachable',
|
2018-06-19 17:46:17 +00:00
|
|
|
'blockedbyclient': 'BlockedByClient',
|
|
|
|
'blockedbyresponse': 'BlockedByResponse',
|
2017-10-18 07:26:48 +00:00
|
|
|
'connectionaborted': 'ConnectionAborted',
|
|
|
|
'connectionclosed': 'ConnectionClosed',
|
|
|
|
'connectionfailed': 'ConnectionFailed',
|
|
|
|
'connectionrefused': 'ConnectionRefused',
|
|
|
|
'connectionreset': 'ConnectionReset',
|
|
|
|
'internetdisconnected': 'InternetDisconnected',
|
|
|
|
'namenotresolved': 'NameNotResolved',
|
|
|
|
'timedout': 'TimedOut',
|
|
|
|
'failed': 'Failed',
|
|
|
|
};
|
|
|
|
|
2017-07-19 03:53:00 +00:00
|
|
|
helper.tracePublicAPI(Request);
|
2017-06-29 06:09:28 +00:00
|
|
|
|
2017-07-28 06:11:24 +00:00
|
|
|
class Response {
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {!Request} request
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {number} status
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {!Object} headers
|
2018-02-05 22:59:07 +00:00
|
|
|
* @param {boolean} fromDiskCache
|
|
|
|
* @param {boolean} fromServiceWorker
|
2018-02-15 00:08:20 +00:00
|
|
|
* @param {?Object} securityDetails
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2018-02-15 00:08:20 +00:00
|
|
|
constructor(client, request, status, headers, fromDiskCache, fromServiceWorker, securityDetails) {
|
2017-07-30 00:42:00 +00:00
|
|
|
this._client = client;
|
2017-06-30 01:18:06 +00:00
|
|
|
this._request = request;
|
2017-07-28 06:11:24 +00:00
|
|
|
this._contentPromise = null;
|
2017-07-30 00:42:00 +00:00
|
|
|
|
2018-04-11 03:22:18 +00:00
|
|
|
this._bodyLoadedPromise = new Promise(fulfill => {
|
|
|
|
this._bodyLoadedPromiseFulfill = fulfill;
|
|
|
|
});
|
|
|
|
|
2017-12-19 01:05:57 +00:00
|
|
|
this._status = status;
|
|
|
|
this._url = request.url();
|
2018-02-05 22:59:07 +00:00
|
|
|
this._fromDiskCache = fromDiskCache;
|
|
|
|
this._fromServiceWorker = fromServiceWorker;
|
2017-12-19 01:05:57 +00:00
|
|
|
this._headers = {};
|
2017-08-28 19:09:24 +00:00
|
|
|
for (const key of Object.keys(headers))
|
2017-12-19 01:05:57 +00:00
|
|
|
this._headers[key.toLowerCase()] = headers[key];
|
2018-02-15 00:08:20 +00:00
|
|
|
this._securityDetails = null;
|
2018-02-13 19:26:18 +00:00
|
|
|
if (securityDetails) {
|
|
|
|
this._securityDetails = new SecurityDetails(
|
|
|
|
securityDetails['subjectName'],
|
|
|
|
securityDetails['issuer'],
|
|
|
|
securityDetails['validFrom'],
|
|
|
|
securityDetails['validTo'],
|
|
|
|
securityDetails['protocol']);
|
|
|
|
}
|
2017-12-19 01:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
url() {
|
|
|
|
return this._url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
ok() {
|
2018-03-12 18:34:33 +00:00
|
|
|
return this._status === 0 || (this._status >= 200 && this._status <= 299);
|
2017-12-19 01:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
status() {
|
|
|
|
return this._status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Object}
|
|
|
|
*/
|
|
|
|
headers() {
|
|
|
|
return this._headers;
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 19:26:18 +00:00
|
|
|
/**
|
2018-02-15 00:08:20 +00:00
|
|
|
* @return {?SecurityDetails}
|
2018-02-13 19:26:18 +00:00
|
|
|
*/
|
|
|
|
securityDetails() {
|
|
|
|
return this._securityDetails;
|
|
|
|
}
|
|
|
|
|
2017-07-28 06:11:24 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!Buffer>}
|
|
|
|
*/
|
|
|
|
buffer() {
|
2017-07-30 00:42:00 +00:00
|
|
|
if (!this._contentPromise) {
|
2018-04-11 03:22:18 +00:00
|
|
|
this._contentPromise = this._bodyLoadedPromise.then(async error => {
|
|
|
|
if (error)
|
|
|
|
throw error;
|
2017-08-21 23:39:04 +00:00
|
|
|
const response = await this._client.send('Network.getResponseBody', {
|
2017-07-30 00:42:00 +00:00
|
|
|
requestId: this._request._requestId
|
|
|
|
});
|
|
|
|
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
|
|
|
|
});
|
|
|
|
}
|
2017-07-28 06:11:24 +00:00
|
|
|
return this._contentPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<string>}
|
|
|
|
*/
|
|
|
|
async text() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const content = await this.buffer();
|
2017-07-28 06:11:24 +00:00
|
|
|
return content.toString('utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<!Object>}
|
|
|
|
*/
|
|
|
|
async json() {
|
2017-08-21 23:39:04 +00:00
|
|
|
const content = await this.text();
|
2017-07-28 06:11:24 +00:00
|
|
|
return JSON.parse(content);
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @return {!Request}
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
|
|
|
request() {
|
2017-06-30 01:18:06 +00:00
|
|
|
return this._request;
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
2018-02-05 22:59:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
fromCache() {
|
|
|
|
return this._fromDiskCache || this._request._fromMemoryCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
fromServiceWorker() {
|
|
|
|
return this._fromServiceWorker;
|
|
|
|
}
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
2017-07-19 03:53:00 +00:00
|
|
|
helper.tracePublicAPI(Response);
|
2017-06-29 06:09:28 +00:00
|
|
|
|
2017-08-12 00:24:31 +00:00
|
|
|
/**
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Network.Request} request
|
2017-08-12 00:24:31 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function generateRequestHash(request) {
|
2017-09-28 23:29:19 +00:00
|
|
|
let normalizedURL = request.url;
|
|
|
|
try {
|
2017-08-27 19:41:09 +00:00
|
|
|
// Decoding is necessary to normalize URLs. @see crbug.com/759388
|
2017-09-28 23:29:19 +00:00
|
|
|
// The method will throw if the URL is malformed. In this case,
|
|
|
|
// consider URL to be normalized as-is.
|
|
|
|
normalizedURL = decodeURI(request.url);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
const hash = {
|
|
|
|
url: normalizedURL,
|
2017-08-12 00:24:31 +00:00
|
|
|
method: request.method,
|
|
|
|
postData: request.postData,
|
|
|
|
headers: {},
|
|
|
|
};
|
2017-11-01 21:04:10 +00:00
|
|
|
|
|
|
|
if (!normalizedURL.startsWith('data:')) {
|
|
|
|
const headers = Object.keys(request.headers);
|
|
|
|
headers.sort();
|
2018-01-10 00:52:28 +00:00
|
|
|
for (let header of headers) {
|
|
|
|
const headerValue = request.headers[header];
|
|
|
|
header = header.toLowerCase();
|
2018-07-31 00:06:53 +00:00
|
|
|
if (header === 'accept' || header === 'referer' || header === 'x-devtools-emulate-network-conditions-client-id' || header === 'cookie')
|
2017-11-01 21:04:10 +00:00
|
|
|
continue;
|
2018-01-10 00:52:28 +00:00
|
|
|
hash.headers[header] = headerValue;
|
2017-11-01 21:04:10 +00:00
|
|
|
}
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
2017-08-12 00:24:31 +00:00
|
|
|
return JSON.stringify(hash);
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 19:26:18 +00:00
|
|
|
class SecurityDetails {
|
|
|
|
/**
|
|
|
|
* @param {string} subjectName
|
|
|
|
* @param {string} issuer
|
|
|
|
* @param {number} validFrom
|
|
|
|
* @param {number} validTo
|
|
|
|
* @param {string} protocol
|
|
|
|
*/
|
|
|
|
|
|
|
|
constructor(subjectName, issuer, validFrom, validTo, protocol) {
|
|
|
|
this._subjectName = subjectName;
|
|
|
|
this._issuer = issuer;
|
|
|
|
this._validFrom = validFrom;
|
|
|
|
this._validTo = validTo;
|
|
|
|
this._protocol = protocol;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
subjectName() {
|
|
|
|
return this._subjectName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
issuer() {
|
|
|
|
return this._issuer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
validFrom() {
|
|
|
|
return this._validFrom;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
validTo() {
|
|
|
|
return this._validTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
protocol() {
|
|
|
|
return this._protocol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
NetworkManager.Events = {
|
|
|
|
Request: 'request',
|
|
|
|
Response: 'response',
|
|
|
|
RequestFailed: 'requestfailed',
|
|
|
|
RequestFinished: 'requestfinished',
|
|
|
|
};
|
|
|
|
|
2017-10-20 23:55:15 +00:00
|
|
|
const statusTexts = {
|
|
|
|
'100': 'Continue',
|
|
|
|
'101': 'Switching Protocols',
|
|
|
|
'102': 'Processing',
|
|
|
|
'200': 'OK',
|
|
|
|
'201': 'Created',
|
|
|
|
'202': 'Accepted',
|
|
|
|
'203': 'Non-Authoritative Information',
|
|
|
|
'204': 'No Content',
|
|
|
|
'206': 'Partial Content',
|
|
|
|
'207': 'Multi-Status',
|
|
|
|
'208': 'Already Reported',
|
|
|
|
'209': 'IM Used',
|
|
|
|
'300': 'Multiple Choices',
|
|
|
|
'301': 'Moved Permanently',
|
|
|
|
'302': 'Found',
|
|
|
|
'303': 'See Other',
|
|
|
|
'304': 'Not Modified',
|
|
|
|
'305': 'Use Proxy',
|
|
|
|
'306': 'Switch Proxy',
|
|
|
|
'307': 'Temporary Redirect',
|
|
|
|
'308': 'Permanent Redirect',
|
|
|
|
'400': 'Bad Request',
|
|
|
|
'401': 'Unauthorized',
|
|
|
|
'402': 'Payment Required',
|
|
|
|
'403': 'Forbidden',
|
|
|
|
'404': 'Not Found',
|
|
|
|
'405': 'Method Not Allowed',
|
|
|
|
'406': 'Not Acceptable',
|
|
|
|
'407': 'Proxy Authentication Required',
|
|
|
|
'408': 'Request Timeout',
|
|
|
|
'409': 'Conflict',
|
|
|
|
'410': 'Gone',
|
|
|
|
'411': 'Length Required',
|
|
|
|
'412': 'Precondition Failed',
|
|
|
|
'413': 'Payload Too Large',
|
|
|
|
'414': 'URI Too Long',
|
|
|
|
'415': 'Unsupported Media Type',
|
|
|
|
'416': 'Range Not Satisfiable',
|
|
|
|
'417': 'Expectation Failed',
|
|
|
|
'418': 'I\'m a teapot',
|
|
|
|
'421': 'Misdirected Request',
|
|
|
|
'422': 'Unprocessable Entity',
|
|
|
|
'423': 'Locked',
|
|
|
|
'424': 'Failed Dependency',
|
|
|
|
'426': 'Upgrade Required',
|
|
|
|
'428': 'Precondition Required',
|
|
|
|
'429': 'Too Many Requests',
|
|
|
|
'431': 'Request Header Fields Too Large',
|
|
|
|
'451': 'Unavailable For Legal Reasons',
|
|
|
|
'500': 'Internal Server Error',
|
|
|
|
'501': 'Not Implemented',
|
|
|
|
'502': 'Bad Gateway',
|
|
|
|
'503': 'Service Unavailable',
|
|
|
|
'504': 'Gateway Timeout',
|
|
|
|
'505': 'HTTP Version Not Supported',
|
|
|
|
'506': 'Variant Also Negotiates',
|
|
|
|
'507': 'Insufficient Storage',
|
|
|
|
'508': 'Loop Detected',
|
|
|
|
'510': 'Not Extended',
|
|
|
|
'511': 'Network Authentication Required',
|
|
|
|
};
|
|
|
|
|
2018-03-12 18:34:33 +00:00
|
|
|
module.exports = {Request, Response, NetworkManager};
|