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');
|
|
|
|
const helper = 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-08-09 23:14:00 +00:00
|
|
|
* @param {!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-08-12 00:24:31 +00:00
|
|
|
this._requestInterceptionEnabled = false;
|
|
|
|
/** @type {!Multimap<string, string>} */
|
|
|
|
this._requestHashToRequestIds = new Multimap();
|
|
|
|
/** @type {!Multimap<string, !Object>} */
|
|
|
|
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-08-28 19:09:24 +00:00
|
|
|
* @param {!Object<string, string>} extraHTTPHeaders
|
2017-06-29 06:09:28 +00:00
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
2017-07-27 19:25:52 +00:00
|
|
|
async setExtraHTTPHeaders(extraHTTPHeaders) {
|
2017-08-28 19:09:24 +00:00
|
|
|
this._extraHTTPHeaders = {};
|
|
|
|
for (const key of Object.keys(extraHTTPHeaders))
|
|
|
|
this._extraHTTPHeaders[key.toLowerCase()] = extraHTTPHeaders[key];
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} userAgent
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
async setUserAgent(userAgent) {
|
|
|
|
return this._client.send('Network.setUserAgentOverride', { userAgent });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {boolean} value
|
2017-06-29 06:09:28 +00:00
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
2017-08-12 00:24:31 +00:00
|
|
|
async setRequestInterceptionEnabled(value) {
|
|
|
|
await this._client.send('Network.setRequestInterceptionEnabled', {enabled: !!value});
|
|
|
|
this._requestInterceptionEnabled = value;
|
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-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);
|
|
|
|
this._handleRequestStart(request._requestId, event.interceptionId, event.redirectUrl, event.request);
|
|
|
|
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);
|
|
|
|
this.emit(NetworkManager.Events.Response, response);
|
|
|
|
this.emit(NetworkManager.Events.RequestFinished, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} requestId
|
|
|
|
* @param {string} interceptionId
|
|
|
|
* @param {string} url
|
|
|
|
* @param {!Object} requestPayload
|
|
|
|
*/
|
|
|
|
_handleRequestStart(requestId, interceptionId, url, requestPayload) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const request = new Request(this._client, requestId, interceptionId, url, 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-08-15 20:55:48 +00:00
|
|
|
if (this._requestInterceptionEnabled && !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-12 00:24:31 +00:00
|
|
|
this._handleRequestStart(event.requestId, null, event.request.url, event.request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} requestHash
|
|
|
|
* @param {!{requestEvent: ?Object, interceptionEvent: ?Object}} interception
|
|
|
|
*/
|
|
|
|
_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);
|
|
|
|
this._handleRequestStart(requestId, interception.interceptionId, interception.request.url, 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-08-12 00:24:31 +00:00
|
|
|
this._requestIdToRequest.delete(event.requestId);
|
|
|
|
this._interceptionIdToRequest.delete(event.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-08-12 00:24:31 +00:00
|
|
|
this._requestIdToRequest.delete(event.requestId);
|
|
|
|
this._interceptionIdToRequest.delete(event.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-08-12 00:24:31 +00:00
|
|
|
* @param {!Connection} client
|
|
|
|
* @param {string} requestId
|
|
|
|
* @param {string} interceptionId
|
|
|
|
* @param {string} url
|
2017-06-29 06:09:28 +00:00
|
|
|
* @param {!Object} payload
|
|
|
|
*/
|
2017-08-12 00:24:31 +00:00
|
|
|
constructor(client, requestId, interceptionId, url, payload) {
|
|
|
|
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;
|
|
|
|
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-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
|
|
|
|
*/
|
|
|
|
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-08-12 00:24:31 +00:00
|
|
|
console.assert(this._interceptionId, 'Request Interception is not enabled!');
|
|
|
|
console.assert(!this._interceptionHandled, 'Request is already handled!');
|
|
|
|
this._interceptionHandled = true;
|
|
|
|
this._client.send('Network.continueInterceptedRequest', {
|
|
|
|
interceptionId: this._interceptionId,
|
|
|
|
url: overrides.url,
|
|
|
|
method: overrides.method,
|
|
|
|
postData: overrides.postData,
|
2017-08-28 19:09:24 +00:00
|
|
|
headers: overrides.headers,
|
2017-08-12 00:24:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-12 00:24:31 +00:00
|
|
|
console.assert(this._interceptionId, 'Request Interception is not enabled!');
|
|
|
|
console.assert(!this._interceptionHandled, 'Request is already handled!');
|
|
|
|
this._interceptionHandled = true;
|
|
|
|
this._client.send('Network.continueInterceptedRequest', {
|
|
|
|
interceptionId: this._interceptionId,
|
|
|
|
errorReason: 'Failed'
|
|
|
|
});
|
|
|
|
}
|
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-08-09 23:14:00 +00:00
|
|
|
* @param {!Session} client
|
2017-08-12 00:24:31 +00:00
|
|
|
* @param {!Request} request
|
|
|
|
* @param {integer} status
|
|
|
|
* @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-08-12 00:24:31 +00:00
|
|
|
* @return {!Response}
|
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-08-21 23:39:04 +00:00
|
|
|
const hash = {
|
2017-08-27 19:41:09 +00:00
|
|
|
// Decoding is necessary to normalize URLs. @see crbug.com/759388
|
|
|
|
url: decodeURI(request.url),
|
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;
|