@@ -36,7 +36,6 @@ let response = match request.read_response() {
3636
3737*/
3838
39- use url;
4039use url:: Url ;
4140use method:: Method ;
4241use std:: io:: { IoError , IoResult } ;
@@ -106,23 +105,24 @@ impl<S: Reader + Writer = super::NetworkStream> RequestWriter<S> {
106105 }
107106
108107 pub fn new_request ( method : Method , url : Url , use_ssl : bool , auto_detect_ssl : bool ) -> IoResult < RequestWriter < S > > {
109- let host = match url. port {
110- None => Host {
111- name : url. host . clone ( ) ,
112- port : None ,
113- } ,
114- Some ( ref p) => Host {
115- name : url. host . clone ( ) ,
116- port : Some ( from_str ( p. as_slice ( ) ) . expect ( "You didn’t aught to give a bad port!" ) ) ,
108+ let host = Host {
109+ name : url. domain ( ) . unwrap ( ) . to_string ( ) ,
110+ port : {
111+ let port = url. port ( ) . unwrap ( ) ;
112+ if port. is_empty ( ) {
113+ None
114+ } else {
115+ Some ( from_str ( port) . expect ( "You didn’t aught to give a bad port!" ) )
116+ }
117117 } ,
118118 } ;
119119
120- let remote_addr = try!( url_to_socket_addr ( & url) ) ;
121- info ! ( "using ip address {} for {}" , remote_addr. to_str( ) , url . host. as_slice( ) ) ;
120+ let remote_addr = try!( url_to_socket_addr ( & url, & host ) ) ;
121+ info ! ( "using ip address {} for {}" , remote_addr. to_str( ) , host. name . as_slice( ) ) ;
122122
123- fn url_to_socket_addr ( url : & Url ) -> IoResult < SocketAddr > {
123+ fn url_to_socket_addr ( url : & Url , host : & Host ) -> IoResult < SocketAddr > {
124124 // Just grab the first IPv4 address
125- let addrs = try!( get_host_addresses ( url . host . as_slice ( ) ) ) ;
125+ let addrs = try!( get_host_addresses ( host. name . as_slice ( ) ) ) ;
126126 let addr = addrs. move_iter ( ) . find ( |& a| {
127127 match a {
128128 Ipv4Addr ( ..) => true ,
@@ -134,8 +134,8 @@ impl<S: Reader + Writer = super::NetworkStream> RequestWriter<S> {
134134 let addr = addr. unwrap ( ) ;
135135
136136 // Default to 80, using the port specified or 443 if the protocol is HTTPS.
137- let port = match url . port {
138- Some ( ref p) => from_str ( p . as_slice ( ) ) . expect ( "You didn’t aught to give a bad port!" ) ,
137+ let port = match host . port {
138+ Some ( p) => p ,
139139 // FIXME: case insensitivity?
140140 None => if url. scheme . as_slice ( ) == "https" { 443 } else { 80 } ,
141141 } ;
@@ -186,7 +186,8 @@ impl<S: Connecter + Reader + Writer = super::NetworkStream> RequestWriter<S> {
186186
187187 self . stream = match self . remote_addr {
188188 Some ( addr) => {
189- let stream = try!( Connecter :: connect ( addr, self . url . host . as_slice ( ) , self . use_ssl ) ) ;
189+ let stream = try!( Connecter :: connect (
190+ addr, self . headers . host . as_ref ( ) . unwrap ( ) . name . as_slice ( ) , self . use_ssl ) ) ;
190191 Some ( BufferedStream :: new ( stream) )
191192 } ,
192193 None => fail ! ( "connect() called before remote_addr was set" ) ,
@@ -217,12 +218,13 @@ impl<S: Connecter + Reader + Writer = super::NetworkStream> RequestWriter<S> {
217218
218219 // Write the Request-Line (RFC2616 §5.1)
219220 // TODO: get to the point where we can say HTTP/1.1 with good conscience
221+ let ( question_mark, query) = match self . url . query {
222+ Some ( ref query) => ( "?" , query. as_slice ( ) ) ,
223+ None => ( "" , "" )
224+ } ;
220225 try!( write ! ( self . stream. get_mut_ref( ) as & mut Writer ,
221226 "{} {}{}{} HTTP/1.0\r \n " ,
222- self . method. to_str( ) ,
223- if self . url. path. len( ) > 0 { self . url. path. as_slice( ) } else { "/" } ,
224- if self . url. query. len( ) > 0 { "?" } else { "" } ,
225- url:: query_to_str( & self . url. query) ) ) ;
227+ self . method. to_str( ) , self . url. serialize_path( ) . unwrap( ) , question_mark, query) ) ;
226228
227229 try!( self . headers . write_all ( self . stream . get_mut_ref ( ) ) ) ;
228230 self . headers_written = true ;
0 commit comments