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
use std::fmt;
use header;
use http::{self, Body};
use status::StatusCode;
use version;
#[derive(Default)]
pub struct Response {
head: http::MessageHead<StatusCode>,
body: Option<Body>,
}
impl Response {
#[inline]
pub fn new() -> Response {
Response::default()
}
#[inline]
pub fn headers(&self) -> &header::Headers { &self.head.headers }
#[inline]
pub fn status(&self) -> &StatusCode {
&self.head.subject
}
#[inline]
pub fn version(&self) -> &version::HttpVersion { &self.head.version }
#[inline]
pub fn headers_mut(&mut self) -> &mut header::Headers { &mut self.head.headers }
#[inline]
pub fn set_status(&mut self, status: StatusCode) {
self.head.subject = status;
}
#[inline]
pub fn set_body<T: Into<Body>>(&mut self, body: T) {
self.body = Some(body.into());
}
#[inline]
pub fn with_status(mut self, status: StatusCode) -> Self {
self.set_status(status);
self
}
#[inline]
pub fn with_header<H: header::Header>(mut self, header: H) -> Self {
self.head.headers.set(header);
self
}
#[inline]
pub fn with_headers(mut self, headers: header::Headers) -> Self {
self.head.headers = headers;
self
}
#[inline]
pub fn with_body<T: Into<Body>>(mut self, body: T) -> Self {
self.set_body(body);
self
}
}
impl fmt::Debug for Response {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Response")
.field("status", &self.head.subject)
.field("version", &self.head.version)
.field("headers", &self.head.headers)
.finish()
}
}
pub fn split(res: Response) -> (http::MessageHead<StatusCode>, Option<Body>) {
(res.head, res.body)
}