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
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use bytes::Bytes;
use futures::Future;
use futures::FutureExt;
use futures::Stream;
use futures::StreamExt;
use http;
use http::Request;
use http::Response;
use http_body_util::combinators::UnsyncBoxBody;
use hyper_util::rt::TokioIo;
use std::convert::Infallible;
use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use std::result::Result;
use tokio::net::TcpListener;
#[derive(Debug, Clone, Copy)]
pub enum ServerKind {
Auto,
OnlyHttp1,
OnlyHttp2,
}
#[derive(Debug, Clone, Copy)]
pub struct ServerOptions {
pub error_msg: &'static str,
pub addr: SocketAddr,
pub kind: ServerKind,
}
pub type HandlerOutput =
Result<Response<UnsyncBoxBody<Bytes, Infallible>>, anyhow::Error>;
pub async fn run_server<F, S>(options: ServerOptions, handler: F)
where
F: Fn(Request<hyper::body::Incoming>) -> S + Copy + 'static,
S: Future<Output = HandlerOutput> + 'static,
{
let fut: Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>> =
async move {
let listener = TcpListener::bind(options.addr).await?;
println!("ready: {}", options.addr);
loop {
let (stream, _) = listener.accept().await?;
let io = TokioIo::new(stream);
deno_unsync::spawn(hyper_serve_connection(
io,
handler,
options.error_msg,
options.kind,
));
}
}
.boxed_local();
if let Err(e) = fut.await {
let err_str = e.to_string();
if !err_str.contains("early eof") {
eprintln!("{}: {:?}", options.error_msg, e);
}
}
}
pub async fn run_server_with_acceptor<'a, A, F, S>(
mut acceptor: Pin<Box<A>>,
handler: F,
error_msg: &'static str,
kind: ServerKind,
) where
A: Stream<Item = io::Result<rustls_tokio_stream::TlsStream>> + ?Sized,
F: Fn(Request<hyper::body::Incoming>) -> S + Copy + 'static,
S: Future<Output = HandlerOutput> + 'static,
{
let fut: Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>> =
async move {
while let Some(result) = acceptor.next().await {
let stream = result?;
let io = TokioIo::new(stream);
deno_unsync::spawn(hyper_serve_connection(
io, handler, error_msg, kind,
));
}
Ok(())
}
.boxed_local();
if let Err(e) = fut.await {
let err_str = e.to_string();
if !err_str.contains("early eof") {
eprintln!("{}: {:?}", error_msg, e);
}
}
}
async fn hyper_serve_connection<I, F, S>(
io: I,
handler: F,
error_msg: &'static str,
kind: ServerKind,
) where
I: hyper::rt::Read + hyper::rt::Write + Unpin + 'static,
F: Fn(Request<hyper::body::Incoming>) -> S + Copy + 'static,
S: Future<Output = HandlerOutput> + 'static,
{
let service = hyper::service::service_fn(handler);
let result: Result<(), anyhow::Error> = match kind {
ServerKind::Auto => {
let builder =
hyper_util::server::conn::auto::Builder::new(DenoUnsyncExecutor);
builder
.serve_connection(io, service)
.await
.map_err(|e| anyhow::anyhow!("{:?}", e))
}
ServerKind::OnlyHttp1 => {
let builder = hyper::server::conn::http1::Builder::new();
builder
.serve_connection(io, service)
.await
.map_err(|e| e.into())
}
ServerKind::OnlyHttp2 => {
let builder =
hyper::server::conn::http2::Builder::new(DenoUnsyncExecutor);
builder
.serve_connection(io, service)
.await
.map_err(|e| e.into())
}
};
if let Err(e) = result {
let err_str = e.to_string();
if !err_str.contains("early eof") {
eprintln!("{}: {:?}", error_msg, e);
}
}
}
#[derive(Clone)]
struct DenoUnsyncExecutor;
impl<Fut> hyper::rt::Executor<Fut> for DenoUnsyncExecutor
where
Fut: Future + 'static,
Fut::Output: 'static,
{
fn execute(&self, fut: Fut) {
deno_unsync::spawn(fut);
}
}
|