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
use crate::{Config, ConfigExt};
use futures_lite::{AsyncRead, AsyncWrite};
use std::{
future::{ready, Future},
io::Result,
net::IpAddr,
pin::Pin,
sync::Arc,
};
use trillium::{Handler, Info};
use trillium_http::Stopper;
use trillium_tls_common::Acceptor;
pub trait Server: Send + 'static {
type Listener: Send + Sync + 'static;
type Transport: AsyncRead + AsyncWrite + Unpin + Send + Sync + 'static;
const DESCRIPTION: &'static str;
fn accept(
listener: &mut Self::Listener,
) -> Pin<Box<dyn Future<Output = Result<Self::Transport>> + Send + '_>>;
#[allow(unused_variables)]
fn peer_ip(transport: &Self::Transport) -> Option<IpAddr> {
None
}
#[allow(unused_variables)]
fn set_nodelay(transport: &mut Self::Transport, nodelay: bool) {}
#[allow(unused_variables)]
fn set_ip_ttl(transport: &mut Self::Transport, ttl: u32) {}
fn info(listener: &Self::Listener) -> Info;
fn clean_up(_listener: Self::Listener) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
Box::pin(ready(()))
}
#[cfg(unix)]
fn build_listener<A>(config: &Config<Self, A>) -> Self::Listener
where
A: Acceptor<Self::Transport>,
{
use std::os::unix::prelude::FromRawFd;
let host = config.host();
if host.starts_with(|c| c == '/' || c == '.' || c == '~') {
Self::listener_from_unix(std::os::unix::net::UnixListener::bind(host).unwrap())
} else {
let tcp_listener = if let Some(fd) = std::env::var("LISTEN_FD")
.ok()
.and_then(|fd| fd.parse().ok())
{
log::debug!("using fd {} from LISTEN_FD", fd);
unsafe { std::net::TcpListener::from_raw_fd(fd) }
} else {
std::net::TcpListener::bind((host, config.port())).unwrap()
};
tcp_listener.set_nonblocking(true).unwrap();
Self::listener_from_tcp(tcp_listener)
}
}
#[cfg(not(unix))]
fn build_listener<A>(config: &Config<Self, A>) -> Self::Listener
where
A: Acceptor<Self::Transport>,
{
let tcp_listener = std::net::TcpListener::bind((config.host(), config.port())).unwrap();
tcp_listener.set_nonblocking(true).unwrap();
Self::listener_from_tcp(tcp_listener)
}
fn listener_from_tcp(_tcp: std::net::TcpListener) -> Self::Listener {
unimplemented!()
}
#[cfg(unix)]
fn listener_from_unix(_tcp: std::os::unix::net::UnixListener) -> Self::Listener {
unimplemented!()
}
fn handle_signals(_stopper: Stopper) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
Box::pin(ready(()))
}
fn spawn(fut: impl Future<Output = ()> + Send + 'static);
fn block_on(fut: impl Future<Output = ()> + 'static);
fn run<A, H>(config: Config<Self, A>, handler: H)
where
A: Acceptor<Self::Transport>,
H: Handler,
{
Self::block_on(Self::run_async(config, handler))
}
fn run_async<A, H>(
config: Config<Self, A>,
mut handler: H,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
where
A: Acceptor<Self::Transport>,
H: Handler,
{
Box::pin(async move {
if config.should_register_signals() {
#[cfg(unix)]
Self::spawn(Self::handle_signals(config.stopper()));
#[cfg(not(unix))]
log::error!("signals handling not supported on windows yet");
}
let mut listener = Self::build_listener(&config);
let mut info = Self::info(&listener);
info.server_description_mut().push_str(Self::DESCRIPTION);
handler.init(&mut info).await;
let handler = Arc::new(handler);
let stopper = config.stopper.clone();
while let Some(stream) = stopper.stop_future(Self::accept(&mut listener)).await {
match stream {
Ok(stream) => {
Self::spawn(config.clone().handle_stream(stream, handler.clone()))
}
Err(e) => log::error!("tcp error: {}", e),
}
}
config.graceful_shutdown().await;
Self::clean_up(listener).await;
})
}
}