Skip to content

Commit

Permalink
Update overrideMimeType and final-charset to match xhr spec (#29812)
Browse files Browse the repository at this point in the history
* Update overrideMimeType to match xhr spec

* Update final-encoding to match xhr spec

* Fix an issue and add more comments

---------

Co-authored-by: Martin Robinson <mrobinson@igalia.com>
  • Loading branch information
CYBAI and mrobinson committed Feb 28, 2024
1 parent 5ba29c2 commit a89bacb
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 274 deletions.
66 changes: 35 additions & 31 deletions components/script/dom/xmlhttprequest.rs
Expand Up @@ -145,8 +145,6 @@ pub struct XMLHttpRequest {
#[ignore_malloc_size_of = "Defined in hyper"]
#[no_trace]
override_mime_type: DomRefCell<Option<Mime>>,
#[no_trace]
override_charset: DomRefCell<Option<&'static Encoding>>,

// Associated concepts
#[ignore_malloc_size_of = "Defined in hyper"]
Expand Down Expand Up @@ -201,7 +199,6 @@ impl XMLHttpRequest {
response_json: Heap::default(),
response_headers: DomRefCell::new(HeaderMap::new()),
override_mime_type: DomRefCell::new(None),
override_charset: DomRefCell::new(None),

request_method: DomRefCell::new(Method::GET),
request_url: DomRefCell::new(None),
Expand Down Expand Up @@ -892,31 +889,28 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
ByteString::new(v)
}

// https://xhr.spec.whatwg.org/#the-overridemimetype()-method
/// <https://xhr.spec.whatwg.org/#the-overridemimetype()-method>
fn OverrideMimeType(&self, mime: DOMString) -> ErrorResult {
// Step 1
// 1. If this’s state is loading or done, then throw an "InvalidStateError"
// DOMException.
match self.ready_state.get() {
XMLHttpRequestState::Loading | XMLHttpRequestState::Done => {
return Err(Error::InvalidState);
},
_ => {},
}
// Step 2
let override_mime = mime.parse::<Mime>().map_err(|_| Error::Syntax)?;
// Step 3
let mime_str = override_mime.as_ref();
let mime_parts: Vec<&str> = mime_str.split(";").collect();
let mime_no_params = if mime_parts.len() > 1 {
mime_parts[0].parse().unwrap()
} else {
override_mime.clone()

// 2. Set this’s override MIME type to the result of parsing mime.
// 3. If this’s override MIME type is failure, then set this’s override MIME type
// to application/octet-stream.
let override_mime = match mime.parse::<Mime>() {
Ok(mime) => mime,
Err(_) => "application/octet-stream"
.parse::<Mime>()
.map_err(|_| Error::Syntax)?,
};

*self.override_mime_type.borrow_mut() = Some(mime_no_params);
// Step 4
let value = override_mime.get_param(mime::CHARSET);
*self.override_charset.borrow_mut() =
value.and_then(|value| Encoding::for_label(value.as_ref().as_bytes()));
*self.override_mime_type.borrow_mut() = Some(override_mime);
Ok(())
}

Expand Down Expand Up @@ -1604,19 +1598,29 @@ impl XMLHttpRequest {
Ok(())
}

/// <https://xhr.spec.whatwg.org/#final-charset>
fn final_charset(&self) -> Option<&'static Encoding> {
if self.override_charset.borrow().is_some() {
self.override_charset.borrow().clone()
} else {
match self.response_headers.borrow().typed_get::<ContentType>() {
Some(ct) => {
let mime: Mime = ct.into();
let value = mime.get_param(mime::CHARSET);
value.and_then(|value| Encoding::for_label(value.as_ref().as_bytes()))
},
None => None,
}
}
// 1. Let label be null.
// 2. Let responseMIME be the result of get a response MIME type for xhr.
// 3. If responseMIME’s parameters["charset"] exists, then set label to it.
let response_charset = self
.response_mime_type()
.and_then(|mime| mime.get_param(mime::CHARSET).map(|c| c.to_string()));

// 4. If xhr’s override MIME type’s parameters["charset"] exists, then set label to it.
let override_charset = self
.override_mime_type
.borrow()
.as_ref()
.and_then(|mime| mime.get_param(mime::CHARSET).map(|c| c.to_string()));

// 5. If label is null, then return null.
// 6. Let encoding be the result of getting an encoding from label.
// 7. If encoding is failure, then return null.
// 8. Return encoding.
override_charset
.or(response_charset)
.and_then(|charset| Encoding::for_label(&charset.as_bytes()))
}

/// <https://xhr.spec.whatwg.org/#response-mime-type>
Expand Down
@@ -1,31 +1,4 @@
[single-byte-decoder.window.html?XMLHttpRequest]
[ISO-8859-2: iso_8859-2:1987 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-3: iso_8859-3:1988 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-4: iso_8859-4:1988 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-5: iso_8859-5:1988 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-6: iso_8859-6:1987 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-7: iso_8859-7:1987 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-8: iso_8859-8:1988 (XMLHttpRequest)]
expected: FAIL

[windows-1252: iso_8859-1:1987 (XMLHttpRequest)]
expected: FAIL

[windows-1254: iso_8859-9:1989 (XMLHttpRequest)]
expected: FAIL


[single-byte-decoder.window.html?TextDecoder]

Expand Down
81 changes: 0 additions & 81 deletions tests/wpt/meta-legacy-layout/xhr/overridemimetype-blob.html.ini
Expand Up @@ -6,9 +6,6 @@
[Bogus MIME type should end up as application/octet-stream, 2]
expected: FAIL

[1) MIME types need to be parsed and serialized: text/html;charset=gbk]
expected: FAIL

[2) MIME types need to be parsed and serialized: TEXT/HTML;CHARSET=GBK]
expected: FAIL

