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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
pub use http_types::header::HeaderName;
pub use http_types::header::HeaderValue;
pub use http_types::request::Parts as ReqParts;
pub use http_types::response::Parts as ResParts;
pub use http_types::response::Builder as ResponseBuilder;
pub use http_types::Extensions;
pub use hyper::Method;
pub use hyper::Uri;
pub use hyper::Version;
pub use hyper::HeaderMap;
pub use hyper::Body;
pub use hyper::body::Payload;
pub use hyper::StatusCode;
pub use hyper::service::Service;
pub use hyper::service::service_fn;
pub use hyper::Request;
pub use hyper::Response;
pub use hyper::Server as HyperServer;
use futures::Future;
use futures::Stream;
use http_types::HttpTryFrom;
use std::any::Any;

static EMPTY_BODY: &[u8] = b"";

/// A Structure which represent an http request with a fully loaded body
#[derive(Debug)]
pub struct SyncRequest {
    /// Method
    head: ReqParts,
    /// Body
    body: Vec<u8>,
}

impl SyncRequest {
    /// Construct a new Request.
    #[inline]
    pub fn new(head: ReqParts,
               body: Vec<u8>,
    ) -> SyncRequest {
        SyncRequest {
            head,
            body,
        }
    }

    /// Returns a reference to the associated HTTP method.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let request: Request<()> = Request::default();
    /// assert_eq!(*request.method(), Method::GET);
    /// ```
    #[inline]
    pub fn method(&self) -> &Method {
        &self.head.method
    }

    /// Returns a mutable reference to the associated HTTP method.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let mut request: Request<()> = Request::default();
    /// *request.method_mut() = Method::PUT;
    /// assert_eq!(*request.method(), Method::PUT);
    /// ```
    #[inline]
    pub fn method_mut(&mut self) -> &mut Method {
        &mut self.head.method
    }

    /// Returns a reference to the associated URI.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let request: Request<()> = Request::default();
    /// assert_eq!(*request.uri(), *"/");
    /// ```
    #[inline]
    pub fn uri(&self) -> &Uri {
        &self.head.uri
    }

    /// Returns a mutable reference to the associated URI.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let mut request: Request<()> = Request::default();
    /// *request.uri_mut() = "/hello".parse().unwrap();
    /// assert_eq!(*request.uri(), *"/hello");
    /// ```
    #[inline]
    pub fn uri_mut(&mut self) -> &mut Uri {
        &mut self.head.uri
    }

    /// Returns the associated version.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let request: Request<()> = Request::default();
    /// assert_eq!(request.version(), Version::HTTP_11);
    /// ```
    #[inline]
    pub fn version(&self) -> Version {
        self.head.version
    }

    /// Returns a mutable reference to the associated version.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let mut request: Request<()> = Request::default();
    /// *request.version_mut() = Version::HTTP_2;
    /// assert_eq!(request.version(), Version::HTTP_2);
    /// ```
    #[inline]
    pub fn version_mut(&mut self) -> &mut Version {
        &mut self.head.version
    }

    /// Returns a reference to the associated header field map.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let request: Request<()> = Request::default();
    /// assert!(request.headers().is_empty());
    /// ```
    #[inline]
    pub fn headers(&self) -> &HeaderMap<HeaderValue> {
        &self.head.headers
    }

    /// Returns a mutable reference to the associated header field map.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// # use http::header::*;
    /// let mut request: Request<()> = Request::default();
    /// request.headers_mut().insert(HOST, HeaderValue::from_static("world"));
    /// assert!(!request.headers().is_empty());
    /// ```
    #[inline]
    pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue> {
        &mut self.head.headers
    }


    /// Returns a reference to the associated extensions.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let request: Request<()> = Request::default();
    /// assert!(request.extensions().get::<i32>().is_none());
    /// ```
    #[inline]
    pub fn extensions(&self) -> &Extensions {
        &self.head.extensions
    }

    /// Returns a mutable reference to the associated extensions.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// # use http::header::*;
    /// let mut request: Request<()> = Request::default();
    /// request.extensions_mut().insert("hello");
    /// assert_eq!(request.extensions().get(), Some(&"hello"));
    /// ```
    #[inline]
    pub fn extensions_mut(&mut self) -> &mut Extensions {
        &mut self.head.extensions
    }

    /// Returns a reference to the associated HTTP body.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let request: Request<String> = Request::default();
    /// assert!(request.body().is_empty());
    /// ```
    #[inline]
    pub fn body(&self) -> &Vec<u8> {
        &self.body
    }

