Implement step 9 of XHR open method

Implement step 9 of the Open method for XMLHttpRequest.
This commit is contained in:
Daniel Robertson 2016-02-13 01:00:23 +00:00
parent 3f74c07e20
commit 10e7af50fe

View file

@ -60,6 +60,7 @@ use string_cache::Atom;
use time; use time;
use timers::{ScheduledCallback, TimerHandle}; use timers::{ScheduledCallback, TimerHandle};
use url::Url; use url::Url;
use url::percent_encoding::{utf8_percent_encode, USERNAME_ENCODE_SET, PASSWORD_ENCODE_SET};
use util::str::DOMString; use util::str::DOMString;
pub type SendParam = BlobOrStringOrURLSearchParams; pub type SendParam = BlobOrStringOrURLSearchParams;
@ -293,7 +294,9 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
} }
// https://xhr.spec.whatwg.org/#the-open()-method // https://xhr.spec.whatwg.org/#the-open()-method
fn Open(&self, method: ByteString, url: USVString) -> ErrorResult { fn Open_(&self, method: ByteString, url: USVString, async: bool,
username: Option<USVString>, password: Option<USVString>) -> ErrorResult {
self.sync.set(!async);
//FIXME(seanmonstar): use a Trie instead? //FIXME(seanmonstar): use a Trie instead?
let maybe_method = method.as_str().and_then(|s| { let maybe_method = method.as_str().and_then(|s| {
// Note: hyper tests against the uppercase versions // Note: hyper tests against the uppercase versions
@ -324,10 +327,11 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
// Step 6 // Step 6
let base = self.global().r().get_url(); let base = self.global().r().get_url();
let parsed_url = match base.join(&url.0) { let mut parsed_url = match base.join(&url.0) {
Ok(parsed) => parsed, Ok(parsed) => parsed,
Err(_) => return Err(Error::Syntax) // Step 7 Err(_) => return Err(Error::Syntax) // Step 7
}; };
// XXXManishearth Do some handling of username/passwords // XXXManishearth Do some handling of username/passwords
if self.sync.get() { if self.sync.get() {
// FIXME: This should only happen if the global environment is a document environment // FIXME: This should only happen if the global environment is a document environment
@ -336,6 +340,21 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
return Err(Error::InvalidAccess) return Err(Error::InvalidAccess)
} }
} }
if parsed_url.host().is_some() {
if let Some(scheme_data) = parsed_url.relative_scheme_data_mut() {
if let Some(user_str) = username {
scheme_data.username = utf8_percent_encode(&user_str.0, USERNAME_ENCODE_SET);
// ensure that the password is mutated when a username is provided
scheme_data.password = match password {
Some(pass_str) => Some(utf8_percent_encode(&pass_str.0, PASSWORD_ENCODE_SET)),
None => None
}
}
}
}
// abort existing requests // abort existing requests
self.terminate_ongoing_fetch(); self.terminate_ongoing_fetch();
@ -359,10 +378,8 @@ impl XMLHttpRequestMethods for XMLHttpRequest {
} }
// https://xhr.spec.whatwg.org/#the-open()-method // https://xhr.spec.whatwg.org/#the-open()-method
fn Open_(&self, method: ByteString, url: USVString, async: bool, fn Open(&self, method: ByteString, url: USVString) -> ErrorResult {
_username: Option<USVString>, _password: Option<USVString>) -> ErrorResult { self.Open_(method, url, true, None, None)
self.sync.set(!async);
self.Open(method, url)
} }
// https://xhr.spec.whatwg.org/#the-setrequestheader()-method // https://xhr.spec.whatwg.org/#the-setrequestheader()-method