Skip to content

Commit 44473bb

Browse files
author
Ricardo Monteiro
committed
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
1 parent 1c1e406 commit 44473bb

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

url/src/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,8 @@ impl Url {
921921
}
922922
}
923923

924-
/// Return the password for this URL, if any, as a percent-encoded ASCII string.
924+
/// Return the password for this URL (typically the empty string)
925+
/// as a percent-encoded ASCII string.
925926
///
926927
/// # Examples
927928
///
@@ -931,31 +932,34 @@ impl Url {
931932
///
932933
/// # fn run() -> Result<(), ParseError> {
933934
/// let url = Url::parse("ftp://rms:[email protected]")?;
934-
/// assert_eq!(url.password(), Some("secret123"));
935+
/// assert_eq!(url.password(), "secret123");
935936
///
936937
/// let url = Url::parse("ftp://:[email protected]")?;
937-
/// assert_eq!(url.password(), Some("secret123"));
938+
/// assert_eq!(url.password(), "secret123");
939+
///
940+
/// let url = Url::parse("ftp://rms:@example.com")?;
941+
/// assert_eq!(url.password(), "");
938942
///
939943
/// let url = Url::parse("ftp://[email protected]")?;
940-
/// assert_eq!(url.password(), None);
944+
/// assert_eq!(url.password(), "");
941945
///
942946
/// let url = Url::parse("https://example.com")?;
943-
/// assert_eq!(url.password(), None);
947+
/// assert_eq!(url.password(), "");
944948
/// # Ok(())
945949
/// # }
946950
/// # run().unwrap();
947951
/// ```
948-
pub fn password(&self) -> Option<&str> {
952+
pub fn password(&self) -> &str {
949953
// This ':' is not the one marking a port number since a host can not be empty.
950954
// (Except for file: URLs, which do not have port numbers.)
951955
if self.has_authority()
952956
&& self.username_end != self.serialization.len() as u32
953957
&& self.byte_at(self.username_end) == b':'
954958
{
955959
debug_assert!(self.byte_at(self.host_start - 1) == b'@');
956-
Some(self.slice(self.username_end + 1..self.host_start - 1))
960+
self.slice(self.username_end + 1..self.host_start - 1)
957961
} else {
958-
None
962+
""
959963
}
960964
}
961965

url/src/quirks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub fn set_username(url: &mut Url, new_username: &str) -> Result<(), ()> {
124124
/// Getter for https://url.spec.whatwg.org/#dom-url-password
125125
#[inline]
126126
pub fn password(url: &Url) -> &str {
127-
url.password().unwrap_or("")
127+
url.password()
128128
}
129129

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

0 commit comments

Comments
 (0)