    /// Returns a mutable reference to the associated HTTP body.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// let mut request: Request<String> = Request::default();
    /// request.body_mut().push_str("hello world");
    /// assert!(!request.body().is_empty());
    /// ```
    #[inline]
    pub fn body_mut(&mut self) -> &mut Vec<u8> {
        &mut self.body
    }
}

/// 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> + Send>;
}

impl LoadBody for Request<Body> {
    fn load_body(self) -> Box<Future<Item=SyncRequest, Error=::hyper::Error> + Send> {
        let (parts, body) = self.into_parts();
        if body.content_length().unwrap_or_else(|| {0}) > 0 {
            Box::new(body.concat2().map(move |b| {
                let body_vec: Vec<u8> = b.to_vec();
                SyncRequest::new(parts, body_vec)
            }))
        } else {
            Box::new(::futures::future::ok( SyncRequest::new(parts, Vec::with_capacity(0))))
        }
    }
}

/// A Structure which represent a fully mutable http response
pub struct SyncResponse {
    builder: ResponseBuilder,
    body: Box<ToBody>,
}

impl SyncResponse {

    ///
    pub fn new() -> Self{
        SyncResponse {
            builder: ResponseBuilder::new(),
            body: Box::new(EMPTY_BODY),
        }
    }

    /// Set the HTTP status for this response.
    ///
    /// This function will configure the HTTP status code of the `Response` that
    /// will be returned from `Builder::build`.
    ///
    /// By default this is `200`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    ///
    /// let response = SyncResponse::new()
    ///     .status(200)
    ///     .build_response()
    ///     .unwrap();
    /// ```
    pub fn status<T>(&mut self, status: T) -> &mut SyncResponse
        where StatusCode: HttpTryFrom<T>,
    {
        self.builder.status(status);
        self
    }

    /// Set the HTTP version for this response.
    ///
    /// This function will configure the HTTP version of the `Response` that
    /// will be returned from `Builder::build`.
    ///
    /// By default this is HTTP/1.1
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    ///
    /// let response = SyncResponse::new()
    ///     .version(Version::HTTP_2)
    ///     .build_response()
    ///     .unwrap();
    /// ```
    pub fn version(&mut self, version: Version) -> &mut SyncResponse {
        self.builder.version(version);
        self
    }

    /// Appends a header to this response builder.
    ///
    /// This function will append the provided key/value as a header to the
    /// internal `HeaderMap` being constructed. Essentially this is equivalent
    /// to calling `HeaderMap::append`.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    /// # use http::header::HeaderValue;
    ///
    /// let response = SyncResponse::new()
    ///     .header("Content-Type", "text/html")
    ///     .header("X-Custom-Foo", "bar")
    ///     .build_response()
    ///     .unwrap();
    /// ```
    pub fn header<K, V>(&mut self, key: K, value: V) -> &mut SyncResponse
        where HeaderName: HttpTryFrom<K>,
              HeaderValue: HttpTryFrom<V>
    {
        self.builder.header(key, value);
        self
    }

    /// Adds an extension to this builder
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    ///
    /// let response = SyncResponse::new()
    ///     .extension("My Extension")
    ///     .build_response()
    ///     .unwrap();
    ///
    /// assert_eq!(response.extensions().get::<&'static str>(),
    ///            Some(&"My Extension"));
    /// ```
    pub fn extension<T>(&mut self, extension: T) -> &mut SyncResponse
        where T: Any + Send + Sync + 'static,
    {
        self.builder.extension(extension);
        self
    }

    /// Adds a body to a response
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use http::*;
    ///
    /// let response = SyncResponse::new()
    ///     .body(b"this is a payload")
    ///     .build_response()
    ///     .unwrap();
    /// ```
    pub fn body<B: 'static + ToBody>(&mut self, body: B) -> &mut SyncResponse {
        self.body = Box::new(body);
        self
    }

    ///
    pub fn build_response(self) -> Result<Response<Body>, ::http_types::Error> {
        let SyncResponse { mut builder, body } = self;
        let b: Body = body.to_body();
        builder.body(b)
    }
}

///
pub trait ToBody {
    ///
    fn to_body(&self) -> Body;
}

impl<I> ToBody for I where I: Into<Body> + Clone {
    fn to_body(&self) -> Body {
        self.clone().into()
    }
}