Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty string when password does not exists #804

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 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,34 @@ 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(), "");
///
/// 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