Body conversion for File

This commit is contained in:
Anthony Nowell
2016-11-08 01:14:18 -07:00
parent 9677cfe419
commit 45d6169610
2 changed files with 20 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
use std::io::Read;
use std::fs::File;
/// Body type for a request.
pub struct Body {
@@ -60,19 +61,33 @@ impl<'a> From<&'a str> for Body {
}
}
impl From<File> for Body {
#[inline]
fn from(f: File) -> Body {
let len = f.metadata().map(|m| m.len()).ok();
Body {
reader: Kind::Reader(Box::new(f), len),
}
}
}
// Wraps a `std::io::Write`.
//pub struct Pipe(Kind);
pub fn as_hyper_body<'a>(body: &'a Body) -> ::hyper::client::Body<'a> {
pub fn as_hyper_body<'a>(body: &'a mut Body) -> ::hyper::client::Body<'a> {
match body.reader {
Kind::Bytes(ref bytes) => {
let len = bytes.len();
::hyper::client::Body::BufBody(bytes, len)
},
Kind::Reader(..) => unimplemented!()
}
Kind::Reader(ref mut reader, len_opt) => {
match len_opt {
Some(len) => ::hyper::client::Body::SizedBody(reader, len),
None => ::hyper::client::Body::ChunkedBody(reader),
}
}
}
}

View File

@@ -167,8 +167,8 @@ impl<'a> RequestBuilder<'a> {
let mut req = client.inner.request(method.clone(), url.clone())
.headers(headers.clone());
if let Some(ref b) = body {
let body = body::as_hyper_body(&b);
if let Some(ref mut b) = body {
let body = body::as_hyper_body(b);
req = req.body(body);
}