@@ -36,7 +36,6 @@ let response = match request.read_response() {
36
36
37
37
*/
38
38
39
- use url;
40
39
use url:: Url ;
41
40
use method:: Method ;
42
41
use std:: io:: { IoError , IoResult } ;
@@ -106,23 +105,24 @@ impl<S: Reader + Writer = super::NetworkStream> RequestWriter<S> {
106
105
}
107
106
108
107
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
+ }
117
117
} ,
118
118
} ;
119
119
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( ) ) ;
122
122
123
- fn url_to_socket_addr ( url : & Url ) -> IoResult < SocketAddr > {
123
+ fn url_to_socket_addr ( url : & Url , host : & Host ) -> IoResult < SocketAddr > {
124
124
// 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 ( ) ) ) ;
126
126
let addr = addrs. move_iter ( ) . find ( |& a| {
127
127
match a {
128
128
Ipv4Addr ( ..) => true ,
@@ -134,8 +134,8 @@ impl<S: Reader + Writer = super::NetworkStream> RequestWriter<S> {
134
134
let addr = addr. unwrap ( ) ;
135
135
136
136
// 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 ,
139
139
// FIXME: case insensitivity?
140
140
None => if url. scheme . as_slice ( ) == "https" { 443 } else { 80 } ,
141
141
} ;
@@ -186,7 +186,8 @@ impl<S: Connecter + Reader + Writer = super::NetworkStream> RequestWriter<S> {
186
186
187
187
self . stream = match self . remote_addr {
188
188
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 ) ) ;
190
191
Some ( BufferedStream :: new ( stream) )
191
192
} ,
192
193
None => fail ! ( "connect() called before remote_addr was set" ) ,
@@ -217,12 +218,13 @@ impl<S: Connecter + Reader + Writer = super::NetworkStream> RequestWriter<S> {
217
218
218
219
// Write the Request-Line (RFC2616 §5.1)
219
220
// 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
+ } ;
220
225
try!( write ! ( self . stream. get_mut_ref( ) as & mut Writer ,
221
226
"{} {}{}{} 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) ) ;
226
228
227
229
try!( self . headers . write_all ( self . stream . get_mut_ref ( ) ) ) ;
228
230
self . headers_written = true ;
0 commit comments