Skip to content

Commit 5d79947

Browse files
committed
Add url.includes_credentials() and url.is_special().
1 parent 3c71cac commit 5d79947

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

src/lib.rs

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl Url {
474474
assert_eq!(host_str, h.to_string())
475475
}
476476
HostInternal::Domain => {
477-
if SchemeType::from(self.scheme()).is_special() {
477+
if self.is_special() {
478478
assert!(!host_str.is_empty())
479479
}
480480
}
@@ -1566,16 +1566,16 @@ impl Url {
15661566
}
15671567

15681568
if let Some(host) = host {
1569-
if host == "" && SchemeType::from(self.scheme()).is_special() {
1569+
if host == "" && self.is_special() {
15701570
return Err(ParseError::EmptyHost);
15711571
}
1572-
if SchemeType::from(self.scheme()).is_special() {
1572+
if self.is_special() {
15731573
self.set_host_internal(Host::parse(host)?, None)
15741574
} else {
15751575
self.set_host_internal(Host::parse_opaque(host)?, None)
15761576
}
15771577
} else if self.has_host() {
1578-
if SchemeType::from(self.scheme()).is_special() {
1578+
if self.is_special() {
15791579
return Err(ParseError::EmptyHost);
15801580
}
15811581
debug_assert!(self.byte_at(self.scheme_end) == b':');
@@ -1951,6 +1951,68 @@ impl Url {
19511951
Ok(())
19521952
}
19531953

1954+
/// Return whether the URL is special.
1955+
///
1956+
/// A URL is special if its scheme is one of the following:
1957+
///
1958+
/// "http" | "https" | "ws" | "wss" | "ftp" | "gopher" | "file"
1959+
///
1960+
/// # Examples
1961+
///
1962+
/// ```
1963+
/// use url::Url;
1964+
/// # use url::ParseError;
1965+
///
1966+
/// # fn run() -> Result<(), ParseError> {
1967+
/// let url = Url::parse("ftp://rms@example.com")?;
1968+
/// assert!(url.is_special());
1969+
///
1970+
/// let url = Url::parse("file://foo.bar")?;
1971+
/// assert!(url.is_special());
1972+
///
1973+
/// let url = Url::parse("unix:/run/foo.socket")?;
1974+
/// assert!(!url.is_special());
1975+
///
1976+
/// let url = Url::parse("data:text/plain,Stuff")?;
1977+
/// assert!(!url.is_special());
1978+
/// # Ok(())
1979+
/// # }
1980+
/// # run().unwrap();
1981+
/// ```
1982+
pub fn is_special(&self) -> bool {
1983+
SchemeType::from(self.scheme()).is_special()
1984+
}
1985+
1986+
/// Return whether the URL includes credentials.
1987+
///
1988+
/// A URL includes credentials if its username or password is not the empty string.
1989+
///
1990+
/// # Examples
1991+
///
1992+
/// ```
1993+
/// use url::Url;
1994+
/// # use url::ParseError;
1995+
///
1996+
/// # fn run() -> Result<(), ParseError> {
1997+
/// let url = Url::parse("https://username:password@www.my_site.com")?;
1998+
/// assert!(url.includes_credentials());
1999+
///
2000+
/// let url = Url::parse("https://username@www.my_site.com")?;
2001+
/// assert!(url.includes_credentials());
2002+
///
2003+
/// let url = Url::parse("https://www.my_site.com")?;
2004+
/// assert!(!url.includes_credentials());
2005+
///
2006+
/// let url = Url::parse("https://@www.my_site.com")?;
2007+
/// assert!(!url.includes_credentials());
2008+
/// # Ok(())
2009+
/// # }
2010+
/// # run().unwrap();
2011+
/// ```
2012+
pub fn includes_credentials(&self) -> bool {
2013+
self.username() != "" || self.password().unwrap_or(&"") != ""
2014+
}
2015+
19542016
/// Convert a file name as `std::path::Path` into an URL in the `file` scheme.
19552017
///
19562018
/// This returns `Err` if the given path is not absolute or,

0 commit comments

Comments
 (0)