Skip to content

Commit 1c1e406

Browse files
authored
Merge pull request #674 from 3scale-rs/add-authority-fn
url: add the authority method
2 parents 359bc90 + d8d4753 commit 1c1e406

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

url/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ impl Url {
792792
/// URLs that do *not* are either path-only like `unix:/run/foo.socket`
793793
/// or cannot-be-a-base like `data:text/plain,Stuff`.
794794
///
795+
/// See also the `authority` method.
796+
///
795797
/// # Examples
796798
///
797799
/// ```
@@ -817,6 +819,47 @@ impl Url {
817819
self.slice(self.scheme_end..).starts_with("://")
818820
}
819821

822+
/// Return the authority of this URL as an ASCII string.
823+
///
824+
/// Non-ASCII domains are punycode-encoded per IDNA if this is the host
825+
/// of a special URL, or percent encoded for non-special URLs.
826+
/// IPv6 addresses are given between `[` and `]` brackets.
827+
/// Ports are omitted if they match the well known port of a special URL.
828+
///
829+
/// Username and password are percent-encoded.
830+
///
831+
/// See also the `has_authority` method.
832+
///
833+
/// # Examples
834+
///
835+
/// ```
836+
/// use url::Url;
837+
/// # use url::ParseError;
838+
///
839+
/// # fn run() -> Result<(), ParseError> {
840+
/// let url = Url::parse("unix:/run/foo.socket")?;
841+
/// assert_eq!(url.authority(), "");
842+
/// let url = Url::parse("file:///tmp/foo")?;
843+
/// assert_eq!(url.authority(), "");
844+
/// let url = Url::parse("https://user:[email protected]/tmp/foo")?;
845+
/// assert_eq!(url.authority(), "user:[email protected]");
846+
/// let url = Url::parse("irc://àlex.рф.example.com:6667/foo")?;
847+
/// assert_eq!(url.authority(), "%C3%A0lex.%D1%80%D1%84.example.com:6667");
848+
/// let url = Url::parse("http://àlex.рф.example.com:80/foo")?;
849+
/// assert_eq!(url.authority(), "xn--lex-8ka.xn--p1ai.example.com");
850+
/// # Ok(())
851+
/// # }
852+
/// # run().unwrap();
853+
/// ```
854+
pub fn authority(&self) -> &str {
855+
let scheme_separator_len = "://".len() as u32;
856+
if self.has_authority() && self.path_start > self.scheme_end + scheme_separator_len {
857+
self.slice(self.scheme_end + scheme_separator_len..self.path_start)
858+
} else {
859+
""
860+
}
861+
}
862+
820863
/// Return whether this URL is a cannot-be-a-base URL,
821864
/// meaning that parsing a relative URL string with this URL as the base will return an error.
822865
///

url/tests/unit.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,3 +1162,47 @@ fn test_make_relative() {
11621162
assert_eq!(make_relative, None, "base: {}, uri: {}", base, uri);
11631163
}
11641164
}
1165+
1166+
#[test]
1167+
fn test_has_authority() {
1168+
let url = Url::parse("mailto:[email protected]").unwrap();
1169+
assert!(!url.has_authority());
1170+
let url = Url::parse("unix:/run/foo.socket").unwrap();
1171+
assert!(!url.has_authority());
1172+
let url = Url::parse("file:///tmp/foo").unwrap();
1173+
assert!(url.has_authority());
1174+
let url = Url::parse("http://example.com/tmp/foo").unwrap();
1175+
assert!(url.has_authority());
1176+
}
1177+
1178+
#[test]
1179+
fn test_authority() {
1180+
let url = Url::parse("mailto:[email protected]").unwrap();
1181+
assert_eq!(url.authority(), "");
1182+
let url = Url::parse("unix:/run/foo.socket").unwrap();
1183+
assert_eq!(url.authority(), "");
1184+
let url = Url::parse("file:///tmp/foo").unwrap();
1185+
assert_eq!(url.authority(), "");
1186+
let url = Url::parse("http://example.com/tmp/foo").unwrap();
1187+
assert_eq!(url.authority(), "example.com");
1188+
let url = Url::parse("ftp://127.0.0.1:21/").unwrap();
1189+
assert_eq!(url.authority(), "127.0.0.1");
1190+
let url = Url::parse("ftp://[email protected]:2121/").unwrap();
1191+
assert_eq!(url.authority(), "[email protected]:2121");
1192+
let url = Url::parse("https://:@example.com/").unwrap();
1193+
assert_eq!(url.authority(), "example.com");
1194+
let url = Url::parse("https://:password@[::1]:8080/").unwrap();
1195+
assert_eq!(url.authority(), ":password@[::1]:8080");
1196+
let url = Url::parse("gopher://user:@àlex.example.com:70").unwrap();
1197+
assert_eq!(url.authority(), "user@%C3%A0lex.example.com:70");
1198+
let url = Url::parse("irc://àlex:àlex@àlex.рф.example.com:6667/foo").unwrap();
1199+
assert_eq!(
1200+
url.authority(),
1201+
"%C3%A0lex:%C3%A0lex@%C3%A0lex.%D1%80%D1%84.example.com:6667"
1202+
);
1203+
let url = Url::parse("https://àlex:àlex@àlex.рф.example.com:443/foo").unwrap();
1204+
assert_eq!(
1205+
url.authority(),
1206+
"%C3%A0lex:%C3%[email protected]"
1207+
);
1208+
}

0 commit comments

Comments
 (0)