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 /// Internal components / offsets of a URL.
42 /// Internal component / parsed offsets of the URL.
45 /// for the URL.
47 pub fn internal_components(url: &Url) -> InternalComponents { in internal_components()
49 scheme_end: url.scheme_end, in internal_components()
50 username_end: url.username_end, in internal_components()
51 host_start: url.host_start, in internal_components()
52 host_end: url.host_end, in internal_components()
53 port: url.port, in internal_components()
54 path_start: url.path_start, in internal_components()
55 query_start: url.query_start, in internal_components()
56 fragment_start: url.fragment_start, in internal_components()
60 /// <https://url.spec.whatwg.org/#dom-url-domaintoascii>
68 /// <https://url.spec.whatwg.org/#dom-url-domaintounicode>
79 /// Getter for <https://url.spec.whatwg.org/#dom-url-href>
80 pub fn href(url: &Url) -> &str { in href()
81 url.as_str() in href()
84 /// Setter for <https://url.spec.whatwg.org/#dom-url-href>
85 pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> { in set_href() argument
86 *url = Url::parse(value)?; in set_href()
90 /// Getter for <https://url.spec.whatwg.org/#dom-url-origin>
91 pub fn origin(url: &Url) -> String { in origin()
92 url.origin().ascii_serialization() in origin()
95 /// Getter for <https://url.spec.whatwg.org/#dom-url-protocol>
97 pub fn protocol(url: &Url) -> &str { in protocol()
98 &url.as_str()[..url.scheme().len() + ":".len()] in protocol()
101 /// Setter for <https://url.spec.whatwg.org/#dom-url-protocol>
103 pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> { in set_protocol() argument
109 url.set_scheme(new_protocol) in set_protocol()
112 /// Getter for <https://url.spec.whatwg.org/#dom-url-username>
114 pub fn username(url: &Url) -> &str { in username()
115 url.username() in username()
118 /// Setter for <https://url.spec.whatwg.org/#dom-url-username>
120 pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> { in set_username() argument
121 url.set_username(new_username) in set_username()
124 /// Getter for <https://url.spec.whatwg.org/#dom-url-password>
126 pub fn password(url: &Url) -> &str { in password()
127 url.password().unwrap_or("") in password()
130 /// Setter for <https://url.spec.whatwg.org/#dom-url-password>
132 pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> { in set_password() argument
133 url.set_password(if new_password.is_empty() { in set_password()
140 /// Getter for <https://url.spec.whatwg.org/#dom-url-host>
142 pub fn host(url: &Url) -> &str { in host()
143 &url[Position::BeforeHost..Position::AfterPort] in host()
146 /// Setter for <https://url.spec.whatwg.org/#dom-url-host>
148 pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> { in set_host() argument
149 // If context object’s url’s cannot-be-a-base-URL flag is set, then return. in set_host()
150 if url.cannot_be_a_base() { in set_host()
159 let scheme = url.scheme(); in set_host()
162 url.set_host_internal(Host::Domain(String::new()), None); in set_host()
183 // Make sure we won't set an empty host to a url with a username or a port in set_host()
185 && (!username(url).is_empty() || matches!(opt_port, Some(Some(_))) || url.port().is_some()) in set_host()
189 url.set_host_internal(host, opt_port); in set_host()
193 /// Getter for <https://url.spec.whatwg.org/#dom-url-hostname>
195 pub fn hostname(url: &Url) -> &str { in hostname()
196 url.host_str().unwrap_or("") in hostname()
199 /// Setter for <https://url.spec.whatwg.org/#dom-url-hostname>
201 pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> { in set_hostname() argument
202 if url.cannot_be_a_base() { in set_hostname()
207 let scheme_type = SchemeType::from(url.scheme()); in set_hostname()
209 url.set_host_internal(Host::Domain(String::new()), None); in set_hostname()
216 // Empty host on special not file url in set_hostname()
217 if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile in set_hostname()
219 ||!port(url).is_empty() in set_hostname()
221 || !url.username().is_empty() in set_hostname()
222 || !url.password().unwrap_or("").is_empty() in set_hostname()
228 url.set_host_internal(host, None); in set_hostname()
235 /// Getter for <https://url.spec.whatwg.org/#dom-url-port>
237 pub fn port(url: &Url) -> &str { in port()
238 &url[Position::BeforePort..Position::AfterPort] in port()
241 /// Setter for <https://url.spec.whatwg.org/#dom-url-port>
243 pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> { in set_port() argument
247 let scheme = url.scheme(); in set_port()
248 if !url.has_host() || url.host() == Some(Host::Domain("")) || scheme == "file" { in set_port()
258 url.set_port_internal(new_port); in set_port()
265 /// Getter for <https://url.spec.whatwg.org/#dom-url-pathname>
267 pub fn pathname(url: &Url) -> &str { in pathname()
268 url.path() in pathname()
271 /// Setter for <https://url.spec.whatwg.org/#dom-url-pathname>
272 pub fn set_pathname(url: &mut Url, new_pathname: &str) { in set_pathname() argument
273 if url.cannot_be_a_base() { in set_pathname()
277 || (SchemeType::from(url.scheme()).is_special() in set_pathname()
281 url.set_path(new_pathname) in set_pathname()
282 } else if SchemeType::from(url.scheme()).is_special() in set_pathname()
284 || !url.has_host() in set_pathname()
288 url.set_path(&path_to_set) in set_pathname()
290 url.set_path(new_pathname) in set_pathname()
294 /// Getter for <https://url.spec.whatwg.org/#dom-url-search>
295 pub fn search(url: &Url) -> &str { in search()
296 trim(&url[Position::AfterPath..Position::AfterQuery]) in search()
299 /// Setter for <https://url.spec.whatwg.org/#dom-url-search>
300 pub fn set_search(url: &mut Url, new_search: &str) { in set_search() argument
301 url.set_query(match new_search { in set_search()
308 /// Getter for <https://url.spec.whatwg.org/#dom-url-hash>
309 pub fn hash(url: &Url) -> &str { in hash()
310 trim(&url[Position::AfterQuery..]) in hash()
313 /// Setter for <https://url.spec.whatwg.org/#dom-url-hash>
314 pub fn set_hash(url: &mut Url, new_hash: &str) { in set_hash() argument
315 url.set_fragment(match new_hash { in set_hash()
317 // then set context object’s url’s fragment to null and return. in set_hash()