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 
14 use crate::parser::{default_port, Context, Input, Parser, SchemeType};
15 use crate::{Host, ParseError, Position, Url};
16 
17 /// https://url.spec.whatwg.org/#dom-url-domaintoascii
domain_to_ascii(domain: &str) -> String18 pub fn domain_to_ascii(domain: &str) -> String {
19     match Host::parse(domain) {
20         Ok(Host::Domain(domain)) => domain,
21         _ => String::new(),
22     }
23 }
24 
25 /// https://url.spec.whatwg.org/#dom-url-domaintounicode
domain_to_unicode(domain: &str) -> String26 pub fn domain_to_unicode(domain: &str) -> String {
27     match Host::parse(domain) {
28         Ok(Host::Domain(ref domain)) => {
29             let (unicode, _errors) = idna::domain_to_unicode(domain);
30             unicode
31         }
32         _ => String::new(),
33     }
34 }
35 
36 /// Getter for https://url.spec.whatwg.org/#dom-url-href
href(url: &Url) -> &str37 pub fn href(url: &Url) -> &str {
38     url.as_str()
39 }
40 
41 /// Setter for https://url.spec.whatwg.org/#dom-url-href
set_href(url: &mut Url, value: &str) -> Result<(), ParseError>42 pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
43     *url = Url::parse(value)?;
44     Ok(())
45 }
46 
47 /// Getter for https://url.spec.whatwg.org/#dom-url-origin
origin(url: &Url) -> String48 pub fn origin(url: &Url) -> String {
49     url.origin().ascii_serialization()
50 }
51 
52 /// Getter for https://url.spec.whatwg.org/#dom-url-protocol
53 #[inline]
protocol(url: &Url) -> &str54 pub fn protocol(url: &Url) -> &str {
55     &url.as_str()[..url.scheme().len() + ":".len()]
56 }
57 
58 /// Setter for https://url.spec.whatwg.org/#dom-url-protocol
set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()>59 pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
60     // The scheme state in the spec ignores everything after the first `:`,
61     // but `set_scheme` errors if there is more.
62     if let Some(position) = new_protocol.find(':') {
63         new_protocol = &new_protocol[..position];
64     }
65     url.set_scheme(new_protocol)
66 }
67 
68 /// Getter for https://url.spec.whatwg.org/#dom-url-username
69 #[inline]
username(url: &Url) -> &str70 pub fn username(url: &Url) -> &str {
71     url.username()
72 }
73 
74 /// Setter for https://url.spec.whatwg.org/#dom-url-username
set_username(url: &mut Url, new_username: &str) -> Result<(), ()>75 pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
76     url.set_username(new_username)
77 }
78 
79 /// Getter for https://url.spec.whatwg.org/#dom-url-password
80 #[inline]
password(url: &Url) -> &str81 pub fn password(url: &Url) -> &str {
82     url.password().unwrap_or("")
83 }
84 
85 /// Setter for https://url.spec.whatwg.org/#dom-url-password
set_password(url: &mut Url, new_password: &str) -> Result<(), ()>86 pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> {
87     url.set_password(if new_password.is_empty() {
88         None
89     } else {
90         Some(new_password)
91     })
92 }
93 
94 /// Getter for https://url.spec.whatwg.org/#dom-url-host
95 #[inline]
host(url: &Url) -> &str96 pub fn host(url: &Url) -> &str {
97     &url[Position::BeforeHost..Position::AfterPort]
98 }
99 
100 /// Setter for https://url.spec.whatwg.org/#dom-url-host
set_host(url: &mut Url, new_host: &str) -> Result<(), ()>101 pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
102     // If context object’s url’s cannot-be-a-base-URL flag is set, then return.
103     if url.cannot_be_a_base() {
104         return Err(());
105     }
106     // Host parsing rules are strict,
107     // We don't want to trim the input
108     let input = Input::no_trim(new_host);
109     let host;
110     let opt_port;
111     {
112         let scheme = url.scheme();
113         let scheme_type = SchemeType::from(scheme);
114         if scheme_type == SchemeType::File && new_host.is_empty() {
115             url.set_host_internal(Host::Domain(String::new()), None);
116             return Ok(());
117         }
118 
119         if let Ok((h, remaining)) = Parser::parse_host(input, scheme_type) {
120             host = h;
121             opt_port = if let Some(remaining) = remaining.split_prefix(':') {
122                 if remaining.is_empty() {
123                     None
124                 } else {
125                     Parser::parse_port(remaining, || default_port(scheme), Context::Setter)
126                         .ok()
127                         .map(|(port, _remaining)| port)
128                 }
129             } else {
130                 None
131             };
132         } else {
133             return Err(());
134         }
135     }
136     // Make sure we won't set an empty host to a url with a username or a port
137     if host == Host::Domain("".to_string()) {
138         if !username(&url).is_empty() {
139             return Err(());
140         } else if let Some(Some(_)) = opt_port {
141             return Err(());
142         } else if url.port().is_some() {
143             return Err(());
144         }
145     }
146     url.set_host_internal(host, opt_port);
147     Ok(())
148 }
149 
150 /// Getter for https://url.spec.whatwg.org/#dom-url-hostname
151 #[inline]
hostname(url: &Url) -> &str152 pub fn hostname(url: &Url) -> &str {
153     url.host_str().unwrap_or("")
154 }
155 
156 /// Setter for https://url.spec.whatwg.org/#dom-url-hostname
set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()>157 pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
158     if url.cannot_be_a_base() {
159         return Err(());
160     }
161     // Host parsing rules are strict we don't want to trim the input
162     let input = Input::no_trim(new_hostname);
163     let scheme_type = SchemeType::from(url.scheme());
164     if scheme_type == SchemeType::File && new_hostname.is_empty() {
165         url.set_host_internal(Host::Domain(String::new()), None);
166         return Ok(());
167     }
168 
169     if let Ok((host, _remaining)) = Parser::parse_host(input, scheme_type) {
170         if let Host::Domain(h) = &host {
171             if h.is_empty() {
172                 // Empty host on special not file url
173                 if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
174                     // Port with an empty host
175                     ||!port(&url).is_empty()
176                     // Empty host that includes credentials
177                     || !url.username().is_empty()
178                     || !url.password().unwrap_or(&"").is_empty()
179                 {
180                     return Err(());
181                 }
182             }
183         }
184         url.set_host_internal(host, None);
185         Ok(())
186     } else {
187         Err(())
188     }
189 }
190 
191 /// Getter for https://url.spec.whatwg.org/#dom-url-port
192 #[inline]
port(url: &Url) -> &str193 pub fn port(url: &Url) -> &str {
194     &url[Position::BeforePort..Position::AfterPort]
195 }
196 
197 /// Setter for https://url.spec.whatwg.org/#dom-url-port
set_port(url: &mut Url, new_port: &str) -> Result<(), ()>198 pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
199     let result;
200     {
201         // has_host implies !cannot_be_a_base
202         let scheme = url.scheme();
203         if !url.has_host() || url.host() == Some(Host::Domain("")) || scheme == "file" {
204             return Err(());
205         }
206         result = Parser::parse_port(
207             Input::new(new_port),
208             || default_port(scheme),
209             Context::Setter,
210         )
211     }
212     if let Ok((new_port, _remaining)) = result {
213         url.set_port_internal(new_port);
214         Ok(())
215     } else {
216         Err(())
217     }
218 }
219 
220 /// Getter for https://url.spec.whatwg.org/#dom-url-pathname
221 #[inline]
pathname(url: &Url) -> &str222 pub fn pathname(url: &Url) -> &str {
223     url.path()
224 }
225 
226 /// Setter for https://url.spec.whatwg.org/#dom-url-pathname
set_pathname(url: &mut Url, new_pathname: &str)227 pub fn set_pathname(url: &mut Url, new_pathname: &str) {
228     if url.cannot_be_a_base() {
229         return;
230     }
231     if new_pathname.starts_with('/')
232         || (SchemeType::from(url.scheme()).is_special()
233             // \ is a segment delimiter for 'special' URLs"
234             && new_pathname.starts_with('\\'))
235     {
236         url.set_path(new_pathname)
237     } else {
238         let mut path_to_set = String::from("/");
239         path_to_set.push_str(new_pathname);
240         url.set_path(&path_to_set)
241     }
242 }
243 
244 /// Getter for https://url.spec.whatwg.org/#dom-url-search
search(url: &Url) -> &str245 pub fn search(url: &Url) -> &str {
246     trim(&url[Position::AfterPath..Position::AfterQuery])
247 }
248 
249 /// Setter for https://url.spec.whatwg.org/#dom-url-search
set_search(url: &mut Url, new_search: &str)250 pub fn set_search(url: &mut Url, new_search: &str) {
251     url.set_query(match new_search {
252         "" => None,
253         _ if new_search.starts_with('?') => Some(&new_search[1..]),
254         _ => Some(new_search),
255     })
256 }
257 
258 /// Getter for https://url.spec.whatwg.org/#dom-url-hash
hash(url: &Url) -> &str259 pub fn hash(url: &Url) -> &str {
260     trim(&url[Position::AfterQuery..])
261 }
262 
263 /// Setter for https://url.spec.whatwg.org/#dom-url-hash
set_hash(url: &mut Url, new_hash: &str)264 pub fn set_hash(url: &mut Url, new_hash: &str) {
265     url.set_fragment(match new_hash {
266         // If the given value is the empty string,
267         // then set context object’s url’s fragment to null and return.
268         "" => None,
269         // Let input be the given value with a single leading U+0023 (#) removed, if any.
270         _ if new_hash.starts_with('#') => Some(&new_hash[1..]),
271         _ => Some(new_hash),
272     })
273 }
274 
trim(s: &str) -> &str275 fn trim(s: &str) -> &str {
276     if s.len() == 1 {
277         ""
278     } else {
279         s
280     }
281 }
282