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
use futures::Future;
use futures::IntoFuture;
use hyper::header::{ContentLength, ContentType, Header};
use hyper::server::Response as HyperResponse;

use prelude::*;

/// Response represents http response.
pub struct Response {
    /// Hyper original response.
    pub origin: HyperResponse,
}

impl Response {
    /// Creates a fresh response.
    pub fn new() -> Response {
        Response {
            origin: HyperResponse::new(),
        }
    }

    /// Converts Response to HandlerResult.
    /// You need to call render() after building Response.
    pub fn render(self) -> HandlerResult {
        Ok(self).into_future().boxed()
    }

    /// Creates a Response that contains only text.
    pub fn text<T: Into<String>>(text: T) -> Response {
        let mut resp = Response::new();
        let s = text.into();
        resp.origin.headers_mut().set(ContentLength(s.len() as u64));
        resp.origin.headers_mut().set(ContentType::plaintext());
        resp.origin.set_body(s);
        resp
    }

    /// Creates a json Response.
    pub fn json<T: Into<String>>(obj: T) -> Response {
        let mut resp = Response::new();
        let s = obj.into();
        resp.origin.headers_mut().set(ContentLength(s.len() as u64));
        resp.origin.headers_mut().set(ContentType::json());
        resp.origin.set_body(s);
        resp
    }

    /// Creates a Response for redirect.
    pub fn redirect(url: &str) -> Response {
        let mut resp = Response::new();
        resp.origin.set_status(StatusCode::Found);
        resp.origin.headers_mut().set_raw("location", vec![url.as_bytes().to_vec()]);
        resp
    }

    /// Set header in Response.
    /// Use like a builder pattern.
    pub fn with_header<H: Header>(mut self, h: H) -> Response {
        self.origin.headers_mut().set(h);
        self
    }

    /// Set status code in Response.
    /// Use like a builder pattern.
    pub fn with_status(mut self, status: StatusCode) -> Response {
        self.origin.set_status(status);
        self
    }
}