@@ -474,7 +474,7 @@ impl Url {
474
474
assert_eq ! ( host_str, h. to_string( ) )
475
475
}
476
476
HostInternal :: Domain => {
477
- if SchemeType :: from ( self . scheme ( ) ) . is_special ( ) {
477
+ if self . is_special ( ) {
478
478
assert ! ( !host_str. is_empty( ) )
479
479
}
480
480
}
@@ -1566,16 +1566,16 @@ impl Url {
1566
1566
}
1567
1567
1568
1568
if let Some ( host) = host {
1569
- if host == "" && SchemeType :: from ( self . scheme ( ) ) . is_special ( ) {
1569
+ if host == "" && self . is_special ( ) {
1570
1570
return Err ( ParseError :: EmptyHost ) ;
1571
1571
}
1572
- if SchemeType :: from ( self . scheme ( ) ) . is_special ( ) {
1572
+ if self . is_special ( ) {
1573
1573
self . set_host_internal ( Host :: parse ( host) ?, None )
1574
1574
} else {
1575
1575
self . set_host_internal ( Host :: parse_opaque ( host) ?, None )
1576
1576
}
1577
1577
} else if self . has_host ( ) {
1578
- if SchemeType :: from ( self . scheme ( ) ) . is_special ( ) {
1578
+ if self . is_special ( ) {
1579
1579
return Err ( ParseError :: EmptyHost ) ;
1580
1580
}
1581
1581
debug_assert ! ( self . byte_at( self . scheme_end) == b':' ) ;
@@ -1951,6 +1951,68 @@ impl Url {
1951
1951
Ok ( ( ) )
1952
1952
}
1953
1953
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
+
1954
2016
/// Convert a file name as `std::path::Path` into an URL in the `file` scheme.
1955
2017
///
1956
2018
/// This returns `Err` if the given path is not absolute or,
0 commit comments