Bug 1842619 - [bidi] Serialize header and cookies values as network.BytesValue r=webdriver-reviewers,jgraham

Differential Revision: https://phabricator.services.mozilla.com/D183867
This commit is contained in:
Julian Descottes 2023-07-19 14:34:09 +00:00
parent a6bf0f6d37
commit 86f23e52ba

View File

@ -23,9 +23,25 @@ ChromeUtils.defineESModuleGetters(lazy, {
* @property {number} timestamp
*/
/**
* Enum of possible BytesValue types.
*
* @readonly
* @enum {BytesValueType}
*/
const BytesValueType = {
Base64: "base64",
String: "string",
};
/**
* @typedef {object} BytesValue
* @property {BytesValueType} type
* @property {string} value
*/
/**
* @typedef {object} Cookie
* @property {Array<number>=} binaryValue
* @property {string} domain
* @property {number=} expires
* @property {boolean} httpOnly
@ -34,7 +50,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
* @property {('lax' | 'none' | 'strict')} sameSite
* @property {boolean} secure
* @property {number} size
* @property {string=} value
* @property {BytesValue} value
*/
/**
@ -56,9 +72,8 @@ ChromeUtils.defineESModuleGetters(lazy, {
/**
* @typedef {object} Header
* @property {Array<number>=} binaryValue
* @property {string} name
* @property {string=} value
* @property {BytesValue} value
*/
/**
@ -209,10 +224,10 @@ class NetworkModule extends Module {
timestamp,
};
const beforeRequestSentEvent = {
const beforeRequestSentEvent = this.#serializeNetworkEvent({
...baseParameters,
initiator,
};
});
const browsingContext = lazy.TabManager.getBrowsingContextById(contextId);
this.emitEvent(
@ -235,10 +250,10 @@ class NetworkModule extends Module {
timestamp,
};
const responseEvent = {
const responseEvent = this.#serializeNetworkEvent({
...baseParameters,
response: responseData,
};
});
const protocolEventName =
name === "response-started"
@ -253,6 +268,68 @@ class NetworkModule extends Module {
);
};
#serializeHeadersOrCookies(headersOrCookies) {
return headersOrCookies.map(item => ({
name: item.name,
value: this.#serializeStringAsBytesValue(item.value),
}));
}
/**
* Serialize in-place all cookies and headers arrays found in a given network
* event payload.
*
* @param {object} networkEvent
* The network event parameters object to serialize.
* @returns {object}
* The serialized network event parameters.
*/
#serializeNetworkEvent(networkEvent) {
// Make a shallow copy of networkEvent before serializing the headers and
// cookies arrays in request/response.
const serialized = { ...networkEvent };
// Make a shallow copy of the request data.
serialized.request = { ...networkEvent.request };
serialized.request.cookies = this.#serializeHeadersOrCookies(
networkEvent.request.cookies
);
serialized.request.headers = this.#serializeHeadersOrCookies(
networkEvent.request.headers
);
if (networkEvent.response?.headers) {
// Make a shallow copy of the response data.
serialized.response = { ...networkEvent.response };
serialized.response.headers = this.#serializeHeadersOrCookies(
networkEvent.response.headers
);
}
return serialized;
}
/**
* Serialize a string value as BytesValue.
*
* Note: This does not attempt to fully implement serialize protocol bytes
* (https://w3c.github.io/webdriver-bidi/#serialize-protocol-bytes) as the
* header values read from the Channel are already serialized as strings at
* the moment.
*
* @param {string} value
* The value to serialize.
*/
#serializeStringAsBytesValue(value) {
// TODO: For now, we handle all headers and cookies with the "string" type.
// See Bug 1835216 to add support for "base64" type and handle non-utf8
// values.
return {
type: BytesValueType.String,
value,
};
}
#startListening(event) {
if (this.#subscribedEvents.size == 0) {
this.#networkListener.startListening();