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
use std::env;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::str::FromStr;
use std::sync::Arc;
use futures::TryStreamExt;
use http::Request;
use http::Response;
use log::debug;
use reqwest::redirect::Policy;
use reqwest::ClientBuilder;
use reqwest::Url;
use super::body::IncomingAsyncBody;
use super::dns::*;
use super::parse_content_length;
use super::AsyncBody;
use super::Body;
use crate::Error;
use crate::ErrorKind;
use crate::Result;
#[derive(Clone)]
pub struct HttpClient {
async_client: reqwest::Client,
sync_client: ureq::Agent,
}
impl Debug for HttpClient {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HttpClient").finish()
}
}
impl HttpClient {
pub fn new() -> Result<Self> {
let async_client = {
let mut builder = ClientBuilder::new();
builder = builder.no_gzip();
builder = builder.no_brotli();
builder = builder.no_deflate();
builder = builder.redirect(Policy::none());
#[cfg(feature = "trust-dns")]
let builder = builder.dns_resolver(Arc::new(AsyncTrustDnsResolver::new().unwrap()));
#[cfg(not(feature = "trust-dns"))]
let builder = builder.dns_resolver(Arc::new(AsyncStdDnsResolver::default()));
builder.build().map_err(|err| {
Error::new(ErrorKind::Unexpected, "async client build failed").set_source(err)
})?
};
let sync_client = {
let mut builder = ureq::AgentBuilder::new();
for key in ["http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY"] {
if let Ok(proxy) = env::var(key) {
if let Ok(proxy) = ureq::Proxy::new(proxy) {
debug!("sync client: set proxy to {proxy:?}");
builder = builder.proxy(proxy);
}
}
}
let builder = builder.resolver(StdDnsResolver::default());
builder.build()
};
Ok(HttpClient {
async_client,
sync_client,
})
}
pub fn with_client(async_client: reqwest::Client, sync_client: ureq::Agent) -> Self {
Self {
async_client,
sync_client,
}
}
pub fn async_client(&self) -> reqwest::Client {
self.async_client.clone()
}
pub fn sync_client(&self) -> ureq::Agent {
self.sync_client.clone()
}
pub fn send(&self, req: Request<Body>) -> Result<Response<Body>> {
let (parts, body) = req.into_parts();
let mut ur = self
.sync_client
.request(parts.method.as_str(), &parts.uri.to_string());
for (k, v) in parts.headers.iter() {
ur = ur.set(k.as_str(), v.to_str().expect("must be valid header"));
}
let resp = match ur.send(body) {
Ok(resp) => resp,
Err(err_resp) => match err_resp {
ureq::Error::Status(_code, resp) => resp,
ureq::Error::Transport(transport) => {
let is_temporary = matches!(
transport.kind(),
ureq::ErrorKind::Dns
| ureq::ErrorKind::ConnectionFailed
| ureq::ErrorKind::Io
);
let mut err = Error::new(ErrorKind::Unexpected, "send blocking request")
.with_operation("http_util::Client::send")
.set_source(transport);
if is_temporary {
err = err.set_temporary();
}
return Err(err);
}
},
};
let mut hr = Response::builder().status(resp.status());
for name in resp.headers_names() {
if let Some(value) = resp.header(&name) {
hr = hr.header(name, value);
}
}
let resp = hr
.body(Body::Reader(Box::new(resp.into_reader())))
.expect("response must build succeed");
Ok(resp)
}
pub async fn send_async(&self, req: Request<AsyncBody>) -> Result<Response<IncomingAsyncBody>> {
let is_head = req.method() == http::Method::HEAD;
let (parts, body) = req.into_parts();
let mut req_builder = self
.async_client
.request(
parts.method,
Url::from_str(&parts.uri.to_string()).expect("input request url must be valid"),
)
.version(parts.version)
.headers(parts.headers);
req_builder = if let AsyncBody::Multipart(field, r) = body {
let mut form = reqwest::multipart::Form::new();
let part = reqwest::multipart::Part::stream(AsyncBody::Bytes(r));
form = form.part(field, part);
req_builder.multipart(form)
} else {
req_builder.body(body)
};
let resp = req_builder.send().await.map_err(|err| {
let is_temporary = !(
err.is_builder() ||
err.is_redirect() ||
err.is_status()
);
let mut oerr = Error::new(ErrorKind::Unexpected, "send async request")
.with_operation("http_util::Client::send_async")
.set_source(err);
if is_temporary {
oerr = oerr.set_temporary();
}
oerr
})?;
let content_length = if is_head {
None
} else {
parse_content_length(resp.headers()).expect("response content length must be valid")
};
let mut hr = Response::builder()
.version(resp.version())
.status(resp.status());
for (k, v) in resp.headers().iter() {
hr = hr.header(k, v);
}
let stream = resp.bytes_stream().map_err(|err| {
Error::new(ErrorKind::Unexpected, "read data from http stream")
.map(|v| if err.is_body() { v.set_temporary() } else { v })
.set_source(err)
});
let body = IncomingAsyncBody::new(Box::new(stream), content_length);
let resp = hr.body(body).expect("response must build succeed");
Ok(resp)
}
}