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