@@ -206,6 +206,9 @@ impl RequestWriter<TcpStream> {
206
206
if self . url. query. len( ) > 0 { "?" } else { "" } ,
207
207
url:: query_to_str( & self . url. query) ) ;
208
208
209
+ // `write_all` adds '\r\n' at the end, no
210
+ // need to terminate the headers section
211
+ // here.
209
212
self . headers . write_all ( & mut self . stream ) ;
210
213
self . headers_written = true ;
211
214
}
@@ -224,14 +227,38 @@ impl RequestWriter<TcpStream> {
224
227
None => Err ( self ) , // TODO: raise condition
225
228
}
226
229
}
230
+
231
+ /// Send data to the remote server.
232
+ /// This method appends Content-Length
233
+ /// to headers and sends them. If headers
234
+ /// where already sent, it will send data
235
+ /// without the Content-Length.
236
+ // TODO: Implement chunked request, perhaps
237
+ // in a `send_chunked` method.
238
+ pub fn send ( & mut self , buf : & [ u8 ] ) {
239
+
240
+ // NOTE: Should we make this fail?
241
+ // If 'Content-Length' is not sent
242
+ // some servers won't read the request
243
+ // body.
244
+ if !self . headers_written {
245
+ self . headers . content_length = Some ( buf. len ( ) ) ;
246
+ self . write_headers ( ) ;
247
+ }
248
+ self . write ( buf) ;
249
+ }
227
250
}
228
251
229
252
/// Write the request body. Note that any calls to `write()` will cause the headers to be sent.
230
253
impl Writer for RequestWriter < TcpStream > {
231
254
fn write ( & mut self , buf : & [ u8 ] ) {
232
- if ( !self . headers_written ) {
233
- self . write_headers ( ) ;
234
- }
255
+ // No data must be sent before
256
+ // sending headers. Let's make
257
+ // sure that's the case.
258
+ self . try_write_headers ( ) ;
259
+
260
+ // Now we're good to send
261
+ // some data.
235
262
self . stream . write ( buf) ;
236
263
}
237
264
0 commit comments