Skip to content

Commit

Permalink
empty string when password does not exists
Browse files Browse the repository at this point in the history
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."
  • Loading branch information
Ricardo Monteiro committed Nov 15, 2022
1 parent 1c1e406 commit c87ea7d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions url/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -931,31 +932,31 @@ impl Url {
///
/// # fn run() -> Result<(), ParseError> {
/// let url = Url::parse("ftp://rms:secret123@example.com")?;
/// assert_eq!(url.password(), Some("secret123"));
/// assert_eq!(url.password(), "secret123");
///
/// let url = Url::parse("ftp://:secret123@example.com")?;
/// assert_eq!(url.password(), Some("secret123"));
/// assert_eq!(url.password(), "secret123");
///
/// let url = Url::parse("ftp://rms@example.com")?;
/// 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
""
}
}

Expand Down
4 changes: 2 additions & 2 deletions url/src/quirks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
/// Getter for https://url.spec.whatwg.org/#dom-url-password
#[inline]
pub fn password(url: &Url) -> &str {
url.password().unwrap_or("")
url.password()
}

/// Setter for https://url.spec.whatwg.org/#dom-url-password
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn set_hostname(url: &mut Url, new_hostname: &str) -> Result<(), ()> {
||!port(url).is_empty()
// Empty host that includes credentials
|| !url.username().is_empty()
|| !url.password().unwrap_or("").is_empty()
|| !url.password().is_empty()
{
return Err(());
}
Expand Down

0 comments on commit c87ea7d

Please sign in to comment.