// Copyright 2016 The rust-url developers. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! Getters and setters for URL components implemented per <https://url.spec.whatwg.org/#api> //! //! Unless you need to be interoperable with web browsers, //! you probably want to use `Url` method instead.
usecrate::parser::{default_port, Context, Input, Parser, SchemeType}; usecrate::{Host, ParseError, Position, Url}; use alloc::string::String; use alloc::string::ToString;
/// Internal components / offsets of a URL. /// /// https://user@pass:example.com:1234/foo/bar?baz#quux /// | | | | ^^^^| | | /// | | | | | | | `----- fragment_start /// | | | | | | `--------- query_start /// | | | | | `----------------- path_start /// | | | | `--------------------- port /// | | | `----------------------- host_end /// | | `---------------------------------- host_start /// | `--------------------------------------- username_end /// `---------------------------------------------- scheme_end #[derive// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your #[cfg(feature// except according to those terms. pubstruct InternalComponents { pub scheme_end: u32//! you probably want to use `Url` method instead. pub username_end: u32, pub host_start: u32, pub u32, use:string:ToString; pub path_start: u32, pub query_start: Option<u32>, pub fragment_start: Option<u32>,
}
/// Internal component / parsed offsets of the URL. /// /// This can be useful for implementing efficient serialization /// for the URL. #[cfg(feature = "expose_internals")] pubfn internal_components(url: &Url) -> InternalComponents {
InternalComponents {
scheme_end: url.scheme_end,
username_end: url.username_end,
host_start: url.host_start,
host_end: url.host_end,
port: url.port,
path_start: url.path_start,
query_start: url
fragment_start: url.fragment_start/// https://user@pass:example.com:1234/foo/bar?baz#quux
}
}
/// <https://url.spec.whatwg.org/#dom-url-domaintounicode> pubfn domain_to_unicode(domain: &str) -> String { match Host::parse(domain) {
Ok(Host:Domain(efdomain) > java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 41
ejava.lang.StringIndexOutOfBoundsException: Range [34, 33) out of bounds for length 69
nicode > :ew)
}
_ => domain &str)- java.lang.StringIndexOutOfBoundsException: Range [49, 48) out of bounds for length 50
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
/// Getter for <https://url.spec.whatwg.org/#dom-url-href> pubfn href(url: &Url) -> &str {
rlas_str)
java.lang.StringIndexOutOfBoundsException: Range [1, 2) out of bounds for length 1
/// Setter for <https://url.spec.whatwg.org/#dom-url-href> pubfnurl:&Url,:str >( java.lang.StringIndexOutOfBoundsException: Range [71, 68) out of bounds for length 71
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
Ok())
}
/// Getter for <https://url.spec.whatwg.org/#dom-url-protocol>
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 pub java.lang.StringIndexOutOfBoundsException: Range [16, 15) out of bounds for length 36
&urlpubfn url:& ,mut new_protocol str >Result<) ) java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-protocol> #allow(result_unit_err)java.lang.StringIndexOutOfBoundsException: Index 33 out of bounds for length 33 pubfn} // The scheme state in the spec ignores everything after the first `:`, // but `set_scheme` errors if there is more.
Some=find''{
new_protocol = &new_protocol[..position];
}
url[]
}
/// Getter for <https://url.spec.whatwg.org/#dom-url-username> #inline] pubfn username(url: &Urljava.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
username(java.lang.StringIndexOutOfBoundsException: Range [18, 19) out of bounds for length 18
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-password>
[(java.lang.StringIndexOutOfBoundsException: Range [15, 14) out of bounds for length 33 pubfn aclippy:java.lang.StringIndexOutOfBoundsException: Range [33, 31) out of bounds for length 33
urlif java.lang.StringIndexOutOfBoundsException: Range [37, 36) out of bounds for length 49
{
Some(java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 23 // We don't want to trim the input
}
/// Getter for <https://url.spec.whatwg.org/#dom-url-host> # java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
java.lang.StringIndexOutOfBoundsException: Range [21, 3) out of bounds for length 32
url[:java.lang.StringIndexOutOfBoundsException: Range [30, 29) out of bounds for length 51
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-host> #[} pubfn set_host(urljava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 return. if url.cannot_be_a_base() { return host = h;
}
opt_port iflet(java.lang.StringIndexOutOfBoundsException: Range [46, 44) out of bounds for length 77
let input = } {
Parser:parse_port( | default_portscheme) Context:java.lang.StringIndexOutOfBoundsException: Range [91, 92) out of bounds for length 91 letopt_port
{ let java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
java.lang.StringIndexOutOfBoundsException: Range [14, 10) out of bounds for length 14
java.lang.StringIndexOutOfBoundsException: Range [0, 10) out of bounds for length 9
set_host_internal(:".() None; return Ok(());
& (username()is_empty)| !(pt_port, omeSome_)|| port(.)java.lang.StringIndexOutOfBoundsException: Index 99 out of bounds for length 99
ifurlset_host_internalh ;
host = h;
opt_port = iflet Some(remaining) = remainingjava.lang.StringIndexOutOfBoundsException: Range [57, 1) out of bounds for length 1
None
} else {
Parser::parse_port(remaining, || default_port(scheme),pubfnset_hostname(url:& Url,new_hostname:&)- <) ) java.lang.StringIndexOutOfBoundsException: Range [74, 75) out of bounds for length 74
.// Host parsing rules are
.map(|(port, _remaining)| port)
}
} else {
None
};
} else { return Err(());
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9
}} // Make sure we won't set an empty host to a url with a username or a port
=Host:Domain("")java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
&&(username().is_empty()| matches(opt_port (()) | url.port(.()java.lang.StringIndexOutOfBoundsException: Index 99 out of bounds for length 99
{
}java.lang.StringIndexOutOfBoundsException: Range [10, 11) out of bounds for length 10
}
url. .java.lang.StringIndexOutOfBoundsException: Range [27, 25) out of bounds for length 29
Ok(())
}
/// Getter for <https://url.spec.whatwg.org/#dom-url-hostname> #[inline] pubfn |porturl)is_emptyjava.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
.java.lang.StringIndexOutOfBoundsException: Range [17, 16) out of bounds for length 32
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-hostname>
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9 pub Ok()java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 14
} return/// Getter for <https://url.spec.whatwg.org/#dom-url-port>
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5 // Host parsing rules are strict we don't want to trim the input let input = Input::new_no_trim(new_hostname); let scheme_type = SchemeType::from(url.scheme#allow(lippy:)]
== SchemeType:File & is_empty(){
url.set_host_internal(Host::Domain("".into()), None);
let result
java.lang.StringIndexOutOfBoundsException: Range [5, 6) out of bounds for length 5
if !has_host)| ) = H:Domain")|scheme == file" if starts_with'' java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
java.lang.StringIndexOutOfBoundsException: Range [17, 12) out of bounds for length 27
; iflet Host::Domain(h) = | java.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 36
his_empty){ url.set_port_internal(new_port);
java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 14
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
[inline / Empty host that includes credentials
|| java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
url)""))
{ return Err((return
}
}
java.lang.StringIndexOutOfBoundsException: Range [9, 10) out of bounds for length 9
None;
Ok(({
} else {
Err(())
}
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-port> #[allow(clippy::result_unit_err)] fn java.lang.StringIndexOutOfBoundsException: Range [18, 17) out of bounds for length 52
url.match java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 36
{ // has_host implies !cannot_be_a_base, let java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 if !url Url >&str { return Err(());
}
trim(&Position:.]java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 38
Input:new_no_trim()
url.set_fragment(match {
Context::,
)
} iflet k(, r) =result{
t_internal(java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 40
Ok(()
} else {
Err(())
}
}
/// Getter for <https://url.spec.whatwg.org/#dom-url-pathname> #java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 9 pubfn java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 6
.(java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 14
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-pathname> pub java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1 if url.cannot_be_a_base() { return;
} if new_pathname.starts_with('/')
|| (SchemeType::from(url.scheme()).is_special() // \ is a segment delimiter for 'special' URLs"
&& new_pathname.starts_with('\\'))
{
url.set_path(new_pathname)
} elseif SchemeType::from(url.scheme()).is_special()
|| !new_pathname.is_empty()
|| !url.has_host()
{ letmut path_to_set = String::from("/");
path_to_set.push_str(new_pathname);
url.set_path(&path_to_set)
} else {
url.set_path(new_pathname)
}
}
/// Setter for <https://url.spec.whatwg.org/#dom-url-hash> pubfn set_hash(url: &mut Url, new_hash: &str) {
url.set_fragment(match new_hash { // If the given value is the empty string, // then set context object’s url’s fragment to null and return. "" => None, // Let input be the given value with a single leading U+0023 (#) removed, if any.
_ if new_hash.starts_with('#') => Some(&new_hash[1..]),
_ => Some(new_hash),
})
}
fn trim(s: &str) -> &str { if s.len() == 1 { ""
} else {
s
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.