Skip to content

Commit

Permalink
Fix the webidl for Headers
Browse files Browse the repository at this point in the history
Make the HeadersInit type match the spec.

Fixes #26441
  • Loading branch information
Eijebong committed May 30, 2020
1 parent 1a61937 commit c4273d8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 55 deletions.
21 changes: 10 additions & 11 deletions components/script/dom/headers.rs
Expand Up @@ -198,20 +198,19 @@ impl HeadersMethods for Headers {
}

impl Headers {
pub fn copy_from_headers(&self, headers: DomRoot<Headers>) -> ErrorResult {
for (name, value) in headers.header_list.borrow().iter() {
self.Append(
ByteString::new(Vec::from(name.as_str())),
ByteString::new(Vec::from(value.as_bytes())),
)?;
}
Ok(())
}

// https://fetch.spec.whatwg.org/#concept-headers-fill
pub fn fill(&self, filler: Option<HeadersInit>) -> ErrorResult {
match filler {
// Step 1
Some(HeadersInit::Headers(h)) => {
for (name, value) in h.header_list.borrow().iter() {
self.Append(
ByteString::new(Vec::from(name.as_str())),
ByteString::new(Vec::from(value.as_bytes())),
)?;
}
Ok(())
},
// Step 2
Some(HeadersInit::ByteStringSequenceSequence(v)) => {
for mut seq in v {
if seq.len() == 2 {
Expand Down
43 changes: 13 additions & 30 deletions components/script/dom/request.rs
Expand Up @@ -311,27 +311,17 @@ impl Request {
r.headers.or_init(|| Headers::for_request(&r.global()));

// Step 32 - but spec says this should only be when non-empty init?
// Step 32.1
let mut headers_copy = r.Headers();

// Step 32.2
if let Some(possible_header) = init.headers.as_ref() {
match possible_header {
&HeadersInit::Headers(ref init_headers) => {
headers_copy = DomRoot::from_ref(&*init_headers);
},
&HeadersInit::ByteStringSequenceSequence(ref init_sequence) => {
headers_copy.fill(Some(HeadersInit::ByteStringSequenceSequence(
init_sequence.clone(),
)))?;
let headers_copy = init
.headers
.as_ref()
.map(|possible_header| match possible_header {
HeadersInit::ByteStringSequenceSequence(init_sequence) => {
HeadersInit::ByteStringSequenceSequence(init_sequence.clone())
},
&HeadersInit::ByteStringByteStringRecord(ref init_map) => {
headers_copy.fill(Some(HeadersInit::ByteStringByteStringRecord(
init_map.clone(),
)))?;
HeadersInit::ByteStringByteStringRecord(init_map) => {
HeadersInit::ByteStringByteStringRecord(init_map.clone())
},
}
}
});

// Step 32.3
// We cannot empty `r.Headers().header_list` because
Expand All @@ -357,21 +347,17 @@ impl Request {
}

// Step 32.5
match init.headers {
match headers_copy {
None => {
// This is equivalent to the specification's concept of
// "associated headers list". If an init headers is not given,
// but an input with headers is given, set request's
// headers as the input's Headers.
if let RequestInfo::Request(ref input_request) = input {
r.Headers()
.fill(Some(HeadersInit::Headers(input_request.Headers())))?;
r.Headers().copy_from_headers(input_request.Headers())?;
}
},
Some(HeadersInit::Headers(_)) => {
r.Headers().fill(Some(HeadersInit::Headers(headers_copy)))?
},
_ => {},
Some(headers_copy) => r.Headers().fill(Some(headers_copy))?,
}

// Step 32.5-6 depending on how we got here
Expand Down Expand Up @@ -493,9 +479,7 @@ impl Request {
*r_clone.request.borrow_mut() = req.clone();
r_clone.body_used.set(body_used);
*r_clone.mime_type.borrow_mut() = mime_type;
r_clone
.Headers()
.fill(Some(HeadersInit::Headers(r.Headers())))?;
r_clone.Headers().copy_from_headers(r.Headers())?;
r_clone.Headers().set_guard(headers_guard);
Ok(r_clone)
}
Expand Down Expand Up @@ -895,7 +879,6 @@ impl Into<RequestRedirect> for NetTraitsRequestRedirect {
impl Clone for HeadersInit {
fn clone(&self) -> HeadersInit {
match self {
&HeadersInit::Headers(ref h) => HeadersInit::Headers(h.clone()),
&HeadersInit::ByteStringSequenceSequence(ref b) => {
HeadersInit::ByteStringSequenceSequence(b.clone())
},
Expand Down
6 changes: 2 additions & 4 deletions components/script/dom/response.rs
Expand Up @@ -4,7 +4,7 @@

use crate::body::{consume_body, consume_body_with_promise, BodyOperations, BodyType};
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::HeadersBinding::{HeadersInit, HeadersMethods};
use crate::dom::bindings::codegen::Bindings::HeadersBinding::HeadersMethods;
use crate::dom::bindings::codegen::Bindings::ResponseBinding;
use crate::dom::bindings::codegen::Bindings::ResponseBinding::{
ResponseMethods, ResponseType as DOMResponseType,
Expand Down Expand Up @@ -335,9 +335,7 @@ impl ResponseMethods for Response {
// Step 2
let new_response = Response::new(&self.global());
new_response.Headers().set_guard(self.Headers().get_guard());
new_response
.Headers()
.fill(Some(HeadersInit::Headers(self.Headers())))?;
new_response.Headers().copy_from_headers(self.Headers())?;

// https://fetch.spec.whatwg.org/#concept-response-clone
// Instead of storing a net_traits::Response internally, we
Expand Down
2 changes: 1 addition & 1 deletion components/script/dom/webidls/Headers.webidl
Expand Up @@ -4,7 +4,7 @@

// https://fetch.spec.whatwg.org/#headers-class

typedef (Headers or sequence<sequence<ByteString>> or record<ByteString, ByteString>) HeadersInit;
typedef (sequence<sequence<ByteString>> or record<ByteString, ByteString>) HeadersInit;

[Exposed=(Window,Worker)]
interface Headers {
Expand Down
5 changes: 0 additions & 5 deletions tests/wpt/metadata/fetch/api/headers/headers-basic.html.ini

This file was deleted.

6 changes: 2 additions & 4 deletions tests/wpt/metadata/fetch/range/general.any.js.ini
Expand Up @@ -5,19 +5,17 @@
[general]
expected: FAIL

[Privileged header not allowed for guard type: request-no-cors]
expected: FAIL
[Fetch with range header will be sent with Accept-Encoding: identity]
expected: FAIL


[general.any.html]
[Untitled]
expected: FAIL

[general]
expected: FAIL

[Privileged header not allowed for guard type: request-no-cors]
expected: FAIL
[Fetch with range header will be sent with Accept-Encoding: identity]
expected: FAIL

0 comments on commit c4273d8

Please sign in to comment.