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-06-29 06:09:28 +00:00
|
|
|
|
|
|
|
class NetworkManager extends EventEmitter {
|
|
|
|
/**
|
|
|
|
* @param {!Connection} client
|
|
|
|
*/
|
2017-07-25 09:01:10 +00:00
|
|
|
constructor(client) {
|
2017-06-29 06:09:28 +00:00
|
|
|
super();
|
|
|
|
this._client = client;
|
|
|
|
this._requestInterceptor = null;
|
2017-07-27 19:25:52 +00:00
|
|
|
/** @type {!Map<string, !Request>} */
|
2017-06-29 06:09:28 +00:00
|
|
|
this._idToRequest = new Map();
|
2017-07-27 19:25:52 +00:00
|
|
|
/** @type {!Map<string, string>} */
|
|
|
|
this._extraHTTPHeaders = new Map();
|
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-07-27 19:25:52 +00:00
|
|
|
* @param {!Map<string, string>} extraHTTPHeaders
|
2017-06-29 06:09:28 +00:00
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
2017-07-27 19:25:52 +00:00
|
|
|
async setExtraHTTPHeaders(extraHTTPHeaders) {
|
|
|
|
this._extraHTTPHeaders = new Map(extraHTTPHeaders);
|
|
|
|
let headers = {};
|
|
|
|
for (let entry of extraHTTPHeaders.entries())
|
|
|
|
headers[entry[0]] = entry[1];
|
|
|
|
await this._client.send('Network.setExtraHTTPHeaders', { headers });
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-27 19:25:52 +00:00
|
|
|
* @return {!Map<string, string>}
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2017-07-27 19:25:52 +00:00
|
|
|
extraHTTPHeaders() {
|
|
|
|
return new Map(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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?function(!InterceptedRequest)} interceptor
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
async setRequestInterceptor(interceptor) {
|
|
|
|
this._requestInterceptor = interceptor;
|
2017-07-12 02:38:20 +00:00
|
|
|
await this._client.send('Network.setRequestInterceptionEnabled', {enabled: !!interceptor});
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_onRequestIntercepted(event) {
|
2017-07-06 00:02:58 +00:00
|
|
|
let request = new InterceptedRequest(this._client, event.interceptionId, event.request);
|
2017-06-29 06:09:28 +00:00
|
|
|
this._requestInterceptor(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_onRequestWillBeSent(event) {
|
2017-06-30 01:54:01 +00:00
|
|
|
if (event.redirectResponse) {
|
|
|
|
let request = this._idToRequest.get(event.requestId);
|
2017-07-06 02:06:47 +00:00
|
|
|
let response = new Response(request, event.redirectResponse, this._getResponseBody.bind(this, event.requestId));
|
2017-06-30 01:54:01 +00:00
|
|
|
request._response = response;
|
|
|
|
this.emit(NetworkManager.Events.Response, response);
|
|
|
|
this.emit(NetworkManager.Events.RequestFinished, request);
|
|
|
|
}
|
2017-06-30 01:18:06 +00:00
|
|
|
let request = new Request(event.request);
|
2017-06-29 06:09:28 +00:00
|
|
|
this._idToRequest.set(event.requestId, request);
|
|
|
|
this.emit(NetworkManager.Events.Request, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_onResponseReceived(event) {
|
2017-07-10 18:21:46 +00:00
|
|
|
let request = this._idToRequest.get(event.requestId);
|
|
|
|
// FileUpload sends a response without a matching request.
|
|
|
|
if (!request)
|
|
|
|
return;
|
2017-07-06 02:06:47 +00:00
|
|
|
let response = new Response(request, event.response, this._getResponseBody.bind(this, event.requestId));
|
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) {
|
|
|
|
let request = this._idToRequest.get(event.requestId);
|
2017-07-30 01:16:15 +00:00
|
|
|
// For certain requestIds we never receive requestWillBeSent event.
|
|
|
|
// @see https://github.com/GoogleChrome/puppeteer/issues/168
|
|
|
|
if (!request)
|
|
|
|
return;
|
2017-06-30 01:18:06 +00:00
|
|
|
this._idToRequest.delete(event.requestId);
|
2017-06-29 06:09:28 +00:00
|
|
|
this.emit(NetworkManager.Events.RequestFinished, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_onLoadingFailed(event) {
|
|
|
|
let request = this._idToRequest.get(event.requestId);
|
2017-07-30 01:16:15 +00:00
|
|
|
// For certain requestIds we never receive requestWillBeSent event.
|
|
|
|
// @see https://github.com/GoogleChrome/puppeteer/issues/168
|
|
|
|
if (!request)
|
|
|
|
return;
|
2017-06-30 01:18:06 +00:00
|
|
|
this._idToRequest.delete(event.requestId);
|
2017-06-29 06:09:28 +00:00
|
|
|
this.emit(NetworkManager.Events.RequestFailed, request);
|
|
|
|
}
|
2017-07-06 02:06:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} requestId
|
|
|
|
* @return {!Promise<!Buffer>}
|
|
|
|
*/
|
|
|
|
async _getResponseBody(requestId) {
|
|
|
|
let response = await this._client.send('Network.getResponseBody', {requestId});
|
|
|
|
return Buffer.from(response.body, response.base64Encoded ? 'base64' : 'utf8');
|
|
|
|
}
|
2017-06-29 06:09:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 06:11:24 +00:00
|
|
|
class Request {
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
|
|
|
* @param {!Object} payload
|
|
|
|
*/
|
2017-06-30 01:18:06 +00:00
|
|
|
constructor(payload) {
|
|
|
|
this._response = null;
|
2017-06-29 06:09:28 +00:00
|
|
|
this.url = payload.url;
|
|
|
|
this.method = payload.method;
|
2017-07-28 06:11:24 +00:00
|
|
|
this.postData = payload.postData;
|
2017-07-27 16:35:44 +00:00
|
|
|
this.headers = new Map(Object.entries(payload.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-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-06-30 01:18:06 +00:00
|
|
|
* @param {?Request} request
|
2017-06-29 06:09:28 +00:00
|
|
|
* @param {!Object} payload
|
2017-07-06 02:06:47 +00:00
|
|
|
* @param {function():!Promise<!Buffer>} contentCallback
|
2017-06-29 06:09:28 +00:00
|
|
|
*/
|
2017-07-06 02:06:47 +00:00
|
|
|
constructor(request, payload, contentCallback) {
|
2017-06-30 01:18:06 +00:00
|
|
|
this._request = request;
|
2017-07-28 06:11:24 +00:00
|
|
|
this._contentCallback = contentCallback;
|
|
|
|
this._contentPromise = null;
|
2017-07-27 16:35:44 +00:00
|
|
|
this.headers = new Map(Object.entries(payload.headers));
|
2017-06-29 06:09:28 +00:00
|
|
|
this.ok = payload.status >= 200 && payload.status <= 299;
|
|
|
|
this.status = payload.status;
|
|
|
|
this.statusText = payload.statusText;
|
|
|
|
this.url = payload.url;
|
|
|
|
}
|
|
|
|
|
2017-07-28 06:11:24 +00:00
|
|
|
/**
|
|
|
|
* @return {!Promise<!Buffer>}
|
|
|
|
*/
|
|
|
|
buffer() {
|
|
|
|
if (!this._contentPromise)
|
|
|
|
this._contentPromise = this._contentCallback();
|
|
|
|
return this._contentPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<string>}
|
|
|
|
*/
|
|
|
|
async text() {
|
|
|
|
let content = await this.buffer();
|
|
|
|
return content.toString('utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {!Promise<!Object>}
|
|
|
|
*/
|
|
|
|
async json() {
|
|
|
|
let content = await this.text();
|
|
|
|
return JSON.parse(content);
|
|
|
|
}
|
|
|
|
|
2017-06-29 06:09:28 +00:00
|
|
|
/**
|
|
|
|
* @return {?Response}
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
|
|
|
class InterceptedRequest {
|
|
|
|
/**
|
|
|
|
* @param {!Connection} client
|
|
|
|
* @param {string} interceptionId
|
|
|
|
* @param {!Object} payload
|
|
|
|
*/
|
|
|
|
constructor(client, interceptionId, payload) {
|
|
|
|
this._client = client;
|
|
|
|
this._interceptionId = interceptionId;
|
|
|
|
this._handled = false;
|
|
|
|
|
|
|
|
this.url = payload.url;
|
|
|
|
this.method = payload.method;
|
2017-07-27 16:35:44 +00:00
|
|
|
this.headers = new Map(Object.entries(payload.headers));
|
2017-06-29 06:09:28 +00:00
|
|
|
this.postData = payload.postData;
|
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
console.assert(!this._handled, 'This request is already handled!');
|
|
|
|
this._handled = true;
|
|
|
|
this._client.send('Network.continueInterceptedRequest', {
|
|
|
|
interceptionId: this._interceptionId,
|
|
|
|
errorReason: 'Aborted'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
continue() {
|
|
|
|
console.assert(!this._handled, 'This request is already handled!');
|
|
|
|
this._handled = true;
|
|
|
|
let headers = {};
|
|
|
|
for (let entry of this.headers.entries())
|
|
|
|
headers[entry[0]] = entry[1];
|
|
|
|
this._client.send('Network.continueInterceptedRequest', {
|
|
|
|
interceptionId: this._interceptionId,
|
|
|
|
url: this.url,
|
|
|
|
method: this.method,
|
|
|
|
postData: this.postData,
|
|
|
|
headers: headers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkManager.Events = {
|
|
|
|
Request: 'request',
|
|
|
|
Response: 'response',
|
|
|
|
RequestFailed: 'requestfailed',
|
|
|
|
RequestFinished: 'requestfinished',
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = NetworkManager;
|