mirror of
https://github.com/servo/servo.git
synced 2025-08-05 05:30:08 +01:00
add origin to location and url api
This commit is contained in:
parent
1ba1fb0b7f
commit
1ee9ccba21
11 changed files with 62 additions and 607 deletions
|
@ -81,6 +81,11 @@ impl LocationMethods for Location {
|
|||
self.set_url_component(value, UrlHelper::SetHost);
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-location-origin
|
||||
fn Origin(&self) -> USVString {
|
||||
UrlHelper::Origin(&self.get_url())
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-location-hostname
|
||||
fn Hostname(&self) -> USVString {
|
||||
UrlHelper::Hostname(&self.get_url())
|
||||
|
|
|
@ -176,6 +176,11 @@ impl URLMethods for URL {
|
|||
UrlHelper::SetProtocol(&mut self.url.borrow_mut(), value);
|
||||
}
|
||||
|
||||
// https://url.spec.whatwg.org/#dom-url-origin
|
||||
fn Origin(&self) -> USVString {
|
||||
UrlHelper::Origin(&self.url.borrow())
|
||||
}
|
||||
|
||||
// https://url.spec.whatwg.org/#dom-url-search
|
||||
fn Search(&self) -> USVString {
|
||||
UrlHelper::Search(&self.url.borrow())
|
||||
|
|
|
@ -6,7 +6,7 @@ use dom::bindings::str::USVString;
|
|||
use std::borrow::ToOwned;
|
||||
use std::fmt::Write;
|
||||
use url::urlutils::{UrlUtils, UrlUtilsWrapper};
|
||||
use url::{SchemeData, Url, UrlParser};
|
||||
use url::{Origin, SchemeData, Url, UrlParser};
|
||||
|
||||
#[derive(HeapSizeOf)]
|
||||
pub struct UrlHelper;
|
||||
|
@ -43,6 +43,34 @@ impl UrlHelper {
|
|||
let _ = wrapper.set_host(&value.0);
|
||||
}
|
||||
|
||||
pub fn Origin(url: &Url) -> USVString {
|
||||
USVString(match url.origin() {
|
||||
Origin::UID(_) => {
|
||||
// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
|
||||
// If the origin in question is not a scheme/host/port tuple,
|
||||
// then return the literal string "null" and abort these steps.
|
||||
"null".to_owned()
|
||||
},
|
||||
Origin::Tuple(protocol, host, _) => {
|
||||
let mut origin =
|
||||
format!(
|
||||
"{protocol}://{host}",
|
||||
protocol = protocol,
|
||||
host = host
|
||||
);
|
||||
if let Some(port) =
|
||||
// https://html.spec.whatwg.org/multipage/#unicode-serialisation-of-an-origin
|
||||
// only append the port # to the serialized origin if the port is different from
|
||||
// the default port for the protocol. If url.scheme_data.port is None, that
|
||||
// indicates that the port is a default port
|
||||
url.relative_scheme_data().and_then(|scheme| scheme.port) {
|
||||
write!(origin, ":{}", port).unwrap();
|
||||
};
|
||||
origin
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn Hostname(url: &Url) -> USVString {
|
||||
USVString(url.serialize_host().unwrap_or_else(|| "".to_owned()))
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
// https://html.spec.whatwg.org/multipage/#location
|
||||
[Unforgeable] interface Location {
|
||||
/*stringifier*/ attribute USVString href;
|
||||
// attribute USVString origin;
|
||||
readonly attribute USVString origin;
|
||||
attribute USVString protocol;
|
||||
attribute USVString host;
|
||||
attribute USVString hostname;
|
||||
|
|
|
@ -11,7 +11,7 @@ interface URL {
|
|||
|
||||
[SetterThrows]
|
||||
/*stringifier*/ attribute USVString href;
|
||||
// readonly attribute USVString origin;
|
||||
readonly attribute USVString origin;
|
||||
attribute USVString protocol;
|
||||
attribute USVString username;
|
||||
attribute USVString password;
|
||||
|
|
|
@ -5,8 +5,10 @@
|
|||
use dom::bindings::cell::DOMRefCell;
|
||||
use dom::bindings::codegen::Bindings::BlobBinding::BlobMethods;
|
||||
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
|
||||
use dom::bindings::codegen::Bindings::LocationBinding::LocationMethods;
|
||||
use dom::bindings::codegen::Bindings::WebSocketBinding;
|
||||
use dom::bindings::codegen::Bindings::WebSocketBinding::{BinaryType, WebSocketMethods};
|
||||
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
|
||||
use dom::bindings::codegen::UnionTypes::StringOrStringSequence::{self, eString, eStringSequence};
|
||||
use dom::bindings::conversions::{ToJSValConvertible};
|
||||
use dom::bindings::error::{Error, Fallible};
|
||||
|
@ -234,13 +236,12 @@ impl WebSocket {
|
|||
}
|
||||
|
||||
// Step 6: Origin.
|
||||
let origin = global.as_window().Location().Origin().0;
|
||||
|
||||
// Step 7.
|
||||
let ws = WebSocket::new(global, resource_url.clone());
|
||||
let address = Trusted::new(ws.r(), global.networking_thread_source());
|
||||
|
||||
let origin = global.get_url().serialize();
|
||||
|
||||
let connect_data = WebSocketConnectData {
|
||||
resource_url: resource_url.clone(),
|
||||
origin: origin,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue