Lines Matching full:url

1 // Copyright 2016 The rust-url developers.
9 //! Getters and setters for URL components implemented per https://url.spec.whatwg.org/#api
12 //! you probably want to use `Url` method instead.
15 use crate::{Host, ParseError, Position, Url};
17 /// https://url.spec.whatwg.org/#dom-url-domaintoascii
25 /// https://url.spec.whatwg.org/#dom-url-domaintounicode
36 /// Getter for https://url.spec.whatwg.org/#dom-url-href
37 pub fn href(url: &Url) -> &str { in href()
38 url.as_str() in href()
41 /// Setter for https://url.spec.whatwg.org/#dom-url-href
42 pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> { in set_href() argument
43 *url = Url::parse(value)?; in set_href()
47 /// Getter for https://url.spec.whatwg.org/#dom-url-origin
48 pub fn origin(url: &Url) -> String { in origin()
49 url.origin().ascii_serialization() in origin()
52 /// Getter for https://url.spec.whatwg.org/#dom-url-protocol
54 pub fn protocol(url: &Url) -> &str { in protocol()
55 &url.as_str()[..url.scheme().len() + ":".len()] in protocol()
58 /// Setter for https://url.spec.whatwg.org/#dom-url-protocol
60 pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> { in set_protocol() argument
66 url.set_scheme(new_protocol) in set_protocol()
69 /// Getter for https://url.spec.whatwg.org/#dom-url-username
71 pub fn username(url: &Url) -> &str { in username()
72 url.username() in username()
75 /// Setter for https://url.spec.whatwg.org/#dom-url-username
77 pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> { in set_username() argument
78 url.set_username(new_username) in set_username()
81 /// Getter for https://url.spec.whatwg.org/#dom-url-password
83 pub fn password(url: &Url) -> &str { in password()
84 url.password().unwrap_or("") in password()
87 /// Setter for https://url.spec.whatwg.org/#dom-url-password
89 pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> { in set_password() argument
90 url.set_password(if new_password.is_empty() { in set_password()
97 /// Getter for https://url.spec.whatwg.org/#dom-url-host
99 pub fn host(url: &Url) -> &str { in host()
100 &url[Position::BeforeHost..Position::AfterPort] in host()
103 /// Setter for https://url.spec.whatwg.org/#dom-url-host
105 pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> { in set_host() argument
106 // If context object’s url’s cannot-be-a-base-URL flag is set, then return. in set_host()
107 if url.cannot_be_a_base() { in set_host()
116 let scheme = url.scheme(); in set_host()
119 url.set_host_internal(Host::Domain(String::new()), None); in set_host()
140 // Make sure we won't set an empty host to a url with a username or a port in set_host()
142 if !username(&url).is_empty() { in set_host()
146 } else if url.port().is_some() { in set_host()
150 url.set_host_internal(host, opt_port); in set_host()
154 /// Getter for https://url.spec.whatwg.org/#dom-url-hostname
156 pub fn hostname(url: &Url) -> &str { in hostname()
157 url.host_str().unwrap_or("") in hostname()
160 /// Setter for https://url.spec.whatwg.org/#dom-url-hostname
162 pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> { in set_hostname() argument
163 if url.cannot_be_a_base() { in set_hostname()
168 let scheme_type = SchemeType::from(url.scheme()); in set_hostname()
170 url.set_host_internal(Host::Domain(String::new()), None); in set_hostname()
177 // Empty host on special not file url in set_hostname()
178 if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile in set_hostname()
180 ||!port(&url).is_empty() in set_hostname()
182 || !url.username().is_empty() in set_hostname()
183 || !url.password().unwrap_or(&"").is_empty() in set_hostname()
189 url.set_host_internal(host, None); in set_hostname()
196 /// Getter for https://url.spec.whatwg.org/#dom-url-port
198 pub fn port(url: &Url) -> &str { in port()
199 &url[Position::BeforePort..Position::AfterPort] in port()
202 /// Setter for https://url.spec.whatwg.org/#dom-url-port
204 pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> { in set_port() argument
208 let scheme = url.scheme(); in set_port()
209 if !url.has_host() || url.host() == Some(Host::Domain("")) || scheme == "file" { in set_port()
219 url.set_port_internal(new_port); in set_port()
226 /// Getter for https://url.spec.whatwg.org/#dom-url-pathname
228 pub fn pathname(url: &Url) -> &str { in pathname()
229 url.path() in pathname()
232 /// Setter for https://url.spec.whatwg.org/#dom-url-pathname
233 pub fn set_pathname(url: &mut Url, new_pathname: &str) { in set_pathname() argument
234 if url.cannot_be_a_base() { in set_pathname()
238 || (SchemeType::from(url.scheme()).is_special() in set_pathname()
242 url.set_path(new_pathname) in set_pathname()
246 url.set_path(&path_to_set) in set_pathname()
250 /// Getter for https://url.spec.whatwg.org/#dom-url-search
251 pub fn search(url: &Url) -> &str { in search()
252 trim(&url[Position::AfterPath..Position::AfterQuery]) in search()
255 /// Setter for https://url.spec.whatwg.org/#dom-url-search
256 pub fn set_search(url: &mut Url, new_search: &str) { in set_search() argument
257 url.set_query(match new_search { in set_search()
264 /// Getter for https://url.spec.whatwg.org/#dom-url-hash
265 pub fn hash(url: &Url) -> &str { in hash()
266 trim(&url[Position::AfterQuery..]) in hash()
269 /// Setter for https://url.spec.whatwg.org/#dom-url-hash
270 pub fn set_hash(url: &mut Url, new_hash: &str) { in set_hash() argument
271 url.set_fragment(match new_hash { in set_hash()
273 // then set context object’s url’s fragment to null and return. in set_hash()