11//! Utility functions for dealing with URLs
22
3- use url:: Url ;
3+ use url:: { Url , Position } ;
44use url:: Host as UrlHost ;
55use hyper:: header:: Host ;
66use result:: { WebSocketResult , WSUrlErrorKind } ;
@@ -63,7 +63,7 @@ impl ToWebSocketUrlComponents for (UrlHost, u16, String, bool) {
6363 /// Convert a Host, port, resource name and secure flag to WebSocket URL components.
6464 fn to_components ( & self ) -> WebSocketResult < ( Host , String , bool ) > {
6565 ( Host {
66- hostname : self . 0 . serialize ( ) ,
66+ hostname : self . 0 . to_string ( ) ,
6767 port : Some ( self . 1 )
6868 } , self . 2 . clone ( ) , self . 3 ) . to_components ( )
6969 }
@@ -73,7 +73,7 @@ impl<'a> ToWebSocketUrlComponents for (UrlHost, u16, &'a str, bool) {
7373 /// Convert a Host, port, resource name and secure flag to WebSocket URL components.
7474 fn to_components ( & self ) -> WebSocketResult < ( Host , String , bool ) > {
7575 ( Host {
76- hostname : self . 0 . serialize ( ) ,
76+ hostname : self . 0 . to_string ( ) ,
7777 port : Some ( self . 1 )
7878 } , self . 2 , self . 3 ) . to_components ( )
7979 }
@@ -98,29 +98,23 @@ pub fn parse_url(url: &Url) -> WebSocketResult<(Host, String, bool)> {
9898 // https://html.spec.whatwg.org/multipage/#parse-a-websocket-url's-components
9999
100100 // Step 4
101- if url. fragment != None {
101+ if url. fragment ( ) . is_some ( ) {
102102 return Err ( From :: from ( WSUrlErrorKind :: CannotSetFragment ) ) ;
103103 }
104104
105- let secure = match url. scheme . as_ref ( ) {
105+ let secure = match url. scheme ( ) {
106106 // step 5
107107 "ws" => false ,
108108 "wss" => true ,
109109 // step 3
110110 _ => return Err ( From :: from ( WSUrlErrorKind :: InvalidScheme ) ) ,
111111 } ;
112112
113- let host = url. host ( ) . unwrap ( ) . serialize ( ) ; // Step 6
114- let port = url. port_or_default ( ) ; // Steps 7 and 8
113+ let host = url. host_str ( ) . unwrap ( ) . to_owned ( ) ; // Step 6
114+ let port = url. port_or_known_default ( ) ; // Steps 7 and 8
115115
116- let mut resource = "/" . to_owned ( ) ; // step 10
117- resource. push_str ( url. path ( ) . unwrap ( ) . join ( "/" ) . as_ref ( ) ) ; // step 9
118-
119- // Step 11
120- if let Some ( ref query) = url. query {
121- resource. push ( '?' ) ;
122- resource. push_str ( query) ;
123- }
116+ // steps 9, 10, 11
117+ let resource = url[ Position :: BeforePath ..Position :: AfterQuery ] . to_owned ( ) ;
124118
125119 // Step 12
126120 Ok ( ( Host { hostname : host, port : port } , resource, secure) )
@@ -130,29 +124,17 @@ pub fn parse_url(url: &Url) -> WebSocketResult<(Host, String, bool)> {
130124mod tests {
131125 use super :: * ;
132126 //use test;
133- use url:: { Url , SchemeData , RelativeSchemeData , Host } ;
127+ use url:: Url ;
134128 use result:: { WebSocketError , WSUrlErrorKind } ;
135129
136130 fn url_for_test ( ) -> Url {
137- Url {
138- fragment : None ,
139- scheme : "ws" . to_owned ( ) ,
140- scheme_data : SchemeData :: Relative ( RelativeSchemeData {
141- username : "" . to_owned ( ) ,
142- password : None ,
143- host : Host :: Domain ( "www.example.com" . to_owned ( ) ) ,
144- port : Some ( 8080 ) ,
145- default_port : Some ( 80 ) ,
146- path : vec ! [ "some" . to_owned( ) , "path" . to_owned( ) ]
147- } ) ,
148- query : Some ( "a=b&c=d" . to_owned ( ) ) ,
149- }
131+ Url :: parse ( "ws://www.example.com:8080/some/path?a=b&c=d" ) . unwrap ( )
150132 }
151133
152134 #[ test]
153135 fn test_parse_url_fragments_not_accepted ( ) {
154136 let url = & mut url_for_test ( ) ;
155- url. fragment = Some ( "non_null_fragment" . to_owned ( ) ) ;
137+ url. set_fragment ( Some ( "non_null_fragment" ) ) ;
156138
157139 let result = parse_url ( url) ;
158140 match result {
@@ -166,10 +148,10 @@ mod tests {
166148 #[ test]
167149 fn test_parse_url_invalid_schemes_return_error ( ) {
168150 let url = & mut url_for_test ( ) ;
169-
151+
170152 let invalid_schemes = & [ "http" , "https" , "gopher" , "file" , "ftp" , "other" ] ;
171153 for scheme in invalid_schemes {
172- url. scheme = scheme . to_string ( ) ;
154+ url. set_scheme ( scheme) . unwrap ( ) ;
173155
174156 let result = parse_url ( url) ;
175157 match result {
@@ -180,14 +162,14 @@ mod tests {
180162 }
181163 }
182164 }
183-
165+
184166 #[ test]
185167 fn test_parse_url_valid_schemes_return_ok ( ) {
186168 let url = & mut url_for_test ( ) ;
187-
169+
188170 let valid_schemes = & [ "ws" , "wss" ] ;
189171 for scheme in valid_schemes {
190- url. scheme = scheme . to_string ( ) ;
172+ url. set_scheme ( scheme) . unwrap ( ) ;
191173
192174 let result = parse_url ( url) ;
193175 match result {
@@ -200,7 +182,7 @@ mod tests {
200182 #[ test]
201183 fn test_parse_url_ws_returns_unset_secure_flag ( ) {
202184 let url = & mut url_for_test ( ) ;
203- url. scheme = "ws" . to_owned ( ) ;
185+ url. set_scheme ( "ws" ) . unwrap ( ) ;
204186
205187 let result = parse_url ( url) ;
206188 let secure = match result {
@@ -213,7 +195,7 @@ mod tests {
213195 #[ test]
214196 fn test_parse_url_wss_returns_set_secure_flag ( ) {
215197 let url = & mut url_for_test ( ) ;
216- url. scheme = "wss" . to_owned ( ) ;
198+ url. set_scheme ( "wss" ) . unwrap ( ) ;
217199
218200 let result = parse_url ( url) ;
219201 let secure = match result {
@@ -222,7 +204,7 @@ mod tests {
222204 } ;
223205 assert ! ( secure) ;
224206 }
225-
207+
226208 #[ test]
227209 fn test_parse_url_generates_proper_output ( ) {
228210 let url = & url_for_test ( ) ;
@@ -232,7 +214,7 @@ mod tests {
232214 Ok ( ( host, resource, _) ) => ( host, resource) ,
233215 Err ( e) => panic ! ( e) ,
234216 } ;
235-
217+
236218 assert_eq ! ( host. hostname, "www.example.com" . to_owned( ) ) ;
237219 assert_eq ! ( resource, "/some/path?a=b&c=d" . to_owned( ) ) ;
238220
@@ -245,10 +227,7 @@ mod tests {
245227 #[ test]
246228 fn test_parse_url_empty_path_should_give_slash ( ) {
247229 let url = & mut url_for_test ( ) ;
248- match url. scheme_data {
249- SchemeData :: Relative ( ref mut scheme_data) => { scheme_data. path = vec ! [ ] ; } ,
250- _ => ( )
251- }
230+ url. set_path ( "/" ) ;
252231
253232 let result = parse_url ( url) ;
254233 let resource = match result {
@@ -262,7 +241,7 @@ mod tests {
262241 #[ test]
263242 fn test_parse_url_none_query_should_not_append_question_mark ( ) {
264243 let url = & mut url_for_test ( ) ;
265- url. query = None ;
244+ url. set_query ( None ) ;
266245
267246 let result = parse_url ( url) ;
268247 let resource = match result {
@@ -276,12 +255,7 @@ mod tests {
276255 #[ test]
277256 fn test_parse_url_none_port_should_use_default_port ( ) {
278257 let url = & mut url_for_test ( ) ;
279- match url. scheme_data {
280- SchemeData :: Relative ( ref mut scheme_data) => {
281- scheme_data. port = None ;
282- } ,
283- _ => ( )
284- }
258+ url. set_port ( None ) . unwrap ( ) ;
285259
286260 let result = parse_url ( url) ;
287261 let host = match result {
0 commit comments