puppeteer/lib/Request.js

160 lines
3.3 KiB
JavaScript
Raw Normal View History

/**
* 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.
*/
class Headers {
2017-06-21 20:51:06 +00:00
/**
* @param {?Object} payload
* @return {!Headers}
2017-06-21 20:58:49 +00:00
*/
static fromPayload(payload) {
let headers = new Headers();
if (!payload)
return headers;
for (let key in payload)
headers.set(key, payload[key]);
return headers;
}
constructor() {
/** @type {!Map<string, string>} */
this._headers = new Map();
}
/**
* @param {string} name
* @param {string} value
*/
append(name, value) {
name = name.toLowerCase();
this._headers.set(name, value);
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} name
2017-06-21 20:58:49 +00:00
*/
delete(name) {
name = name.toLowerCase();
this._headers.delete(name);
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @return {!Iterator}
2017-06-21 20:58:49 +00:00
*/
entries() {
return this._headers.entries();
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} name
* @return {?string}
2017-06-21 20:58:49 +00:00
*/
get(name) {
name = name.toLowerCase();
return this._headers.get(name);
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @param {string} name
* @return {boolean}
2017-06-21 20:58:49 +00:00
*/
has(name) {
name = name.toLowerCase();
return this._headers.has(name);
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @return {!Iterator}
2017-06-21 20:58:49 +00:00
*/
keys() {
return this._headers.keys();
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
/**
* @return {!Iterator}
*/
values() {
return this._headers.values();
}
/**
* @param {string} name
2017-06-21 20:58:49 +00:00
* @param {string} value
*/
set(name, value) {
name = name.toLowerCase();
this._headers.set(name, value);
2017-06-21 20:51:06 +00:00
}
}
class Request {
2017-06-21 20:51:06 +00:00
/**
* @param {!Object} payload
2017-06-21 20:58:49 +00:00
*/
constructor(payload) {
this.url = payload.url;
this.method = payload.method;
this.headers = Headers.fromPayload(payload.headers);
this.postData = payload.postData;
2017-06-21 20:51:06 +00:00
}
}
class InterceptedRequest extends Request {
2017-06-21 20:51:06 +00:00
/**
* @param {!Connection} client
* @param {string} interceptionId
* @param {!Object} payload
2017-06-21 20:58:49 +00:00
*/
constructor(client, interceptionId, payload) {
super(payload);
this._client = client;
this._interceptionId = interceptionId;
this._handled = false;
2017-06-21 20:51:06 +00:00
}
2017-06-21 20:51:06 +00:00
abort() {
console.assert(!this._handled, 'This request is already handled!');
this._handled = true;
this._client.send('Network.continueInterceptedRequest', {
interceptionId: this._interceptionId,
errorReason: 'Aborted'
});
}
2017-06-21 20:51:06 +00:00
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];
2017-06-21 20:51:06 +00:00
this._client.send('Network.continueInterceptedRequest', {
interceptionId: this._interceptionId,
url: this.url,
method: this.method,
postData: this.postData,
headers: headers
2017-06-21 20:51:06 +00:00
});
}
2017-06-21 20:51:06 +00:00
/**
2017-06-21 20:58:49 +00:00
* @return {boolean}
*/
isHandled() {
2017-06-21 20:51:06 +00:00
return this._handled;
}
}
module.exports = {Request, InterceptedRequest};