Skip to content

Commit b8eaa86

Browse files
committed
Implement send method to send content along with the request
1 parent 647eb48 commit b8eaa86

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/http/client/request.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ impl RequestWriter<TcpStream> {
206206
if self.url.query.len() > 0 { "?" } else { "" },
207207
url::query_to_str(&self.url.query));
208208

209+
// `write_all` adds '\r\n' at the end, no
210+
// need to terminate the headers section
211+
// here.
209212
self.headers.write_all(&mut self.stream);
210213
self.headers_written = true;
211214
}
@@ -224,14 +227,38 @@ impl RequestWriter<TcpStream> {
224227
None => Err(self), // TODO: raise condition
225228
}
226229
}
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+
}
227250
}
228251

229252
/// Write the request body. Note that any calls to `write()` will cause the headers to be sent.
230253
impl Writer for RequestWriter<TcpStream> {
231254
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.
235262
self.stream.write(buf);
236263
}
237264

0 commit comments

Comments
 (0)