Skip to content

Commit

Permalink
Introduce RequestInit.duplex
Browse files Browse the repository at this point in the history
This implements whatwg/fetch#1457.

Bug: 1337696
Change-Id: I3fcf6f484dc922f5a875ed658adad33631d55115
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3740889
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: Yoichi Osato <yoichio@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1020764}
NOKEYCHECK=True
GitOrigin-RevId: b4fc85b816b6e74d4c9ef60572bd66026c44d26a
  • Loading branch information
yutakahirano authored and copybara-github committed Jul 5, 2022
1 parent 85142ec commit dace3b4
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 124 deletions.
2 changes: 2 additions & 0 deletions blink/renderer/bindings/generated_in_core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ generated_dictionary_sources_in_core = [
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_readable_writable_pair.h",
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_reporting_observer_options.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_reporting_observer_options.h",
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_request_duplex.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_request_duplex.h",
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_request_init.cc",
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_request_init.h",
"$root_gen_dir/third_party/blink/renderer/bindings/core/v8/v8_resize_observer_options.cc",
Expand Down
14 changes: 12 additions & 2 deletions blink/renderer/core/fetch/request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static bool AreAnyMembersPresent(const RequestInit* init) {
init->hasReferrer() || init->hasReferrerPolicy() || init->hasMode() ||
init->hasCredentials() || init->hasCache() || init->hasRedirect() ||
init->hasIntegrity() || init->hasKeepalive() || init->hasPriority() ||
init->hasSignal() || init->hasTrustToken();
init->hasSignal() || init->hasDuplex() || init->hasTrustToken();
}

static BodyStreamBuffer* ExtractBody(ScriptState* script_state,
Expand Down Expand Up @@ -677,8 +677,18 @@ Request* Request::CreateRequestWithRequestOrString(
return nullptr;
}

// "If |body| is non-null and |body|’s source is null, then:"
// "If `inputOrInitBody` is non-null and `inputOrInitBody`’s source is null,
// then:"
if (body && body->IsMadeFromReadableStream()) {
// "If `initBody` is non-null and `init["duplex"]` does not exist, then
// throw a TypeError."
if (!init_body.IsEmpty() && !init_body->IsNull() && !init->hasDuplex()) {
exception_state.ThrowTypeError(
"The `duplex` member must be specified for a request with a "
"streaming body");
return nullptr;
}

// "If |this|’s request’s mode is neither "same-origin" nor "cors", then
// throw a TypeError."
if (request->Mode() != network::mojom::RequestMode::kSameOrigin &&
Expand Down
1 change: 1 addition & 0 deletions blink/renderer/core/fetch/request.idl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum RequestRedirect { "follow", "error", "manual" };
enum RequestCache { "default", "no-store", "reload", "no-cache", "force-cache",
"only-if-cached" };
enum FetchPriority {"low", "auto", "high"};
enum RequestDuplex {"half"};

// https://w3c.github.io/webappsec-referrer-policy/#referrer-policies

Expand Down
1 change: 1 addition & 0 deletions blink/renderer/core/fetch/request_init.idl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dictionary RequestInit {
boolean keepalive;
[RuntimeEnabled=PriorityHints] FetchPriority priority;
AbortSignal? signal;
[RuntimeEnabled=FetchUploadStreaming] RequestDuplex duplex;
// Even though Trust Tokens operations are only available in secure
// contexts, this has to be enforced after the fact because the
// SecureContext IDL attribute doesn't affect dictionary members.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ promise_test(async (test) => {
const request = new Request('', {
body: new ReadableStream(),
method: 'POST',
duplex,
});

assert_equals(request.headers.get('Content-Type'), null, `Request should not have a content-type set`);

const response = await fetch('data:a/a;charset=utf-8,test', {
method: 'POST',
body: new ReadableStream(),
duplex,
});

assert_equals(await response.text(), 'test', `Response has correct body`);
Expand All @@ -88,6 +90,7 @@ promise_test(async (test) => {
const request = new Request('data:a/a;charset=utf-8,test', {
body: new ReadableStream(),
method: 'POST',
duplex,
});

assert_equals(request.headers.get('Content-Type'), null, `Request should not have a content-type set`);
Expand All @@ -113,22 +116,6 @@ promise_test(async (t) => {
await promise_rejects_js(t, TypeError, fetch(url, { method, body, duplex }));
}, "Streaming upload with body containing a number");

promise_test(async (t) => {
const url = "/fetch/api/resources/redirect.h2.py?location=/common/blank.html";
const body = createStream([]);
const method = "POST";
await promise_rejects_js(t, TypeError, fetch(url, { method, body, duplex }));
}, "Streaming upload should fail on redirect (302)");

promise_test(async (t) => {
const url = "/fetch/api/resources/redirect.h2.py?" +
"redirect_status=303&location=/common/blank.html";
const body = createStream([]);
const method = "POST";
const resp = await fetch(url, { method, body, duplex });
assert_equals(resp.status, 200, 'status');
}, "Streaming upload should work with 303");

promise_test(async (t) => {
const url = "/fetch/api/resources/authentication.py?realm=test";
const body = createStream([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function fetchStreamRedirect(statusCode) {
controller.enqueue(encoder.encode("Test"));
controller.close();
}});
requestInit.duplex = "half";
return fetch(url, requestInit);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function requestFromBody(body) {
{
method: "POST",
body,
duplex: "half",
},
);
}
Expand Down Expand Up @@ -82,6 +83,7 @@ function requestFromBodyWithOverrideMime(body) {
method: "POST",
body,
headers: { "Content-Type": OVERRIDE_MIME },
duplex: "half",
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@
t.add_cleanup(() => { frame.remove(); });
const res = await frame.contentWindow.fetch('simple.html?request-body', {
method: 'POST',
body: rs.pipeThrough(new TextEncoderStream())
body: rs.pipeThrough(new TextEncoderStream()),
duplex: 'half',
});
assert_equals(await res.text(), 'i am the request body');
}, 'FetchEvent#body is a ReadableStream');
Expand Down Expand Up @@ -503,7 +504,8 @@
const echo_url = '/fetch/api/resources/echo-content.py?ignore';
const response = await frame.contentWindow.fetch(echo_url, {
method: 'POST',
body: rs.pipeThrough(new TextEncoderStream())
body: rs.pipeThrough(new TextEncoderStream()),
duplex: 'half',
});
const text = await response.text();
assert_equals(text,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit dace3b4

Please sign in to comment.