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
use super::requestor::*;
use cyfs_base::*;
use async_std::net::{SocketAddr, TcpStream};
use http_types::{Request, Response};
use std::str::FromStr;
#[derive(Clone)]
pub struct TcpHttpRequestor {
service_addr: SocketAddr,
}
impl TcpHttpRequestor {
pub fn new(service_addr: &str) -> Self {
let service_addr = SocketAddr::from_str(&service_addr).unwrap();
Self { service_addr }
}
}
#[async_trait::async_trait]
impl HttpRequestor for TcpHttpRequestor {
async fn request_ext(
&self,
req: &mut Option<Request>,
conn_info: Option<&mut HttpRequestConnectionInfo>,
) -> BuckyResult<Response> {
debug!(
"will http-local request to {}, url={}",
self.remote_addr(),
req.as_ref().unwrap().url()
);
let begin = std::time::Instant::now();
let tcp_stream = TcpStream::connect(self.service_addr).await.map_err(|e| {
let msg = format!(
"tcp connect to {} error! during={}ms, {}",
self.service_addr,
begin.elapsed().as_millis(),
e
);
error!("{}", msg);
BuckyError::new(BuckyErrorCode::ConnectFailed, msg)
})?;
info!(
"tcp connect to {} success, during={}ms",
self.remote_addr(),
begin.elapsed().as_millis(),
);
if let Some(conn_info) = conn_info {
*conn_info = HttpRequestConnectionInfo::Tcp((
tcp_stream.local_addr().unwrap(),
tcp_stream.peer_addr().unwrap(),
));
}
match async_h1::connect(tcp_stream, req.take().unwrap()).await {
Ok(resp) => {
info!(
"http-tcp request to {} success! during={}ms",
self.remote_addr(),
begin.elapsed().as_millis()
);
Ok(resp)
}
Err(e) => {
let msg = format!(
"http-tcp request to {} failed! during={}ms, {}",
self.remote_addr(),
begin.elapsed().as_millis(),
e,
);
error!("{}", msg);
Err(BuckyError::from(msg))
}
}
}
fn remote_addr(&self) -> String {
self.service_addr.to_string()
}
fn remote_device(&self) -> Option<DeviceId> {
None
}
fn clone_requestor(&self) -> Box<dyn HttpRequestor> {
Box::new(self.clone())
}
async fn stop(&self) {
}
}