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
use crate::{
body::{AsyncBody, Body},
client::ResponseFuture,
config::{
request::{RequestConfig, WithRequestConfig},
Configurable,
},
error::Error,
};
use http::{Request, Response};
pub trait RequestExt<T> {
fn to_builder(&self) -> http::request::Builder;
fn send(self) -> Result<Response<Body>, Error>
where
T: Into<Body>;
fn send_async(self) -> ResponseFuture<'static>
where
T: Into<AsyncBody>;
}
impl<T> RequestExt<T> for Request<T> {
fn to_builder(&self) -> http::request::Builder {
let mut builder = Request::builder()
.method(self.method().clone())
.uri(self.uri().clone())
.version(self.version());
*builder.headers_mut().unwrap() = self.headers().clone();
if let Some(config) = self.extensions().get::<RequestConfig>() {
builder = builder.extension(config.clone());
}
#[cfg(feature = "cookies")]
{
if let Some(cookie_jar) = self.extensions().get::<crate::cookies::CookieJar>() {
builder = builder.extension(cookie_jar.clone());
}
}
builder
}
fn send(self) -> Result<Response<Body>, Error>
where
T: Into<Body>,
{
crate::send(self)
}
fn send_async(self) -> ResponseFuture<'static>
where
T: Into<AsyncBody>,
{
crate::send_async(self)
}
}
impl Configurable for http::request::Builder {
#[cfg(feature = "cookies")]
fn cookie_jar(self, cookie_jar: crate::cookies::CookieJar) -> Self {
self.extension(cookie_jar)
}
}
impl WithRequestConfig for http::request::Builder {
#[inline]
fn with_config(mut self, f: impl FnOnce(&mut RequestConfig)) -> Self {
if let Some(extensions) = self.extensions_mut() {
if let Some(config) = extensions.get_mut() {
f(config);
} else {
extensions.insert(RequestConfig::default());
f(extensions.get_mut().unwrap());
}
}
self
}
}