forked from rust-bitcoin/corepc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.rs
More file actions
381 lines (335 loc) · 13.2 KB
/
connection.rs
File metadata and controls
381 lines (335 loc) · 13.2 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use core::time::Duration;
use std::env;
use std::io::{self, Read, Write};
use std::net::{TcpStream, ToSocketAddrs};
use std::time::Instant;
use crate::request::ParsedRequest;
use crate::{Error, Method, ResponseLazy};
type UnsecuredStream = TcpStream;
#[cfg(feature = "rustls")]
mod rustls_stream;
#[cfg(feature = "rustls")]
type SecuredStream = rustls_stream::SecuredStream;
pub(crate) enum HttpStream {
Unsecured(UnsecuredStream, Option<Instant>),
#[cfg(feature = "rustls")]
Secured(Box<SecuredStream>, Option<Instant>),
}
impl HttpStream {
fn create_unsecured(reader: UnsecuredStream, timeout_at: Option<Instant>) -> HttpStream {
HttpStream::Unsecured(reader, timeout_at)
}
#[cfg(feature = "rustls")]
fn create_secured(reader: SecuredStream, timeout_at: Option<Instant>) -> HttpStream {
HttpStream::Secured(Box::new(reader), timeout_at)
}
}
fn timeout_err() -> io::Error {
io::Error::new(io::ErrorKind::TimedOut, "the timeout of the request was reached")
}
fn timeout_at_to_duration(timeout_at: Option<Instant>) -> Result<Option<Duration>, io::Error> {
if let Some(timeout_at) = timeout_at {
if let Some(duration) = timeout_at.checked_duration_since(Instant::now()) {
Ok(Some(duration))
} else {
Err(timeout_err())
}
} else {
Ok(None)
}
}
impl Read for HttpStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let timeout = |tcp: &TcpStream, timeout_at: Option<Instant>| -> io::Result<()> {
let _ = tcp.set_read_timeout(timeout_at_to_duration(timeout_at)?);
Ok(())
};
let result = match self {
HttpStream::Unsecured(inner, timeout_at) => {
timeout(inner, *timeout_at)?;
inner.read(buf)
}
#[cfg(feature = "rustls")]
HttpStream::Secured(inner, timeout_at) => {
timeout(inner.get_ref(), *timeout_at)?;
inner.read(buf)
}
};
match result {
Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
// We're a blocking socket, so EWOULDBLOCK indicates a timeout
Err(timeout_err())
}
r => r,
}
}
}
/// An async connection to the server for sending
/// [`Request`](struct.Request.html)s.
#[cfg(feature = "async")]
pub struct AsyncConnection {
request: ParsedRequest,
timeout_at: Option<Instant>,
}
#[cfg(feature = "async")]
impl AsyncConnection {
/// Creates a new `AsyncConnection`.
pub(crate) fn new(request: ParsedRequest) -> AsyncConnection {
let timeout = request.config.timeout.or_else(|| match env::var("BITREQ_TIMEOUT") {
Ok(t) => t.parse::<u64>().ok(),
Err(_) => None,
});
let timeout_at = timeout.map(|t| Instant::now() + Duration::from_secs(t));
AsyncConnection { request, timeout_at }
}
/// Sends the [`Request`](struct.Request.html) asynchronously using HTTPS.
#[cfg(feature = "async-https")]
pub(crate) async fn send_https(self) -> Result<ResponseLazy, Error> {
// Use spawn_blocking to run the sync HTTPS code in a thread pool
let sync_conn = Connection { request: self.request, timeout_at: self.timeout_at };
tokio::task::spawn_blocking(move || sync_conn.send_https())
.await
.map_err(|e| Error::IoError(io::Error::new(io::ErrorKind::Other, e)))?
}
/// Sends the [`Request`](struct.Request.html) asynchronously using HTTP.
pub(crate) async fn send(self) -> Result<ResponseLazy, Error> {
// Use spawn_blocking to run the sync HTTP code in a thread pool
let sync_conn = Connection { request: self.request, timeout_at: self.timeout_at };
tokio::task::spawn_blocking(move || sync_conn.send())
.await
.map_err(|e| Error::IoError(io::Error::new(io::ErrorKind::Other, e)))?
}
}
/// A connection to the server for sending
/// [`Request`](struct.Request.html)s.
pub struct Connection {
request: ParsedRequest,
timeout_at: Option<Instant>,
}
impl Connection {
/// Creates a new `Connection`. See [Request] and [ParsedRequest]
/// for specifics about *what* is being sent.
pub(crate) fn new(request: ParsedRequest) -> Connection {
let timeout = request.config.timeout.or_else(|| match env::var("BITREQ_TIMEOUT") {
Ok(t) => t.parse::<u64>().ok(),
Err(_) => None,
});
let timeout_at = timeout.map(|t| Instant::now() + Duration::from_secs(t));
Connection { request, timeout_at }
}
/// Returns the timeout duration for operations that should end at
/// timeout and are starting "now".
///
/// The Result will be Err if the timeout has already passed.
fn timeout(&self) -> Result<Option<Duration>, io::Error> {
let timeout = timeout_at_to_duration(self.timeout_at);
#[cfg(feature = "log")]
log::trace!("Timeout requested, it is currently: {:?}", timeout);
timeout
}
/// Sends the [`Request`](struct.Request.html), consumes this
/// connection, and returns a [`Response`](struct.Response.html).
#[cfg(feature = "rustls")]
pub(crate) fn send_https(mut self) -> Result<ResponseLazy, Error> {
enforce_timeout(self.timeout_at, move || {
self.request.url.host = ensure_ascii_host(self.request.url.host)?;
let secured_stream = rustls_stream::create_secured_stream(&self)?;
#[cfg(feature = "log")]
log::trace!("Reading HTTPS response from {}.", self.request.url.host);
let response = ResponseLazy::from_stream(
secured_stream,
self.request.config.max_headers_size,
self.request.config.max_status_line_len,
)?;
handle_redirects(self, response)
})
}
/// Sends the [`Request`](struct.Request.html), consumes this
/// connection, and returns a [`Response`](struct.Response.html).
pub(crate) fn send(mut self) -> Result<ResponseLazy, Error> {
enforce_timeout(self.timeout_at, move || {
self.request.url.host = ensure_ascii_host(self.request.url.host)?;
let bytes = self.request.as_bytes();
#[cfg(feature = "log")]
log::trace!("Establishing TCP connection to {}.", self.request.url.host);
let mut tcp = self.connect()?;
// Send request
#[cfg(feature = "log")]
log::trace!("Writing HTTP request.");
let _ = tcp.set_write_timeout(self.timeout()?);
tcp.write_all(&bytes)?;
// Receive response
#[cfg(feature = "log")]
log::trace!("Reading HTTP response.");
let stream = HttpStream::create_unsecured(tcp, self.timeout_at);
let response = ResponseLazy::from_stream(
stream,
self.request.config.max_headers_size,
self.request.config.max_status_line_len,
)?;
handle_redirects(self, response)
})
}
fn connect(&self) -> Result<TcpStream, Error> {
let tcp_connect = |host: &str, port: u32| -> Result<TcpStream, Error> {
let addrs = (host, port as u16).to_socket_addrs().map_err(Error::IoError)?;
let addrs_count = addrs.len();
// Try all resolved addresses. Return the first one to which we could connect. If all
// failed return the last error encountered.
for (i, addr) in addrs.enumerate() {
let stream = if let Some(timeout) = self.timeout()? {
TcpStream::connect_timeout(&addr, timeout)
} else {
TcpStream::connect(addr)
};
if stream.is_ok() || i == addrs_count - 1 {
return stream.map_err(Error::from);
}
}
Err(Error::AddressNotFound)
};
#[cfg(feature = "proxy")]
match self.request.config.proxy {
Some(ref proxy) => {
// do proxy things
let mut tcp = tcp_connect(&proxy.server, proxy.port)?;
write!(tcp, "{}", proxy.connect(&self.request)).unwrap();
tcp.flush()?;
let mut proxy_response = Vec::new();
loop {
let mut buf = vec![0; 256];
let total = tcp.read(&mut buf)?;
proxy_response.append(&mut buf);
if total < 256 {
break;
}
}
crate::Proxy::verify_response(&proxy_response)?;
Ok(tcp)
}
None => tcp_connect(&self.request.url.host, self.request.url.port.port()),
}
#[cfg(not(feature = "proxy"))]
tcp_connect(&self.request.url.host, self.request.url.port.port())
}
}
fn handle_redirects(
connection: Connection,
mut response: ResponseLazy,
) -> Result<ResponseLazy, Error> {
let status_code = response.status_code;
let url = response.headers.get("location");
match get_redirect(connection, status_code, url) {
NextHop::Redirect(connection) => {
let connection = connection?;
if connection.request.url.https {
#[cfg(not(feature = "rustls"))]
return Err(Error::HttpsFeatureNotEnabled);
#[cfg(feature = "rustls")]
return connection.send_https();
} else {
connection.send()
}
}
NextHop::Destination(connection) => {
let dst_url = connection.request.url;
dst_url.write_base_url_to(&mut response.url).unwrap();
dst_url.write_resource_to(&mut response.url).unwrap();
Ok(response)
}
}
}
enum NextHop {
Redirect(Result<Connection, Error>),
Destination(Connection),
}
fn get_redirect(mut connection: Connection, status_code: i32, url: Option<&String>) -> NextHop {
match status_code {
301 | 302 | 303 | 307 => {
let url = match url {
Some(url) => url,
None => return NextHop::Redirect(Err(Error::RedirectLocationMissing)),
};
#[cfg(feature = "log")]
log::debug!("Redirecting ({}) to: {}", status_code, url);
match connection.request.redirect_to(url.as_str()) {
Ok(()) => {
if status_code == 303 {
match connection.request.config.method {
Method::Post | Method::Put | Method::Delete => {
connection.request.config.method = Method::Get;
}
_ => {}
}
}
NextHop::Redirect(Ok(connection))
}
Err(err) => NextHop::Redirect(Err(err)),
}
}
_ => NextHop::Destination(connection),
}
}
fn ensure_ascii_host(host: String) -> Result<String, Error> {
if host.is_ascii() {
Ok(host)
} else {
#[cfg(not(feature = "punycode"))]
{
Err(Error::PunycodeFeatureNotEnabled)
}
#[cfg(feature = "punycode")]
{
let mut result = String::with_capacity(host.len() * 2);
for s in host.split('.') {
if s.is_ascii() {
result += s;
} else {
match punycode::encode(s) {
Ok(s) => result = result + "xn--" + &s,
Err(_) => return Err(Error::PunycodeConversionFailed),
}
}
result += ".";
}
result.truncate(result.len() - 1); // Remove the trailing dot
Ok(result)
}
}
}
/// Enforce the timeout by running the function in a new thread and
/// parking the current one with a timeout.
///
/// While bitreq does use timeouts (somewhat) properly, some
/// interfaces such as [ToSocketAddrs] don't allow for specifying the
/// timeout. Hence this.
fn enforce_timeout<F, R>(timeout_at: Option<Instant>, f: F) -> Result<R, Error>
where
F: 'static + Send + FnOnce() -> Result<R, Error>,
R: 'static + Send,
{
use std::sync::mpsc::{channel, RecvTimeoutError};
match timeout_at {
Some(deadline) => {
let (sender, receiver) = channel();
let thread = std::thread::spawn(move || {
let result = f();
let _ = sender.send(());
result
});
if let Some(timeout_duration) = deadline.checked_duration_since(Instant::now()) {
match receiver.recv_timeout(timeout_duration) {
Ok(()) => thread.join().unwrap(),
Err(err) => match err {
RecvTimeoutError::Timeout => Err(Error::IoError(timeout_err())),
RecvTimeoutError::Disconnected =>
Err(Error::Other("request connection paniced")),
},
}
} else {
Err(Error::IoError(timeout_err()))
}
}
None => f(),
}
}