1use crate::parser::{default_port, Context, Input, Parser, SchemeType};
15use crate::{Host, ParseError, Position, Url};
16
17#[derive(Copy, Clone)]
30#[cfg(feature = "expose_internals")]
31pub struct InternalComponents {
32 pub scheme_end: u32,
33 pub username_end: u32,
34 pub host_start: u32,
35 pub host_end: u32,
36 pub port: Option<u16>,
37 pub path_start: u32,
38 pub query_start: Option<u32>,
39 pub fragment_start: Option<u32>,
40}
41
42#[cfg(feature = "expose_internals")]
47pub fn internal_components(url: &Url) -> InternalComponents {
48 InternalComponents {
49 scheme_end: url.scheme_end,
50 username_end: url.username_end,
51 host_start: url.host_start,
52 host_end: url.host_end,
53 port: url.port,
54 path_start: url.path_start,
55 query_start: url.query_start,
56 fragment_start: url.fragment_start,
57 }
58}
59
60pub fn domain_to_ascii(domain: &str) -> String {
62 match Host::parse(domain) {
63 Ok(Host::Domain(domain)) => domain,
64 _ => String::new(),
65 }
66}
67
68pub fn domain_to_unicode(domain: &str) -> String {
70 match Host::parse(domain) {
71 Ok(Host::Domain(ref domain)) => {
72 let (unicode, _errors) = idna::domain_to_unicode(domain);
73 unicode
74 }
75 _ => String::new(),
76 }
77}
78
79pub fn href(url: &Url) -> &str {
81 url.as_str()
82}
83
84pub fn set_href(url: &mut Url, value: &str) -> Result<(), ParseError> {
86 *url = Url::parse(value)?;
87 Ok(())
88}
89
90pub fn origin(url: &Url) -> String {
92 url.origin().ascii_serialization()
93}
94
95#[inline]
97pub fn protocol(url: &Url) -> &str {
98 &url.as_str()[..url.scheme().len() + ":".len()]
99}
100
101#[allow(clippy::result_unit_err)]
103pub fn set_protocol(url: &mut Url, mut new_protocol: &str) -> Result<(), ()> {
104 if let Some(position) = new_protocol.find(':') {
107 new_protocol = &new_protocol[..position];
108 }
109 url.set_scheme(new_protocol)
110}
111
112#[inline]
114pub fn username(url: &Url) -> &str {
115 url.username()
116}
117
118#[allow(clippy::result_unit_err)]
120pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
121 url.set_username(new_username)
122}
123
124#[inline]
126pub fn password(url: &Url) -> &str {
127 url.password().unwrap_or("")
128}
129
130#[allow(clippy::result_unit_err)]
132pub fn set_password(url: &mut Url, new_password: &str) -> Result<(), ()> {
133 url.set_password(if new_password.is_empty() {
134 None
135 } else {
136 Some(new_password)
137 })
138}
139
140#[inline]
142pub fn host(url: &Url) -> &str {
143 &url[Position::BeforeHost..Position::AfterPort]
144}
145
146#[allow(clippy::result_unit_err)]
148pub fn set_host(url: &mut Url, new_host: &str) -> Result<(), ()> {
149 if url.cannot_be_a_base() {
151 return Err(());
152 }
153 let input = Input::no_trim(new_host);
156 let host;
157 let opt_port;
158 {
159 let scheme = url.scheme();
160 let scheme_type = SchemeType::from(scheme);
161 if scheme_type == SchemeType::File && new_host.is_empty() {
162 url.set_host_internal(Host::Domain(String::new()), None);
163 return Ok(());
164 }
165
166 if let Ok((h, remaining)) = Parser::parse_host(input, scheme_type) {
167 host = h;
168 opt_port = if let Some(remaining) = remaining.split_prefix(':') {
169 if remaining.is_empty() {
170 None
171 } else {
172 Parser::parse_port(remaining, || default_port(scheme), Context::Setter)
173 .ok()
174 .map(|(port, _remaining)| port)
175 }
176 } else {
177 None
178 };
179 } else {
180 return Err(());
181 }
182 }
183 if host == Host::Domain("".to_string())
185 && (!username(url).is_empty() || matches!(opt_port, Some(Some(_))) || url.port().is_some())
186 {
187 return Err(());
188 }
189 url.set_host_internal(host, opt_port);
190 Ok(())
191}
192
193#[inline]
195pub fn hostname(url: &Url) -> &str {
196 url.host_str().unwrap_or("")
197}
198
199#[allow(clippy::result_unit_err)]
201pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
202 if url.cannot_be_a_base() {
203 return Err(());
204 }
205 let input = Input::no_trim(new_hostname);
207 let scheme_type = SchemeType::from(url.scheme());
208 if scheme_type == SchemeType::File && new_hostname.is_empty() {
209 url.set_host_internal(Host::Domain(String::new()), None);
210 return Ok(());
211 }
212
213 if let Ok((host, _remaining)) = Parser::parse_host(input, scheme_type) {
214 if let Host::Domain(h) = &host {
215 if h.is_empty() {
216 if SchemeType::from(url.scheme()) == SchemeType::SpecialNotFile
218 ||!port(url).is_empty()
220 || !url.username().is_empty()
222 || !url.password().unwrap_or("").is_empty()
223 {
224 return Err(());
225 }
226 }
227 }
228 url.set_host_internal(host, None);
229 Ok(())
230 } else {
231 Err(())
232 }
233}
234
235#[inline]
237pub fn port(url: &Url) -> &str {
238 &url[Position::BeforePort..Position::AfterPort]
239}
240
241#[allow(clippy::result_unit_err)]
243pub fn set_port(url: &mut Url, new_port: &str) -> Result<(), ()> {
244 let result;
245 {
246 let scheme = url.scheme();
248 if !url.has_host() || url.host() == Some(Host::Domain("")) || scheme == "file" {
249 return Err(());
250 }
251 result = Parser::parse_port(
252 Input::new(new_port),
253 || default_port(scheme),
254 Context::Setter,
255 )
256 }
257 if let Ok((new_port, _remaining)) = result {
258 url.set_port_internal(new_port);
259 Ok(())
260 } else {
261 Err(())
262 }
263}
264
265#[inline]
267pub fn pathname(url: &Url) -> &str {
268 url.path()
269}
270
271pub fn set_pathname(url: &mut Url, new_pathname: &str) {
273 if url.cannot_be_a_base() {
274 return;
275 }
276 if new_pathname.starts_with('/')
277 || (SchemeType::from(url.scheme()).is_special()
278 && new_pathname.starts_with('\\'))
280 {
281 url.set_path(new_pathname)
282 } else {
283 let mut path_to_set = String::from("/");
284 path_to_set.push_str(new_pathname);
285 url.set_path(&path_to_set)
286 }
287}
288
289pub fn search(url: &Url) -> &str {
291 trim(&url[Position::AfterPath..Position::AfterQuery])
292}
293
294pub fn set_search(url: &mut Url, new_search: &str) {
296 url.set_query(match new_search {
297 "" => None,
298 _ if new_search.starts_with('?') => Some(&new_search[1..]),
299 _ => Some(new_search),
300 })
301}
302
303pub fn hash(url: &Url) -> &str {
305 trim(&url[Position::AfterQuery..])
306}
307
308pub fn set_hash(url: &mut Url, new_hash: &str) {
310 url.set_fragment(match new_hash {
311 "" => None,
314 _ if new_hash.starts_with('#') => Some(&new_hash[1..]),
316 _ => Some(new_hash),
317 })
318}
319
320fn trim(s: &str) -> &str {
321 if s.len() == 1 {
322 ""
323 } else {
324 s
325 }
326}