@@ -6,34 +6,22 @@ use url::Url;
66
77/// A URL identifying a relay server.
88///
9- /// It is cheaply clonable, as the underlying type is wrapped into an `Arc`.
10- /// The main type under the hood though is [`Url`], with a few custom tweaks:
11- ///
12- /// - A relay URL is never a relative URL, so an implicit `.` is added at the end of the
13- /// domain name if missing.
14- ///
15- /// - [`fmt::Debug`] is implemented so it prints the URL rather than the URL struct fields.
16- /// Useful when logging e.g. `Option<RelayUrl>`.
9+ /// It is cheaply clonable, as the underlying type is wrapped into an `Arc`. The main type
10+ /// under the hood though is [`Url`].
1711///
1812/// To create a [`RelayUrl`] use the `From<Url>` implementation.
13+ ///
14+ /// It is encouraged to use a fully-qualified DNS domain name in the URL. Meaning a DNS
15+ /// name which ends in a `.`, e.g, in `relay.example.com.`. Otherwise the DNS resolution of
16+ /// your local host or network could interpret the DNS name as relative and in some
17+ /// configurations might cause additional delays or even connection problems.
1918#[ derive(
2019 Clone , derive_more:: Display , PartialEq , Eq , PartialOrd , Ord , Hash , Serialize , Deserialize ,
2120) ]
2221pub struct RelayUrl ( Arc < Url > ) ;
2322
2423impl From < Url > for RelayUrl {
25- fn from ( mut url : Url ) -> Self {
26- if let Some ( domain) = url. domain ( ) {
27- if !domain. ends_with ( '.' ) {
28- let domain = String :: from ( domain) + "." ;
29-
30- // This can fail, though it is unlikely the resulting URL is usable as a
31- // relay URL, probably it has the wrong scheme or is not a base URL or the
32- // like. We don't do full URL validation however, so just silently leave
33- // this bad URL in place. Something will fail later.
34- url. set_host ( Some ( & domain) ) . ok ( ) ;
35- }
36- }
24+ fn from ( url : Url ) -> Self {
3725 Self ( Arc :: new ( url) )
3826 }
3927}
@@ -105,24 +93,24 @@ mod tests {
10593 fn test_relay_url_debug_display ( ) {
10694 let url = RelayUrl :: from ( Url :: parse ( "https://example.com" ) . unwrap ( ) ) ;
10795
108- assert_eq ! ( format!( "{url:?}" ) , r#"RelayUrl("https://example.com. /")"# ) ;
96+ assert_eq ! ( format!( "{url:?}" ) , r#"RelayUrl("https://example.com/")"# ) ;
10997
110- assert_eq ! ( format!( "{url}" ) , "https://example.com. /" ) ;
98+ assert_eq ! ( format!( "{url}" ) , "https://example.com/" ) ;
11199 }
112100
113101 #[ test]
114102 fn test_relay_url_absolute ( ) {
115103 let url = RelayUrl :: from ( Url :: parse ( "https://example.com" ) . unwrap ( ) ) ;
116104
117- assert_eq ! ( url. domain( ) , Some ( "example.com. " ) ) ;
105+ assert_eq ! ( url. domain( ) , Some ( "example.com" ) ) ;
118106
119107 let url1 = RelayUrl :: from ( Url :: parse ( "https://example.com." ) . unwrap ( ) ) ;
120- assert_eq ! ( url , url1 ) ;
108+ assert_eq ! ( url1 . domain ( ) , Some ( "example.com." ) ) ;
121109
122110 let url2 = RelayUrl :: from ( Url :: parse ( "https://example.com./" ) . unwrap ( ) ) ;
123- assert_eq ! ( url , url2 ) ;
111+ assert_eq ! ( url2 . domain ( ) , Some ( "example.com." ) ) ;
124112
125113 let url3 = RelayUrl :: from ( Url :: parse ( "https://example.com/" ) . unwrap ( ) ) ;
126- assert_eq ! ( url , url3 ) ;
114+ assert_eq ! ( url3 . domain ( ) , Some ( "example.com" ) ) ;
127115 }
128116}
0 commit comments