1use crate::parser::{default_port, Context, Input, Parser, SchemeType};
15use crate::{Host, ParseError, Position, Url};
16use alloc::string::String;
17use alloc::string::ToString;
18
19#[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#[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
62pub fn domain_to_ascii(domain: &str) -> String {
64 match Host::parse(domain) {
65 Ok(Host::Domain(domain)) => domain,
66 _ => String::new(),
67 }
68}
69
70pub 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
81pub fn href(url: &Url) -> &str {
83 url.as_str()
84}
85
86pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
88 *url = Url::parse(value)?;
89 Ok(())
90}
91
92pub fn origin(url: &Url) -> String {
94 url.origin().ascii_serialization()
95}
96
97#[inline]
99pub fn protocol(url: &Url) -> &str {
100 &url.as_str()[..url.scheme().len() + ":".len()]
101}
102
103#[allow(clippy::result_unit_err)]
105pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
106 if let Some(position) = new_protocol.find(':') {
109 new_protocol = &new_protocol[..position];
110 }
111 url.set_scheme(new_protocol)
112}
113
114#[inline]
116pub fn username(url: &Url) -> &str {
117 url.username()
118}
119
120#[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#[inline]
128pub fn password(url: &Url) -> &str {
129 url.password().unwrap_or("")
130}
131
132#[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#[inline]
144pub fn host(url: &Url) -> &str {
145 &url[Position::BeforeHost..Position::AfterPort]
146}
147
148#[allow(clippy::result_unit_err)]
150pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
151 if url.cannot_be_a_base() {
153 return Err(());
154 }
155 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 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#[inline]
197pub fn hostname(url: &Url) -> &str {
198 url.host_str().unwrap_or("")
199}
200
201#[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 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 if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
223 ||!port(url).is_empty()
225 || !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#[inline]
242pub fn port(url: &Url) -> &str {
243 &url[Position::BeforePort..Position::AfterPort]
244}
245
246#[allow(clippy::result_unit_err)]
248pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
249 let result;
250 {
251 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#[inline]
272pub fn pathname(url: &Url) -> &str {
273 url.path()
274}
275
276pub 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 && 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
299pub fn search(url: &Url) -> &str {
301 trim(&url[Position::AfterPath..Position::AfterQuery])
302}
303
304pub 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
313pub fn hash(url: &Url) -> &str {
315 trim(&url[Position::AfterQuery..])
316}
317
318pub fn set_hash(url: &mut Url, new_hash: &str) {
320 url.set_fragment(match new_hash {
321 "" => None,
324 _ 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}