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');
|
2017-09-11 23:21:51 +00:00
|
|
|
const {helper, debugError} = require('./helper');
|
2017-08-12 00:24:31 +00:00
|
|
|
const Multimap = require('./Multimap');
|
2017-08-25 22:13:59 +00:00
|
|
|
const url = require('url');
|
2017-06-29 06:09:28 +00:00
|
|
|
|
|
|
|
class NetworkManager extends EventEmitter {
|
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {Puppeteer.Session} client
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2017-07-25 09:01:10 +00:00
|
|
|
constructor(client) {
|
2017-06-29 06:09:28 +00:00
|
|
|
super();
|
|
|
|
this._client = client;
|
2017-07-27 19:25:52 +00:00
|
|
|
/** @type {!Map<string, !Request>} */
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestIdToRequest = new Map();
|
|
|
|
/** @type {!Map<string, !Request>} */
|
|
|
|
this._interceptionIdToRequest = 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;
|
2017-10-10 05:31:40 +00:00
|
|
|
/** @type {!Multimap} */
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestHashToRequestIds = new Multimap();
|
2017-10-10 05:31:40 +00:00
|
|
|
/** @type {!Multimap} */
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestHashToInterceptions = new Multimap();
|
|
|
|
|
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));
|
|
|
|
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];
|
|
|
|
console.assert(helper.isString(value), `Expected value of header "${key}" to be String, but "${typeof value}" is found.`);
|
|
|
|
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-08-12 00:24:31 +00:00
|
|
|
async setRequestInterceptionEnabled(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;
|
|
|
|
await this._client.send('Network.setRequestInterceptionEnabled', {enabled});
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_onRequestIntercepted(event) {
|
2017-08-15 20:55:48 +00:00
|
|
|
// Strip out url hash to be consistent with requestWillBeSent. @see crbug.com/755456
|
|
|
|
event.request.url = removeURLHash(event.request.url);
|
|
|
|
|
2017-09-11 23:32:13 +00:00
|
|
|
if (event.authChallenge) {
|
|
|
|
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-12 00:24:31 +00:00
|
|
|
if (event.redirectStatusCode) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const request = this._interceptionIdToRequest.get(event.interceptionId);
|
2017-08-12 00:24:31 +00:00
|
|
|
console.assert(request, 'INTERNAL ERROR: failed to find request for interception redirect.');
|
|
|
|
this._handleRequestRedirect(request, event.redirectStatusCode, event.redirectHeaders);
|
2017-08-29 23:27:59 +00:00
|
|
|
this._handleRequestStart(request._requestId, event.interceptionId, event.redirectUrl, event.resourceType, event.request);
|
2017-08-12 00:24:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-21 23:39:04 +00:00
|
|
|
const requestHash = generateRequestHash(event.request);
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestHashToInterceptions.set(requestHash, event);
|
|
|
|
this._maybeResolveInterception(requestHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Request} request
|
|
|
|
* @param {number} redirectStatus
|
|
|
|
* @param {!Object} redirectHeaders
|
|
|
|
*/
|
|
|
|
_handleRequestRedirect(request, redirectStatus, redirectHeaders) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const response = new Response(this._client, request, redirectStatus, redirectHeaders);
|
2017-08-12 00:24:31 +00:00
|
|
|
request._response = response;
|
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
this._interceptionIdToRequest.delete(request._interceptionId);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} requestId
|
|
|
|
* @param {string} interceptionId
|
|
|
|
* @param {string} url
|
2017-08-29 23:27:59 +00:00
|
|
|
* @param {string} resourceType
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {!Object} requestPayload
|
|
|
|
*/
|
2017-08-29 23:27:59 +00:00
|
|
|
_handleRequestStart(requestId, interceptionId, url, resourceType, requestPayload) {
|
2017-09-11 23:32:13 +00:00
|
|
|
const request = new Request(this._client, requestId, interceptionId, this._userRequestInterceptionEnabled, url, resourceType, requestPayload);
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestIdToRequest.set(requestId, request);
|
|
|
|
this._interceptionIdToRequest.set(interceptionId, request);
|
|
|
|
this.emit(NetworkManager.Events.Request, request);
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_onRequestWillBeSent(event) {
|
2017-09-11 23:32:13 +00:00
|
|
|
if (this._protocolRequestInterceptionEnabled && !event.request.url.startsWith('data:')) {
|
2017-08-12 00:24:31 +00:00
|
|
|
// All redirects are handled in requestIntercepted.
|
|
|
|
if (event.redirectResponse)
|
|
|
|
return;
|
2017-08-21 23:39:04 +00:00
|
|
|
const requestHash = generateRequestHash(event.request);
|
2017-08-12 00:24:31 +00:00
|
|
|
this._requestHashToRequestIds.set(requestHash, event.requestId);
|
|
|
|
this._maybeResolveInterception(requestHash);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-30 01:54:01 +00:00
|
|
|
if (event.redirectResponse) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const request = this._requestIdToRequest.get(event.requestId);
|
2017-08-12 00:24:31 +00:00
|
|
|
this._handleRequestRedirect(request, event.redirectResponse.status, event.redirectResponse.headers);
|
2017-06-30 01:54:01 +00:00
|
|
|
}
|
2017-08-29 23:27:59 +00:00
|
|
|
this._handleRequestStart(event.requestId, null, event.request.url, event.type, event.request);
|
2017-08-12 00:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} requestHash
|
|
|
|
*/
|
|
|
|
_maybeResolveInterception(requestHash) {
|
|
|
|
const requestId = this._requestHashToRequestIds.firstValue(requestHash);
|
|
|
|
const interception = this._requestHashToInterceptions.firstValue(requestHash);
|
|
|
|
if (!requestId || !interception)
|
|
|
|
return;
|
|
|
|
this._requestHashToRequestIds.delete(requestHash, requestId);
|
|
|
|
this._requestHashToInterceptions.delete(requestHash, interception);
|
2017-08-29 23:27:59 +00:00
|
|
|
this._handleRequestStart(requestId, interception.interceptionId, interception.request.url, interception.resourceType, interception.request);
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_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;
|
2017-08-21 23:39:04 +00:00
|
|
|
const response = new Response(this._client, request, event.response.status, event.response.headers);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_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;
|
2017-07-30 00:42:00 +00:00
|
|
|
request._completePromiseFulfill.call(null);
|
2017-09-11 23:32:13 +00:00
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
this._interceptionIdToRequest.delete(request._interceptionId);
|
|
|
|
this._attemptedAuthentications.delete(request._interceptionId);
|
2017-06-29 06:09:28 +00:00
|
|
|
this.emit(NetworkManager.Events.RequestFinished, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_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-07-30 00:42:00 +00:00
|
|
|
request._completePromiseFulfill.call(null);
|
2017-09-11 23:32:13 +00:00
|
|
|
this._requestIdToRequest.delete(request._requestId);
|
|
|
|
this._interceptionIdToRequest.delete(request._interceptionId);
|
|
|
|
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
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.Session} client
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {string} requestId
|
|
|
|
* @param {string} interceptionId
|
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
|
2017-06-29 06:09:28 +00:00
|
|
|
* @param {!Object} payload
|
|
|
|
*/
|
2017-09-11 23:32:13 +00:00
|
|
|
constructor(client, requestId, interceptionId, allowInterception, url, resourceType, payload) {
|
2017-08-12 00:24:31 +00:00
|
|
|
this._client = client;
|
2017-07-30 00:42:00 +00:00
|
|
|
this._requestId = requestId;
|
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-07-30 00:42:00 +00:00
|
|
|
this._completePromise = new Promise(fulfill => {
|
|
|
|
this._completePromiseFulfill = fulfill;
|
|
|
|
});
|
2017-08-12 00:24:31 +00:00
|
|
|
|
|
|
|
this.url = url;
|
2017-10-10 17:53:37 +00:00
|
|
|
this.resourceType = resourceType.toLowerCase();
|
2017-06-29 06:09:28 +00:00
|
|
|
this.method = payload.method;
|
2017-07-28 06:11:24 +00:00
|
|
|
this.postData = payload.postData;
|
2017-08-28 19:09:24 +00:00
|
|
|
this.headers = {};
|
|
|
|
for (const key of Object.keys(payload.headers))
|
|
|
|
this.headers[key.toLowerCase()] = payload.headers[key];
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object=} overrides
|
|
|
|
*/
|
2017-09-11 23:21:51 +00:00
|
|
|
async continue(overrides = {}) {
|
2017-08-15 20:55:48 +00:00
|
|
|
// DataURL's are not interceptable. In this case, do nothing.
|
|
|
|
if (this.url.startsWith('data:'))
|
|
|
|
return;
|
2017-09-11 23:32:13 +00:00
|
|
|
console.assert(this._allowInterception, 'Request Interception is not enabled!');
|
2017-08-12 00:24:31 +00:00
|
|
|
console.assert(!this._interceptionHandled, 'Request is already handled!');
|
|
|
|
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-09-11 23:21:51 +00:00
|
|
|
async abort() {
|
2017-08-15 20:55:48 +00:00
|
|
|
// DataURL's are not interceptable. In this case, do nothing.
|
|
|
|
if (this.url.startsWith('data:'))
|
|
|
|
return;
|
2017-09-11 23:32:13 +00:00
|
|
|
console.assert(this._allowInterception, 'Request Interception is not enabled!');
|
2017-08-12 00:24:31 +00:00
|
|
|
console.assert(!this._interceptionHandled, 'Request is already handled!');
|
|
|
|
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,
|
|
|
|
errorReason: 'Failed'
|
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-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
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!Puppeteer.Session} 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
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2017-08-12 00:24:31 +00:00
|
|
|
constructor(client, request, status, headers) {
|
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
|
|
|
|
2017-08-12 00:24:31 +00:00
|
|
|
this.status = status;
|
|
|
|
this.ok = status >= 200 && status <= 299;
|
|
|
|
this.url = request.url;
|
2017-08-28 19:09:24 +00:00
|
|
|
this.headers = {};
|
|
|
|
for (const key of Object.keys(headers))
|
|
|
|
this.headers[key.toLowerCase()] = headers[key];
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 06:11:24 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!Buffer>}
|
|
|
|
*/
|
|
|
|
buffer() {
|
2017-07-30 00:42:00 +00:00
|
|
|
if (!this._contentPromise) {
|
|
|
|
this._contentPromise = this._request._completePromise.then(async() => {
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* @param {!Object} request
|
|
|
|
* @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-08-21 23:39:04 +00:00
|
|
|
const headers = Object.keys(request.headers);
|
2017-08-12 00:24:31 +00:00
|
|
|
headers.sort();
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const header of headers) {
|
2017-08-15 20:55:48 +00:00
|
|
|
if (header === 'Accept' || header === 'Referer' || header === 'X-DevTools-Emulate-Network-Conditions-Client-Id')
|
2017-08-12 00:24:31 +00:00
|
|
|
continue;
|
|
|
|
hash.headers[header] = request.headers[header];
|
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
|
|
|
}
|
|
|
|
|
2017-08-15 20:55:48 +00:00
|
|
|
/**
|
2017-08-25 22:13:59 +00:00
|
|
|
* @param {string} urlString
|
2017-08-15 20:55:48 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-08-25 22:13:59 +00:00
|
|
|
function removeURLHash(urlString) {
|
|
|
|
const urlObject = url.parse(urlString);
|
2017-08-15 20:55:48 +00:00
|
|
|
urlObject.hash = '';
|
2017-08-25 22:13:59 +00:00
|
|
|
return url.format(urlObject);
|
2017-08-15 20:55:48 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
NetworkManager.Events = {
|
|
|
|
Request: 'request',
|
|
|
|
Response: 'response',
|
|
|
|
RequestFailed: 'requestfailed',
|
|
|
|
RequestFinished: 'requestfinished',
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = NetworkManager;
|