cyfs_util/interface/
http_interface.rs1use super::http_tcp_listener::*;
2use cyfs_base::*;
3
4use std::sync::Arc;
5
6#[derive(Clone, Debug)]
7pub enum HttpInterfaceHost {
8 Local,
10
11 Unspecified,
13
14 Specified(Vec<IpAddr>),
15}
16
17impl Default for HttpInterfaceHost {
18 fn default() -> Self {
19 Self::Local
20 }
21}
22
23impl std::str::FromStr for HttpInterfaceHost {
24 type Err = BuckyError;
25 fn from_str(s: &str) -> Result<Self, Self::Err> {
26 let ret = match s {
27 "local" | "l" => Self::Local,
28 "unspecified" | "u" => Self::Unspecified,
29 _ => {
30 let list: Vec<_> = s.split(",").collect();
31 let mut addrs = vec![];
32 for v in list {
33 let addr = IpAddr::from_str(v).map_err(|e| {
34 let msg = format!("invalid ip addr: {}, {}", v, e);
35 error!("{}", msg);
36 BuckyError::new(BuckyErrorCode::InvalidParam, msg)
37 })?;
38 addrs.push(addr);
39 }
40
41 Self::Specified(addrs)
42 }
43 };
44
45 Ok(ret)
46 }
47}
48
49pub struct HttpInterface {
50 list: Vec<HttpTcpListener>,
51}
52
53impl HttpInterface {
54 pub fn new(host: HttpInterfaceHost, port: u16, server: tide::Server<()>) -> Self {
55 let server = Arc::new(server);
56 let mut list = vec![];
57 match host {
58 HttpInterfaceHost::Local => {
59 let addr = std::net::SocketAddr::new(
60 std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST),
61 port,
62 );
63
64 let interface = HttpTcpListener::new_with_raw_server(addr, server);
65 list.push(interface);
66 }
67 HttpInterfaceHost::Unspecified => {
68 let addr = std::net::SocketAddr::new(
69 std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED),
70 port,
71 );
72
73 let interface = HttpTcpListener::new_with_raw_server(addr, server);
74 list.push(interface);
75 }
76 HttpInterfaceHost::Specified(addrs) => {
77 for addr in addrs {
78 let addr = std::net::SocketAddr::new(addr, port);
79
80 let interface = HttpTcpListener::new_with_raw_server(addr, server.clone());
81 list.push(interface);
82 }
83 }
84 }
85
86 Self { list }
87 }
88
89 pub async fn start(&self) -> BuckyResult<()> {
90 for interface in &self.list {
91 interface.start().await?;
92 }
93
94 Ok(())
95 }
96}