Skip to content

Commit 455079d

Browse files
Bug 1992210 - [bidi] Support network.getData for requests using the data scheme r=webdriver-reviewers,Sasha
Adds support for retrieving the response body of requests using the data scheme via WebDriver BiDi data collection. This closes a gap with the current spec, and with the chromium implementation. Implementation wise, the approach is similar to DevTools and we just process the data url to extract the body. Differential Revision: https://phabricator.services.mozilla.com/D267404
1 parent a47d392 commit 455079d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

remote/shared/NetworkResponse.sys.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export class NetworkResponse {
100100
return this.#fromServiceWorker;
101101
}
102102

103+
get isDataURL() {
104+
return this.#isDataURL;
105+
}
106+
103107
get mimeType() {
104108
return this.#getComputedMimeType();
105109
}
@@ -247,10 +251,11 @@ export class NetworkResponse {
247251
toJSON() {
248252
return {
249253
decodedBodySize: this.decodedBodySize,
250-
headers: this.headers,
251-
headersTransmittedSize: this.headersTransmittedSize,
252254
encodedBodySize: this.encodedBodySize,
253255
fromCache: this.fromCache,
256+
headers: this.headers,
257+
headersTransmittedSize: this.headersTransmittedSize,
258+
isDataURL: this.isDataURL,
254259
mimeType: this.mimeType,
255260
protocol: this.protocol,
256261
serializedURL: this.serializedURL,

remote/webdriver-bidi/modules/root/network.sys.mjs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { RootBiDiModule } from "chrome://remote/content/webdriver-bidi/modules/R
99
const lazy = {};
1010

1111
ChromeUtils.defineESModuleGetters(lazy, {
12+
NetworkHelper:
13+
"resource://devtools/shared/network-observer/NetworkHelper.sys.mjs",
14+
1215
assert: "chrome://remote/content/shared/webdriver/Assert.sys.mjs",
1316
CacheBehavior: "chrome://remote/content/shared/NetworkCacheManager.sys.mjs",
1417
error: "chrome://remote/content/shared/webdriver/Errors.sys.mjs",
@@ -2185,13 +2188,12 @@ class NetworkModule extends RootBiDiModule {
21852188
return;
21862189
}
21872190

2188-
if (!(response instanceof lazy.NetworkResponse)) {
2191+
if (!(response instanceof lazy.NetworkResponse) && !response.isDataURL) {
21892192
lazy.logger.trace(
21902193
`Network data not collected for request "${request.requestId}" and data type "${DataType.Response}"` +
2191-
`: unsupported response (data scheme or cached resource)`
2194+
`: unsupported response (read from memory cache)`
21922195
);
21932196
// Cached stencils do not return any response body.
2194-
// TODO: Handle response body for data URLs.
21952197
collectedData.pending = false;
21962198
collectedData.networkDataCollected.resolve();
21972199
this.#collectedNetworkData.delete(
@@ -2247,10 +2249,26 @@ class NetworkModule extends RootBiDiModule {
22472249
// body. Since this is handled by the DevTools NetworkResponseListener, so
22482250
// here we wait until the response content is set.
22492251
try {
2250-
const bytesOrNull = await response.readResponseBody();
2251-
if (bytesOrNull !== null) {
2252-
bytes = bytesOrNull;
2253-
size = response.encodedBodySize;
2252+
if (response.isDataURL) {
2253+
// Handle data URLs as a special case since the response is not provided
2254+
// by the DevTools ResponseListener in this case.
2255+
const url = request.serializedURL;
2256+
const body = url.substring(url.indexOf(",") + 1);
2257+
const isText =
2258+
response.mimeType &&
2259+
lazy.NetworkHelper.isTextMimeType(response.mimeType);
2260+
// TODO: Reuse a common interface being introduced in Bug 1988955.
2261+
bytes = {
2262+
getDecodedResponseBody: () => body,
2263+
encoding: isText ? null : "base64",
2264+
};
2265+
size = body.length;
2266+
} else {
2267+
const bytesOrNull = await response.readResponseBody();
2268+
if (bytesOrNull !== null) {
2269+
bytes = bytesOrNull;
2270+
size = response.encodedBodySize;
2271+
}
22542272
}
22552273
} catch {
22562274
// Let processBodyError be this step: Do nothing.

0 commit comments

Comments
 (0)