diff --git a/url/src/lib.rs b/url/src/lib.rs index 32c02e396..94e7af175 100644 --- a/url/src/lib.rs +++ b/url/src/lib.rs @@ -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,21 +932,24 @@ 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() @@ -953,9 +957,9 @@ impl Url { && 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 + "" } } diff --git a/url/src/quirks.rs b/url/src/quirks.rs index 0674ebb62..f6f8c6e6c 100644 --- a/url/src/quirks.rs +++ b/url/src/quirks.rs @@ -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 @@ -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(()); }