1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
pub use hyper::Method;
pub use hyper::Uri;
pub use hyper::HttpVersion;
pub use hyper::Headers;
pub use hyper::Response;
pub use hyper::Body;
pub use hyper::StatusCode;
pub use hyper::server::Http;
pub use hyper::server::Service;
pub use hyper::Request;
use futures::Future;
use futures::Stream;

/// A Structure which represent an http request with a fully loaded body
#[derive(Debug)]
pub struct SyncRequest {
    /// Method
    method: Method,
    /// Uri
    uri: Uri,
    /// Version
    version: HttpVersion,
    /// Headers
    headers: Headers,
    /// Body
    body: Vec<u8>,
}

impl SyncRequest {
    /// Construct a new Request.
    #[inline]
    pub fn new(method: Method,
               uri: Uri,
               version: HttpVersion,
               headers: Headers,
               body: Vec<u8>,
    ) -> SyncRequest {
        SyncRequest {
            method,
            uri,
            version,
            headers,
            body,
        }
    }

    /// Read the Request Uri.
    #[inline]
    pub fn uri(&self) -> &Uri { &self.uri }

    /// Read the Request Version.
    #[inline]
    pub fn version(&self) -> HttpVersion { self.version }

    /// Read the Request headers.
    #[inline]
    pub fn headers(&self) -> &Headers { &self.headers }

    /// Read the Request method.
    #[inline]
    pub fn method(&self) -> &Method { &self.method }

    /// Read the Request body.
    #[inline]
    pub fn body_ref(&self) -> &Vec<u8> { self.body.as_ref() }

    /// Get a mutable reference to the Request body.
    #[inline]
    pub fn body_mut(&mut self) -> &mut Vec<u8> { &mut self.body }

    /// The target path of this Request.
    #[inline]
    pub fn path(&self) -> &str {
        self.uri.path()
    }

    /// The query string of this Request.
    #[inline]
    pub fn query(&self) -> Option<&str> {
        self.uri.query()
    }

    /// Get a mutable reference to the Request headers.
    #[inline]
    pub fn headers_mut(&mut self) -> &mut Headers { &mut self.headers }
}

/// A trait allowing the implicit conversion of a Hyper::Request into a SyncRequest
pub trait LoadBody {
    ///
    fn load_body(self) -> Box<Future<Item=SyncRequest, Error=::hyper::Error>>;
}

impl LoadBody for Request<Body> {
    fn load_body(self) -> Box<Future<Item=SyncRequest, Error=::hyper::Error>> {
        let (method, uri, version, headers, body) = self.deconstruct();
        Box::new(body.concat2().map(move |b| {
            let body_vec: Vec<u8> = b.to_vec();
            SyncRequest::new(method, uri, version, headers, body_vec)
        }))
    }
}