url/
quirks.rs

1// Copyright 2016 The rust-url developers.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Getters and setters for URL components implemented per <https://url.spec.whatwg.org/#api>
10//!
11//! Unless you need to be interoperable with web browsers,
12//! you probably want to use `Url` method instead.
13
14use crate::parser::{default_port, Context, Input, Parser, SchemeType};
15use crate::{Host, ParseError, Position, Url};
16use alloc::string::String;
17use alloc::string::ToString;
18
19/// Internal components / offsets of a URL.
20///
21/// https://user@pass:example.com:1234/foo/bar?baz#quux
22///      |      |    |          | ^^^^|       |   |
23///      |      |    |          | |   |       |   `----- fragment_start
24///      |      |    |          | |   |       `--------- query_start
25///      |      |    |          | |   `----------------- path_start
26///      |      |    |          | `--------------------- port
27///      |      |    |          `----------------------- host_end
28///      |      |    `---------------------------------- host_start
29///      |      `--------------------------------------- username_end
30///      `---------------------------------------------- scheme_end
31#[derive(Copy, Clone)]
32#[cfg(feature = "expose_internals")]
33pub struct InternalComponents {
34    pub scheme_end: u32,
35    pub username_end: u32,
36    pub host_start: u32,
37    pub host_end: u32,
38    pub port: Option<u16>,
39    pub path_start: u32,
40    pub query_start: Option<u32>,
41    pub fragment_start: Option<u32>,
42}
43
44/// Internal component / parsed offsets of the URL.
45///
46/// This can be useful for implementing efficient serialization
47/// for the URL.
48#[cfg(feature = "expose_internals")]
49pub fn internal_components(url: &Url) -> InternalComponents {
50    InternalComponents {
51        scheme_end: url.scheme_end,
52        username_end: url.username_end,
53        host_start: url.host_start,
54        host_end: url.host_end,
55        port: url.port,
56        path_start: url.path_start,
57        query_start: url.query_start,
58        fragment_start: url.fragment_start,
59    }
60}
61
62/// <https://url.spec.whatwg.org/#dom-url-domaintoascii>
63pub fn domain_to_ascii(domain: &str) -> String {
64    match Host::parse(domain) {
65        Ok(Host::Domain(domain)) => domain,
66        _ => String::new(),
67    }
68}
69
70/// <https://url.spec.whatwg.org/#dom-url-domaintounicode>
71pub fn domain_to_unicode(domain: &str) -> String {
72    match Host::parse(domain) {
73        Ok(Host::Domain(ref domain)) => {
74            let (unicode, _errors) = idna::domain_to_unicode(domain);
75            unicode
76        }
77        _ => String::new(),
78    }
79}
80
81/// Getter for <https://url.spec.whatwg.org/#dom-url-href>
82pub fn href(url: &Url) -> &str {
83    url.as_str()
84}
85
86/// Setter for <https://url.spec.whatwg.org/#dom-url-href>
87pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
88    *url = Url::parse(value)?;
89    Ok(())
90}
91
92/// Getter for <https://url.spec.whatwg.org/#dom-url-origin>
93pub fn origin(url: &Url) -> String {
94    url.origin().ascii_serialization()
95}
96
97/// Getter for <https://url.spec.whatwg.org/#dom-url-protocol>
98#[inline]
99pub fn protocol(url: &Url) -> &str {
100    &url.as_str()[..url.scheme().len() + ":".len()]
101}
102
103/// Setter for <https://url.spec.whatwg.org/#dom-url-protocol>
104#[allow(clippy::result_unit_err)]
105pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
106    // The scheme state in the spec ignores everything after the first `:`,
107    // but `set_scheme` errors if there is more.
108    if let Some(position) = new_protocol.find(':') {
109        new_protocol = &new_protocol[..position];
110    }
111    url.set_scheme(new_protocol)
112}
113
114/// Getter for <https://url.spec.whatwg.org/#dom-url-username>
115#[inline]
116pub fn username(url: &Url) -> &str {
117    url.username()
118}
119
120/// Setter for <https://url.spec.whatwg.org/#dom-url-username>
121#[allow(clippy::result_unit_err)]
122pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
123    url.set_username(new_username)
124}
125
126/// Getter for <https://url.spec.whatwg.org/#dom-url-password>
127#[inline]
128pub fn password(url: &Url) -> &str {
129    url.password().unwrap_or("")
130}
131
132/// Setter for <https://url.spec.whatwg.org/#dom-url-password>
133#[allow(clippy::result_unit_err)]
134pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> {
135    url.set_password(if new_password.is_empty() {
136        None
137    } else {
138        Some(new_password)
139    })
140}
141
142/// Getter for <https://url.spec.whatwg.org/#dom-url-host>
143#[inline]
144pub fn host(url: &Url) -> &str {
145    &url[Position::BeforeHost..Position::AfterPort]
146}
147
148/// Setter for <https://url.spec.whatwg.org/#dom-url-host>
149#[allow(clippy::result_unit_err)]
150pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
151    // If context object’s url’s cannot-be-a-base-URL flag is set, then return.
152    if url.cannot_be_a_base() {
153        return Err(());
154    }
155    // Host parsing rules are strict,
156    // We don't want to trim the input
157    let input = Input::new_no_trim(new_host);
158    let host;
159    let opt_port;
160    {
161        let scheme = url.scheme();
162        let scheme_type = SchemeType::from(scheme);
163        if scheme_type == SchemeType::File && new_host.is_empty() {
164            url.set_host_internal(Host::Domain("".into()), None);
165            return Ok(());
166        }
167
168        if let Ok((h, remaining)) = Parser::parse_host(input, scheme_type) {
169            host = h;
170            opt_port = if let Some(remaining) = remaining.split_prefix(':') {
171                if remaining.is_empty() {
172                    None
173                } else {
174                    Parser::parse_port(remaining, || default_port(scheme), Context::Setter)
175                        .ok()
176                        .map(|(port, _remaining)| port)
177                }
178            } else {
179                None
180            };
181        } else {
182            return Err(());
183        }
184    }
185    // Make sure we won't set an empty host to a url with a username or a port
186    if host == Host::Domain("".to_string())
187        && (!username(url).is_empty() || matches!(opt_port, Some(Some(_))) || url.port().is_some())
188    {
189        return Err(());
190    }
191    url.set_host_internal(host, opt_port);
192    Ok(())
193}
194
195/// Getter for <https://url.spec.whatwg.org/#dom-url-hostname>
196#[inline]
197pub fn hostname(url: &Url) -> &str {
198    url.host_str().unwrap_or("")
199}
200
201/// Setter for <https://url.spec.whatwg.org/#dom-url-hostname>
202#[allow(clippy::result_unit_err)]
203pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
204    if url.cannot_be_a_base() {
205        return Err(());
206    }
207    // Host parsing rules are strict we don't want to trim the input
208    let input = Input::new_no_trim(new_hostname);
209    let scheme_type = SchemeType::from(url.scheme());
210    if scheme_type == SchemeType::File && new_hostname.is_empty() {
211        url.set_host_internal(Host::Domain("".into()), None);
212        return Ok(());
213    }
214
215    if let Ok((host, remaining)) = Parser::parse_host(input, scheme_type) {
216        if remaining.starts_with(':') {
217            return Err(());
218        };
219        if let Host::Domain(h) = &host {
220            if h.is_empty() {
221                // Empty host on special not file url
222                if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
223                    // Port with an empty host
224                    ||!port(url).is_empty()
225                    // Empty host that includes credentials
226                    || !url.username().is_empty()
227                    || !url.password().unwrap_or("").is_empty()
228                {
229                    return Err(());
230                }
231            }
232        }
233        url.set_host_internal(host, None);
234        Ok(())
235    } else {
236        Err(())
237    }
238}
239
240/// Getter for <https://url.spec.whatwg.org/#dom-url-port>
241#[inline]
242pub fn port(url: &Url) -> &str {
243    &url[Position::BeforePort..Position::AfterPort]
244}
245
246/// Setter for <https://url.spec.whatwg.org/#dom-url-port>
247#[allow(clippy::result_unit_err)]
248pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
249    let result;
250    {
251        // has_host implies !cannot_be_a_base
252        let scheme = url.scheme();
253        if !url.has_host() || url.host() == Some(Host::Domain("")) || scheme == "file" {
254            return Err(());
255        }
256        result = Parser::parse_port(
257            Input::new_no_trim(new_port),
258            || default_port(scheme),
259            Context::Setter,
260        )
261    }
262    if let Ok((new_port, _remaining)) = result {
263        url.set_port_internal(new_port);
264        Ok(())
265    } else {
266        Err(())
267    }
268}
269
270/// Getter for <https://url.spec.whatwg.org/#dom-url-pathname>
271#[inline]
272pub fn pathname(url: &Url) -> &str {
273    url.path()
274}
275
276/// Setter for <https://url.spec.whatwg.org/#dom-url-pathname>
277pub fn set_pathname(url: &mut Url, new_pathname: &str) {
278    if url.cannot_be_a_base() {
279        return;
280    }
281    if new_pathname.starts_with('/')
282        || (SchemeType::from(url.scheme()).is_special()
283            // \ is a segment delimiter for 'special' URLs"
284            && new_pathname.starts_with('\\'))
285    {
286        url.set_path(new_pathname)
287    } else if SchemeType::from(url.scheme()).is_special()
288        || !new_pathname.is_empty()
289        || !url.has_host()
290    {
291        let mut path_to_set = String::from("/");
292        path_to_set.push_str(new_pathname);
293        url.set_path(&path_to_set)
294    } else {
295        url.set_path(new_pathname)
296    }
297}
298
299/// Getter for <https://url.spec.whatwg.org/#dom-url-search>
300pub fn search(url: &Url) -> &str {
301    trim(&url[Position::AfterPath..Position::AfterQuery])
302}
303
304/// Setter for <https://url.spec.whatwg.org/#dom-url-search>
305pub fn set_search(url: &mut Url, new_search: &str) {
306    url.set_query(match new_search {
307        "" => None,
308        _ if new_search.starts_with('?') => Some(&new_search[1..]),
309        _ => Some(new_search),
310    })
311}
312
313/// Getter for <https://url.spec.whatwg.org/#dom-url-hash>
314pub fn hash(url: &Url) -> &str {
315    trim(&url[Position::AfterQuery..])
316}
317
318/// Setter for <https://url.spec.whatwg.org/#dom-url-hash>
319pub fn set_hash(url: &mut Url, new_hash: &str) {
320    url.set_fragment(match new_hash {
321        // If the given value is the empty string,
322        // then set context object’s url’s fragment to null and return.
323        "" => None,
324        // Let input be the given value with a single leading U+0023 (#) removed, if any.
325        _ if new_hash.starts_with('#') => Some(&new_hash[1..]),
326        _ => Some(new_hash),
327    })
328}
329
330fn trim(s: &str) -> &str {
331    if s.len() == 1 {
332        ""
333    } else {
334        s
335    }
336}