Fix content-type when creating a Request with FormData body (#36194)

When you create a `Request` object with a `FormData` body, the spec
says that the `Content-Type` header should start with
`multipart/form-data; boundary=`. However, in Servo's implementation
it started with `multipart/form-data;boundary=`, without the space.

While all reasonable servers should be able to that headers whether
the space is present or not, this brings Servo closer to the spec, and
also makes some WPT tests pass.

Note that submitting a form with `enctype="multipart/form-data"` does
produce a `Content-Type` header with the space (see
`HTMLFormElement::submit_entity_body`).

Signed-off-by: Andreu Botella <abotella@igalia.com>
This commit is contained in:
Andreu Botella 2025-03-28 14:33:07 +01:00 committed by GitHub
parent c728f31a10
commit 482d28e4ff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 7 additions and 26 deletions

View file

@ -539,7 +539,7 @@ impl Extractable for FormData {
let bytes = encode_multipart_form_data(&mut self.datums(), boundary.clone(), UTF_8); let bytes = encode_multipart_form_data(&mut self.datums(), boundary.clone(), UTF_8);
let total_bytes = bytes.len(); let total_bytes = bytes.len();
let content_type = Some(DOMString::from(format!( let content_type = Some(DOMString::from(format!(
"multipart/form-data;boundary={}", "multipart/form-data; boundary={}",
boundary boundary
))); )));
let stream = ReadableStream::new_from_bytes(global, bytes, can_gc)?; let stream = ReadableStream::new_from_bytes(global, bytes, can_gc)?;

View file

@ -5,16 +5,10 @@
expected: ERROR expected: ERROR
[request-headers.any.html] [request-headers.any.html]
[Fetch with POST with FormData body]
expected: FAIL
[Fetch with POST with Float16Array body] [Fetch with POST with Float16Array body]
expected: FAIL expected: FAIL
[request-headers.any.worker.html] [request-headers.any.worker.html]
[Fetch with POST with FormData body]
expected: FAIL
[Fetch with POST with Float16Array body] [Fetch with POST with Float16Array body]
expected: FAIL expected: FAIL

View file

@ -1,8 +0,0 @@
[request-init-contenttype.any.worker.html]
[Default Content-Type for Request with FormData body]
expected: FAIL
[request-init-contenttype.any.html]
[Default Content-Type for Request with FormData body]
expected: FAIL

View file

@ -16,3 +16,6 @@
[Consume response's body: from multipart form data blob to formData] [Consume response's body: from multipart form data blob to formData]
expected: FAIL expected: FAIL
[Consume response's body: from FormData to blob]
expected: FAIL

View file

@ -1,8 +0,0 @@
[response-init-contenttype.any.worker.html]
[Default Content-Type for Response with FormData body]
expected: FAIL
[response-init-contenttype.any.html]
[Default Content-Type for Response with FormData body]
expected: FAIL

View file

@ -6,3 +6,6 @@
[api-and-duplicate-headers.any.worker.html] [api-and-duplicate-headers.any.worker.html]
[XMLHttpRequest and duplicate Content-Length/Content-Type headers] [XMLHttpRequest and duplicate Content-Length/Content-Type headers]
expected: FAIL expected: FAIL
[fetch() and duplicate Content-Length/Content-Type headers]
expected: FAIL

View file

@ -8,8 +8,5 @@
[HTML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8] [HTML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8]
expected: FAIL expected: FAIL
[FormData request has correct default Content-Type of "multipart/form-data; boundary=_"]
expected: FAIL
[URLSearchParams request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8] [URLSearchParams request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8]
expected: FAIL expected: FAIL