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
use std::net::SocketAddr;
use std::sync::Arc;
use bytes::Bytes;
use h3::error::ErrorLevel;
use h3::quic::BidiStream;
use h3::server::RequestStream;
use http::Method;
use http::Request;
use http::StatusCode;
use hyper::Body;
// use tracing::{error, info, trace_span};
use urlpattern::UrlPatternMatchInput;
use crate::guard::Guard;
use crate::server::http3_stream_wrapper::StreamWrapper;
use crate::web_server::ChiteyError;
use crate::web_server::Factories;
use super::util::TlsCertsKey;
use super::util::throw_chitey_internal_server_error;
#[derive(Clone)]
pub struct Http3ServerOpt {
pub listen: SocketAddr,
}
pub async fn launch_http3_server(
tls_cert_key: TlsCertsKey,
http3_server_opt: Http3ServerOpt,
factories: Factories,
) -> Result<(), ChiteyError> {
let TlsCertsKey { certs, key } = tls_cert_key;
let Http3ServerOpt { listen } = http3_server_opt;
let tls_config = {
let mut tls_config = rustls::ServerConfig::builder()
.with_safe_default_cipher_suites()
.with_safe_default_kx_groups()
.with_protocol_versions(&[&rustls::version::TLS13])
.unwrap()
.with_no_client_auth()
.with_single_cert(certs.clone(), key.clone()).unwrap();
tls_config.max_early_data_size = u32::MAX;
let alpn: &[u8] = b"h3";
tls_config.alpn_protocols = vec![alpn.into()];
tls_config
};
let server_config = quinn::ServerConfig::with_crypto(Arc::new(tls_config));
let endpoint = quinn::Endpoint::server(server_config, listen).unwrap();
while let Some(new_conn) = endpoint.accept().await {
// #[cfg(debug_assertions)]
// trace_span!("New connection being attempted");
let factories2 = factories.clone();
tokio::spawn(async move {
println!("{:?}", factories2.factories.len());
match new_conn.await {
Ok(conn) => {
// #[cfg(debug_assertions)]
// info!("new connection established");
let mut h3_conn = h3::server::Connection::new(h3_quinn::Connection::new(conn))
.await
.unwrap();
loop {
match h3_conn.accept().await {
Ok(Some((req, stream))) => {
// #[cfg(debug_assertions)]
// eprintln!("new request: {:#?}", req);
let factories3 = factories2.clone();
tokio::spawn(async move {
// if let Err(e) = handle_request_http3(req, stream).await {
// #[cfg(debug_assertions)]
// error!("handling request failed: {}", e);
// };
let _ = handle_request_http3(req, stream, factories3).await;
});
}
// indicating no more streams to be received
Ok(None) => {
break;
}
Err(err) => {
#[cfg(debug_assertions)]
eprintln!("error on accept {}", err);
match err.get_error_level() {
ErrorLevel::ConnectionError => break,
ErrorLevel::StreamError => continue,
}
}
}
}
}
Err(_err) => {
// #[cfg(debug_assertions)]
// eprintln!("accepting connection failed: {:?}", err);
}
}
});
}
// shut down gracefully
// wait for connections to be closed before exiting
println!("Starting to serve on https://{}.", listen);
endpoint.wait_idle().await;
Ok(())
}
pub async fn handle_request_http3<T>(
req: Request<()>,
mut stream: RequestStream<T, Bytes>,
factories: Factories,
) -> Result<(), ChiteyError>
where
T: BidiStream<Bytes> + 'static + Send + Sync,
{
if req.uri().path().contains("..") {
let resp = http::Response::builder().status(StatusCode::NOT_FOUND).body(()).unwrap();
throw_chitey_internal_server_error(stream.send_response(resp).await)?;
throw_chitey_internal_server_error(stream.send_data(Bytes::copy_from_slice(b"page not found")).await)?;
throw_chitey_internal_server_error(stream.finish().await)?
}
let url = req.uri().to_string().parse().unwrap();
let input = UrlPatternMatchInput::Url(url);
let method = req.method().clone();
let req_contain_key = req.headers().contains_key("Another-Header");
let (mut send_stream, recv_stream) = stream.split();
let stm: StreamWrapper<T> = StreamWrapper::new(recv_stream);
let req = req.map(|_| Body::wrap_stream(stm));
for (res, factory) in factories.factories {
// GET
if res.guard == Guard::Get && method == Method::GET {
if let Ok(Some(_)) = res.rdef.exec(input.clone()) {
let factory_loc = factory.lock().await;
if factory_loc.analyze_types(input.clone()) {
return match factory_loc.handler_func(input.clone(), (req, false)).await {
Ok((mut resp, body)) => {
if req_contain_key {
resp = resp.header("Another-Header", "Ack");
}
throw_chitey_internal_server_error(send_stream.send_response(resp.body(()).unwrap()).await)?;
throw_chitey_internal_server_error(send_stream.send_data(body).await)?;
throw_chitey_internal_server_error(send_stream.finish().await)
},
Err(e) => Err(ChiteyError::InternalServerError(e.to_string())),
}
}
};
}
// POST
if res.guard == Guard::Post && method == Method::POST {
let factory_loc = factory.lock().await;
if factory_loc.analyze_types(input.clone()) {
return match factory_loc.handler_func(input.clone(), (req, false)).await {
Ok((mut resp, body)) => {
if req_contain_key {
resp = resp.header("Another-Header", "Ack");
}
throw_chitey_internal_server_error(send_stream.send_response(resp.body(()).unwrap()).await)?;
throw_chitey_internal_server_error(send_stream.send_data(body).await)?;
throw_chitey_internal_server_error(send_stream.finish().await)
},
Err(e) => Err(ChiteyError::InternalServerError(e.to_string())),
}
}
}
}
// let content_type = content_type_option.unwrap();
// let mime_type_result: Result<mime::Mime, _> = match content_type.to_str() {
// Ok(s) => s
// .parse()
// .map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)),
// Err(err) => Err(std::io::Error::new(std::io::ErrorKind::Other, err)),
// };
// if mime_type_result.is_err() {
// return Ok(());
// }
// let mime_type = mime_type_result.unwrap();
// if mime_type.essence_str() != "multipart/form-data" {
// return Ok(());
// }
// let boundary = mime_type
// .get_param("boundary")
// .map(|v| v.to_string())
// .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::Other, "boundary not found"))?;
// // if let Ok(Some(post_data)) = stream.recv_data().await{
// // info!("{:?}", post_data.chunk());
// // }
// let stm: StreamWrapper<T> = StreamWrapper::new(recv_stream);
// let mut req = req.map(|_| Body::wrap_stream(stm));
// let (re, b) = req.into_parts();
// let mut multipart_stream = mpart_async::server::MultipartStream::new(
// boundary,
// b.map_ok(|buf| {
// let mut ret = BytesMut::with_capacity(buf.remaining());
// ret.put(buf);
// ret.freeze()
// }),
// );
// while let Ok(Some(mut field)) = multipart_stream.try_next().await {
// println!("Field name:{}", field.name().unwrap());
// if let Ok(filename) = field.filename() {
// println!("Field filename:{}", filename);
// let mut writer = BufWriter::new(File::create(filename.as_ref()).unwrap());
// let mut bufferlen: i64 = 0;
// while let Ok(Some(bytes)) = field.try_next().await {
// bufferlen += bytes.len() as i64;
// writer.write(&bytes).unwrap();
// }
// println!("Bytes received:{}", bufferlen);
// } else {
// let mut buffer = BytesMut::new();
// while let Ok(Some(bytes)) = field.try_next().await {
// buffer.put(bytes);
// }
// let value: String = String::from_utf8(buffer.to_vec()).unwrap();
// println!("{} = {}", field.name().unwrap(), value);
// }
// }
// let resp = http::Response::builder().status(status).body(()).unwrap();
// // let post_stream = PostStream
// match send_stream.send_response(resp).await {
// Ok(_) => {
// #[cfg(debug_assertions)]
// info!("successfully respond to connection");
// }
// Err(err) => {
// #[cfg(debug_assertions)]
// error!("unable to send response to connection peer: {:?}", err);
// }
// }
// if let Ok(Some(post_data)) = recv_stream.recv_data().await{
// info!("{:?}", post_data.chunk());
// }
// info!("{:?}", req.headers().get("content-type"));
// match recv_stream.recv_trailers().await {
// Ok(v) => match v {
// Some(d) => { info!("recv: {:?}", d) },
// None => { info!("recv: None"); },
// },
// Err(v) => { info!("recv: {:?}", v); },
// };
// let mut num = 0;
// loop {
// match recv_stream.recv_data().await {
// Ok(v) => match v {
// Some(d) => {
// // info!("{:?}", d.chunk());
// // info!("{}", std::str::from_utf8(d.chunk()).unwrap());
// num += d.remaining();
// // info!("{:?}", d.remaining());
// },
// None => { info!("None"); break; },
// },
// Err(v) => { info!("{:?}", v); break; },
// }
// // tokio::time::sleep(std::time::Duration::from_millis(1000)).await;
// }
// info!("num: {}", num);
// match recv_stream.recv_trailers().await {
// Ok(v) => match v {
// Some(d) => { info!("recv: {:?}", d) },
// None => { info!("recv: None"); },
// },
// Err(v) => { info!("recv: {:?}", v); },
// };
// let mut buf = BytesMut::new();
// buf.extend_from_slice(b"hello world http3");
// send_stream.send_data(buf.freeze()).await?;
// Ok(send_stream.finish().await?)
// Ok(())
// } else {
let resp = http::Response::builder().status(StatusCode::NOT_FOUND).body(()).unwrap();
throw_chitey_internal_server_error(send_stream.send_response(resp).await)?;
throw_chitey_internal_server_error(send_stream.send_data(Bytes::copy_from_slice(b"page not found")).await)?;
throw_chitey_internal_server_error(send_stream.finish().await)
}