Auto merge of #9972 - dagnir:xhr-data, r=KiChjang

Xhr data

Builds on existing work by @emosenkis.  Fixes #8015.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9972)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-04-03 20:45:36 +05:30
commit d35ae3beb7
4 changed files with 22 additions and 16 deletions

View file

@ -336,9 +336,18 @@ impl Metadata {
/// Extract the parts of a Mime that we care about. /// Extract the parts of a Mime that we care about.
pub fn set_content_type(&mut self, content_type: Option<&Mime>) { pub fn set_content_type(&mut self, content_type: Option<&Mime>) {
match self.headers {
None => self.headers = Some(Headers::new()),
Some(_) => (),
}
match content_type { match content_type {
None => (), None => (),
Some(mime) => { Some(mime) => {
if let Some(headers) = self.headers.as_mut() {
headers.set(ContentType(mime.clone()));
}
self.content_type = Some(ContentType(mime.clone())); self.content_type = Some(ContentType(mime.clone()));
let &Mime(_, _, ref parameters) = mime; let &Mime(_, _, ref parameters) = mime;
for &(ref k, ref v) in parameters { for &(ref k, ref v) in parameters {

View file

@ -64,15 +64,21 @@ impl CORSRequest {
destination: Url, destination: Url,
mode: RequestMode, mode: RequestMode,
method: Method, method: Method,
headers: Headers) headers: Headers,
same_origin_data_url_flag: bool)
-> Result<Option<CORSRequest>, ()> { -> Result<Option<CORSRequest>, ()> {
if referer.scheme == destination.scheme && referer.host() == destination.host() && if referer.scheme == destination.scheme && referer.host() == destination.host() &&
referer.port() == destination.port() { referer.port() == destination.port() {
return Ok(None); // Not cross-origin, proceed with a normal fetch return Ok(None); // Not cross-origin, proceed with a normal fetch
} }
match &*destination.scheme { match &*destination.scheme {
// TODO: If the request's same origin data url flag is set (which isn't the case for XHR) // As per (https://fetch.spec.whatwg.org/#main-fetch 5.1.9), about URLs can be fetched
// we can fetch a data URL normally. about:blank can also be fetched by XHR // the same as a basic request.
"about" if destination.path() == Some(&["blank".to_owned()]) => Ok(None),
// As per (https://fetch.spec.whatwg.org/#main-fetch 5.1.9), data URLs can be fetched
// the same as a basic request if the request's method is GET and the
// same-origin data-URL flag is set.
"data" if same_origin_data_url_flag && method == Method::Get => Ok(None),
"http" | "https" => { "http" | "https" => {
let mut req = CORSRequest::new(referer, destination, mode, method, headers); let mut req = CORSRequest::new(referer, destination, mode, method, headers);
req.preflight_flag = !is_simple_method(&req.method) || req.preflight_flag = !is_simple_method(&req.method) ||

View file

@ -626,7 +626,8 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
load_data.url.clone(), load_data.url.clone(),
mode, mode,
load_data.method.clone(), load_data.method.clone(),
combined_headers); combined_headers,
true);
match cors_request { match cors_request {
Ok(None) => { Ok(None) => {
let mut buf = String::new(); let mut buf = String::new();
@ -1301,7 +1302,8 @@ impl XMLHttpRequest {
global: GlobalRef) -> ErrorResult { global: GlobalRef) -> ErrorResult {
let cors_request = match cors_request { let cors_request = match cors_request {
Err(_) => { Err(_) => {
// Happens in case of cross-origin non-http URIs // Happens in case of unsupported cross-origin URI schemes.
// Supported schemes are http, https, data, and about.
self.process_partial_response(XHRProgress::Errored( self.process_partial_response(XHRProgress::Errored(
self.generation_id.get(), Error::Network)); self.generation_id.get(), Error::Network));
return Err(Error::Network); return Err(Error::Network);

View file

@ -1,16 +1,5 @@
[data-uri.htm] [data-uri.htm]
type: testharness type: testharness
[XHR method GET with charset text/plain]
expected: FAIL
[XHR method GET with charset text/plain (base64)]
expected: FAIL
[XHR method GET with charset text/html]
expected: FAIL
[XHR method GET with charset image/png]
expected: FAIL
[XHR method GET with charset text/html;charset=UTF-8] [XHR method GET with charset text/html;charset=UTF-8]
expected: FAIL expected: FAIL