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
use std::any::Any;
use std::convert::TryFrom;
use std::ops::{Deref, DerefMut};
use cookie::{Cookie, CookieJar};
use http::{HeaderMap, HeaderValue, Response as RawResponse, response::Builder as RawBuilder, StatusCode, Version};
use http::header::HeaderName;
use hyper::Body;
use crate::error::SaphirError;
/// Struct that wraps a hyper response + some magic
pub struct Response<T> {
inner: RawResponse<T>,
cookies: CookieJar,
}
impl<T> Response<T> {
/// Create a new response with T as body
pub fn new(body: T) -> Self {
Response {
inner: RawResponse::new(body),
cookies: Default::default(),
}
}
/// Get the cookies sent by the browsers
pub fn cookies(&self) -> &CookieJar {
&self.cookies
}
/// Get the cookies sent by the browsers in a mutable way
pub fn cookies_mut(&mut self) -> &mut CookieJar {
&mut self.cookies
}
/// Convert a response of T in a response of U
///
/// ```rust
///# use saphir::prelude::*;
///# use hyper::Response as RawResponse;
///# let mut res = Response::new(());
///
/// // res is Response<()>
/// let res: Response<String> = res.map(|_ignored_body| "New body".to_string());
/// ```
#[inline]
pub fn map<F, U>(self, f: F) -> Response<U>
where
F: FnOnce(T) -> U,
{
let Response { inner, cookies } = self;
Response {
inner: inner.map(f),
cookies,
}
}
pub(crate) fn into_raw(self) -> Result<RawResponse<T>, SaphirError> {
let Response { mut inner, cookies } = self;
for c in cookies.iter() {
inner.headers_mut().append(http::header::SET_COOKIE, http::HeaderValue::from_str(c.to_string().as_str())?);
}
Ok(inner)
}
}
impl<T> Deref for Response<T> {
type Target = RawResponse<T>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T> DerefMut for Response<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
/// Struct used to conveniently build a response
pub struct Builder {
inner: RawBuilder,
cookies: Option<CookieJar>,
body: Box<dyn TransmuteBody + Send + Sync>,
}
impl Builder {
/// Creates a new default instance of `Builder` to construct either a
/// `Head` or a `Response`.
/// ```
/// # use saphir::prelude::*;
///
/// let response = Builder::new()
/// .status(200)
/// .build()
/// .unwrap();
/// ```
#[inline]
pub fn new() -> Self {
Builder {
inner: RawBuilder::new(),
cookies: None,
body: Box::new(Option::<String>::None),
}
}
/// 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`.
/// ```
/// # use saphir::prelude::*;
///
/// let response = Builder::new()
/// .status(200)
/// .build()
/// .unwrap();
/// ```
#[inline]
pub fn status<T>(mut self, status: T) -> Builder
where
StatusCode: TryFrom<T>,
<StatusCode as TryFrom<T>>::Error: Into<http::Error>,
{
self.inner = self.inner.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
///
/// ```
/// # use saphir::prelude::*;
///
/// let response = Builder::new()
/// .version(Version::HTTP_2)
/// .build()
/// .unwrap();
/// ```
#[inline]
pub fn version(mut self, version: Version) -> Builder {
self.inner = self.inner.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`.
///
/// ```
/// # use saphir::prelude::*;
/// # use http::header::HeaderValue;
///
/// let response = Builder::new()
/// .header("Content-Type", "text/html")
/// .header("X-Custom-Foo", "bar")
/// .header("content-length", 0)
/// .build()
/// .unwrap();
/// ```
#[inline]
pub fn header<K, V>(mut self, key: K, value: V) -> Builder
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<http::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
{
self.inner = self.inner.header(key, value);
self
}
/// Get header on this response builder.
///
/// When builder has error returns None.
/// ```
/// # use saphir::prelude::*;
/// # use http::header::HeaderValue;
/// let res = Builder::new()
/// .header("Accept", "text/html")
/// .header("X-Custom-Foo", "bar");
/// let headers = res.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
#[inline]
pub fn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>> {
self.inner.headers_ref()
}
/// Get header on this response builder.
/// when builder has error returns None
/// ```
/// # use http::*;
/// # use http::header::HeaderValue;
/// # use saphir::prelude::*;;
/// let mut res = Builder::new();
/// {
/// let headers = res.headers_mut().unwrap();
/// headers.insert("Accept", HeaderValue::from_static("text/html"));
/// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar"));
/// }
/// let headers = res.headers_ref().unwrap();
/// assert_eq!( headers["Accept"], "text/html" );
/// assert_eq!( headers["X-Custom-Foo"], "bar" );
/// ```
#[inline]
pub fn headers_mut(&mut self) -> Option<&mut HeaderMap<HeaderValue>> {
self.inner.headers_mut()
}
/// Adds an extension to this builder
/// ```
/// # use saphir::prelude::*;
///
/// let response = Builder::new()
/// .extension("My Extension")
/// .build()
/// .unwrap();
///
/// assert_eq!(response.extensions().get::<&'static str>(),
/// Some(&"My Extension"));
/// ```
#[inline]
pub fn extension<T>(mut self, extension: T) -> Builder
where
T: Any + Send + Sync + 'static,
{
self.inner = self.inner.extension(extension);
self
}
/// Adds an extension to this builder
/// ```
/// # use saphir::prelude::*;
///
/// let cookie = Cookie::new("MyCookie", "MyCookieValue");
///
/// let response = Builder::new()
/// .cookie(cookie)
/// .build()
/// .unwrap();
///
/// assert_eq!(response.cookies().get("MyCookie").map(|c| c.value()), Some("MyCookieValue"))
/// ```
#[inline]
pub fn cookie(mut self, cookie: Cookie<'static>) -> Builder {
if self.cookies.is_none() {
self.cookies = Some(CookieJar::new());
}
self.cookies.as_mut().expect("Should not happens").add(cookie);
self
}
#[inline]
pub fn body<B: 'static + Into<Body> + Send + Sync>(mut self, body: B) -> Builder {
self.body = Box::new(Some(body));
self
}
/// Finish the builder into Response<Body>
#[inline]
pub fn build(self) -> Result<Response<Body>, SaphirError> {
let Builder { inner, cookies, mut body } = self;
let b: Body = body.transmute();
let raw = inner.body(b)?;
Ok(Response {
inner: raw,
cookies: cookies.unwrap_or_default(),
})
}
}
#[doc(hidden)]
pub trait TransmuteBody {
fn transmute(&mut self) -> Body;
}
#[doc(hidden)]
impl<T> TransmuteBody for Option<T> where T: Into<Body> {
fn transmute(&mut self) -> Body {
if let Some(b) = self.take() {
b.into()
} else {
Body::empty()
}
}
}