-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
empty string when
password
does not exists
standard: https://url.spec.whatwg.org/#url-representation "A URL’s password is an ASCII string identifying a password. It is initially the empty string." test
- Loading branch information
1 parent
1c1e406
commit 16786ea
Showing
2 changed files
with
14 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -921,7 +921,8 @@ impl Url { | |
} | ||
} | ||
|
||
/// Return the password for this URL, if any, as a percent-encoded ASCII string. | ||
/// Return the password for this URL (typically the empty string) | ||
/// as a percent-encoded ASCII string. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -931,31 +932,34 @@ impl Url { | |
/// | ||
/// # fn run() -> Result<(), ParseError> { | ||
/// let url = Url::parse("ftp://rms:[email protected]")?; | ||
/// assert_eq!(url.password(), Some("secret123")); | ||
/// assert_eq!(url.password(), "secret123"); | ||
/// | ||
/// let url = Url::parse("ftp://:[email protected]")?; | ||
/// assert_eq!(url.password(), Some("secret123")); | ||
/// assert_eq!(url.password(), "secret123"); | ||
/// | ||
/// let url = Url::parse("ftp://rms:@example.com")?; | ||
/// assert_eq!(url.password(), ""); | ||
/// | ||
/// let url = Url::parse("ftp://[email protected]")?; | ||
/// assert_eq!(url.password(), None); | ||
/// assert_eq!(url.password(), ""); | ||
/// | ||
/// let url = Url::parse("https://example.com")?; | ||
/// assert_eq!(url.password(), None); | ||
/// assert_eq!(url.password(), ""); | ||
/// # Ok(()) | ||
/// # } | ||
/// # run().unwrap(); | ||
/// ``` | ||
pub fn password(&self) -> Option<&str> { | ||
pub fn password(&self) -> &str { | ||
// This ':' is not the one marking a port number since a host can not be empty. | ||
// (Except for file: URLs, which do not have port numbers.) | ||
if self.has_authority() | ||
&& self.username_end != self.serialization.len() as u32 | ||
&& self.byte_at(self.username_end) == b':' | ||
{ | ||
debug_assert!(self.byte_at(self.host_start - 1) == b'@'); | ||
Some(self.slice(self.username_end + 1..self.host_start - 1)) | ||
self.slice(self.username_end + 1..self.host_start - 1) | ||
} else { | ||
None | ||
"" | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters