use client::{HttpRequest, HttpStream};
use std::io;
use std::io::prelude::*;
pub struct SizedRequest<R> {
inner: R,
buffer: Vec<u8>,
boundary: String,
}
impl<R: HttpRequest> SizedRequest<R> {
#[doc(hidden)]
pub fn from_request(req: R) -> SizedRequest<R> {
SizedRequest {
inner: req,
buffer: Vec::new(),
boundary: String::new(),
}
}
}
impl<R> Write for SizedRequest<R> {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.buffer.write(data)
}
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
impl<R: HttpRequest> HttpRequest for SizedRequest<R>
where <R::Stream as HttpStream>::Error: From<R::Error> {
type Stream = Self;
type Error = R::Error;
fn apply_headers(&mut self, boundary: &str, _content_len: Option<u64>) -> bool {
self.boundary.clear();
self.boundary.push_str(boundary);
true
}
fn open_stream(mut self) -> Result<Self, Self::Error> {
self.buffer.clear();
Ok(self)
}
}
impl<R: HttpRequest> HttpStream for SizedRequest<R>
where <R::Stream as HttpStream>::Error: From<R::Error> {
type Request = Self;
type Response = <<R as HttpRequest>::Stream as HttpStream>::Response;
type Error = <<R as HttpRequest>::Stream as HttpStream>::Error;
fn finish(mut self) -> Result<Self::Response, Self::Error> {
let content_len = self.buffer.len() as u64;
if !self.inner.apply_headers(&self.boundary, Some(content_len)) {
return Err(io::Error::new(
io::ErrorKind::Other,
"SizedRequest failed to apply headers to wrapped request."
).into());
}
let mut req = try!(self.inner.open_stream());
try!(io::copy(&mut &self.buffer[..], &mut req));
req.finish().into()
}
}