Expand Down Expand Up @@ -204,9 +201,6 @@
[39) MIME types need to be parsed and serialized: x/x;test]
expected: FAIL

[16) MIME types need to be parsed and serialized: text/html;charset='gbk']
expected: FAIL

[63) MIME types need to be parsed and serialized: </>]
expected: FAIL

Expand Down Expand Up @@ -264,9 +258,6 @@
[26) MIME types need to be parsed and serialized: text/html;charset= ";charset=GBK]
expected: FAIL

[17) MIME types need to be parsed and serialized: text/html;charset='gbk]
expected: FAIL

[25) MIME types need to be parsed and serialized: text/html;;;;charset=gbk]
expected: FAIL

Expand Down Expand Up @@ -339,9 +330,6 @@
[48) MIME types need to be parsed and serialized: \n\r\t x/x;x=x\n\r\t ]
expected: FAIL

[31) MIME types need to be parsed and serialized: text/html;charset=" gbk"]
expected: FAIL

[49) MIME types need to be parsed and serialized: x/x;\n\r\t x=x\n\r\t ;x=y]
expected: FAIL

Expand All @@ -354,9 +342,6 @@
[12) MIME types need to be parsed and serialized: text/html;charset=\x0bgbk]
expected: FAIL

[32) MIME types need to be parsed and serialized: text/html;charset="gbk "]
expected: FAIL

[53) MIME types need to be parsed and serialized: \x0cx/x]
expected: FAIL

Expand All @@ -366,12 +351,6 @@
[24) MIME types need to be parsed and serialized: text/html ; ; charset=gbk]
expected: FAIL

[18) MIME types need to be parsed and serialized: text/html;charset=gbk']
expected: FAIL

[39) MIME types need to be parsed and serialized: text/html;0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789=x;charset=gbk]
expected: FAIL

[65) MIME types need to be parsed and serialized: ÿ/ÿ]
expected: FAIL

Expand Down Expand Up @@ -399,9 +378,6 @@
[41) MIME types need to be parsed and serialized: text/html;a\]=bar;b[=bar;c=bar]
expected: FAIL

[42) MIME types need to be parsed and serialized: text/html;valid=";";foo=bar]
expected: FAIL

[43) MIME types need to be parsed and serialized: text/html;in\]valid=";asd=foo";foo=bar]
expected: FAIL

Expand Down Expand Up @@ -438,62 +414,5 @@
[54) MIME types need to be parsed and serialized: x/x;test=�;x=x]
expected: FAIL

[55) MIME types need to be parsed and serialized: \x0bx/x]
expected: FAIL

[56) MIME types need to be parsed and serialized: \x0cx/x]
expected: FAIL

[57) MIME types need to be parsed and serialized: x/x\x0b]
expected: FAIL

[58) MIME types need to be parsed and serialized: x/x\x0c]
expected: FAIL

[59) MIME types need to be parsed and serialized: ]
expected: FAIL

[60) MIME types need to be parsed and serialized: \t]
expected: FAIL

[61) MIME types need to be parsed and serialized: /]
expected: FAIL

[62) MIME types need to be parsed and serialized: bogus]
expected: FAIL

[63) MIME types need to be parsed and serialized: bogus/]
expected: FAIL

[64) MIME types need to be parsed and serialized: bogus/ ]
expected: FAIL

[65) MIME types need to be parsed and serialized: bogus/bogus/;]
expected: FAIL

[66) MIME types need to be parsed and serialized: </>]
expected: FAIL

[67) MIME types need to be parsed and serialized: (/)]
expected: FAIL

[68) MIME types need to be parsed and serialized: ÿ/ÿ]
expected: FAIL

[69) MIME types need to be parsed and serialized: text/html(;doesnot=matter]
expected: FAIL

[70) MIME types need to be parsed and serialized: {/}]
expected: FAIL

[71) MIME types need to be parsed and serialized: Ā/Ā]
expected: FAIL

[72) MIME types need to be parsed and serialized: text /html]
expected: FAIL

[73) MIME types need to be parsed and serialized: text/ html]
expected: FAIL

[74) MIME types need to be parsed and serialized: "text/html"]
expected: FAIL
@@ -1,7 +1,3 @@
[overridemimetype-edge-cases.window.html]
[overrideMimeType() state needs to be reset across requests]
expected: FAIL

[Charset can be overridden by overrideMimeType() with a bogus charset]
expected: FAIL

This file was deleted.

27 changes: 0 additions & 27 deletions tests/wpt/meta/encoding/single-byte-decoder.window.js.ini
@@ -1,31 +1,4 @@
[single-byte-decoder.window.html?XMLHttpRequest]
[ISO-8859-2: iso_8859-2:1987 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-3: iso_8859-3:1988 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-4: iso_8859-4:1988 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-5: iso_8859-5:1988 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-6: iso_8859-6:1987 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-7: iso_8859-7:1987 (XMLHttpRequest)]
expected: FAIL

[ISO-8859-8: iso_8859-8:1988 (XMLHttpRequest)]
expected: FAIL

[windows-1252: iso_8859-1:1987 (XMLHttpRequest)]
expected: FAIL

[windows-1254: iso_8859-9:1989 (XMLHttpRequest)]
expected: FAIL


[single-byte-decoder.window.html?TextDecoder]

Expand Down

0 comments on commit a89bacb

Please sign in to comment.