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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::future::Future;
use std::sync::mpsc::Receiver;
use std::task::Poll;
use std::task::Context;
use std::pin::Pin;
use std::collections::HashMap;
use std::cell::Cell;
use std::io::Error;
use pasts;
use async_std;
use async_std::prelude::*;
use async_std::net::TcpStream;
enum AsyncMsg {
Quit,
NewTask(Receiver<Message>, WebserverTask),
OldTask,
}
type WebserverTask = Box<dyn Future<Output = AsyncMsg> + Send>;
async fn slice_select<T>(
tasks: &mut Vec<Box<dyn Future<Output = T> + Send>>,
) -> T
{
struct SliceSelect<'a, T> {
tasks: &'a mut Vec<Box<dyn Future<Output = T> + Send>>,
}
impl<'a, T> Future for SliceSelect<'a, T> {
type Output = T;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
for future_id in 0..self.tasks.len() {
let mut future = unsafe {
Pin::new_unchecked(self.tasks[future_id].as_mut())
};
match future.as_mut().poll(cx) {
Poll::Ready(ret) => {
let _ = self.tasks.remove(future_id);
return Poll::Ready(ret);
},
Poll::Pending => {}
}
}
Poll::Pending
}
}
SliceSelect { tasks }.await
}
fn async_thread_main_future(recv: Receiver<Message>) -> AsyncMsg {
match recv.recv().unwrap() {
Message::NewJob(task) => AsyncMsg::NewTask(recv, task),
Message::Terminate => AsyncMsg::Quit,
}
}
async fn async_thread_main(recv: Receiver<Message>, num_tasks: Arc<AtomicUsize>) {
let mut tasks: Vec<WebserverTask> = vec![];
tasks.push(Box::new(pasts::spawn_blocking(move ||
async_thread_main_future(recv)
)));
loop {
match slice_select(&mut tasks).await {
AsyncMsg::NewTask(recv, task) => {
tasks.push(Box::new(pasts::spawn_blocking(move ||
async_thread_main_future(recv)
)));
tasks.push(task)
}
AsyncMsg::OldTask => {
num_tasks.fetch_sub(1, Ordering::Relaxed);
}
AsyncMsg::Quit => {
break
}
}
}
}
fn thread_main(recv: Receiver<Message>, num_tasks: Arc<AtomicUsize>) {
<pasts::ThreadInterrupt as pasts::Interrupt>::block_on(
async_thread_main(recv, num_tasks)
);
}
struct Thread {
num_tasks: Arc<AtomicUsize>,
join: Option<std::thread::JoinHandle<()>>,
sender: std::sync::mpsc::Sender<Message>,
}
impl Thread {
pub fn new() -> Self {
let (sender, receiver) = std::sync::mpsc::channel();
let num_tasks = Arc::new(AtomicUsize::new(0));
let thread_num_tasks = Arc::clone(&num_tasks);
let join = Some(std::thread::spawn(move ||
thread_main(receiver, thread_num_tasks)
));
Thread {
num_tasks, join, sender,
}
}
pub fn tasks(&self) -> usize {
self.num_tasks.load(Ordering::Relaxed)
}
pub fn send<T>(&self, future: T)
where T: Future<Output = AsyncMsg> + Send + 'static
{
self.num_tasks.fetch_add(1, Ordering::Relaxed);
self.sender.send(Message::NewJob(Box::new(future))).unwrap();
}
}
impl Drop for Thread {
fn drop(&mut self) {
self.sender.send(Message::Terminate).unwrap();
if let Some(thread) = self.join.take() {
thread.join().unwrap();
}
}
}
async fn async_main(web: Arc<Web>) {
let listener = async_std::net::TcpListener::bind("127.0.0.1:7878")
.await
.unwrap();
let mut threads = vec![];
let mut incoming = listener.incoming();
for _ in 0..4 {
threads.push(Thread::new());
}
while let Some(stream) = incoming.next().await {
let mut thread_id = 0;
let mut thread_tasks = threads[0].tasks();
for id in 1..threads.len() {
let n_tasks = threads[id].tasks();
if n_tasks < thread_tasks {
thread_id = id;
thread_tasks = n_tasks;
}
}
let stream = stream.unwrap();
let stream = Arc::new(stream);
let future = handle_connection(stream, Arc::clone(&web));
threads[thread_id].send(future);
}
}
type ResourceGenerator = Box<dyn Fn(Stream) -> Box<dyn Future<Output = Result<(), Error>> + Send> + Send + Sync>;
pub struct WebServer {
web: Web,
}
impl WebServer {
pub fn with_resources(path: &'static str) -> WebServer {
let urls = HashMap::new();
WebServer { web: Web { path, urls } }
}
pub fn url<F: 'static, G: 'static>(mut self, url: &'static str, func: G)
-> Self
where F: Future<Output = Result<(), std::io::Error>> + Send, G: Fn(Stream) -> F + Sync + Send
{
self.web.urls.insert(url, ("text/html; charset=utf-8", Box::new(
move |stream| Box::new(func(stream))
)));
self
}
pub fn url_with_type<F: 'static, G: 'static>(
mut self,
url: &'static str,
func: G,
content_type: &'static str)
-> Self
where F: Future<Output = Result<(), std::io::Error>> + Send, G: Fn(Stream) -> F + Sync + Send
{
self.web.urls.insert(url, (content_type, Box::new(
move |stream| Box::new(func(stream))
)));
self
}
pub fn start(self) {
let web = Arc::new(self.web);
<pasts::ThreadInterrupt as pasts::Interrupt>::block_on(async_main(web));
}
}
struct Web {
path: &'static str,
urls: HashMap<&'static str, (&'static str, ResourceGenerator)>,
}
unsafe impl Sync for Stream {}
pub struct Stream {
internal: Cell<Option<InternalStream>>
}
impl Stream {
pub async fn send(&self) -> Result<(), std::io::Error> {
let mut this = self.internal.take().unwrap();
let ret = this.send().await;
self.internal.set(Some(this));
ret
}
pub fn push_str(&self, text: &str) {
let mut this = self.internal.take().unwrap();
this.push_str(text);
self.internal.set(Some(this));
}
pub fn push_data(&self, bytes: &[u8]) {
let mut this = self.internal.take().unwrap();
this.push_data(bytes);
self.internal.set(Some(this));
}
}
struct InternalStream {
stream: Arc<TcpStream>,
output: Vec<u8>,
}
impl InternalStream {
pub async fn send(&mut self) -> Result<(), std::io::Error> {
let stream = Arc::get_mut(&mut self.stream).unwrap();
stream.write(&self.output).await?;
stream.flush().await?;
Ok(())
}
pub fn push_str(&mut self, text: &str) {
self.output.extend(text.bytes());
}
pub fn push_data(&mut self, bytes: &[u8]) {
self.output.extend(bytes);
}
}
enum Message {
NewJob(WebserverTask),
Terminate,
}
async fn handle_connection(mut streama: Arc<TcpStream>, web: Arc<Web>) -> AsyncMsg {
let stream = Arc::get_mut(&mut streama).unwrap();
let mut buffer = [0; 512];
stream.read(&mut buffer).await.unwrap();
if !buffer.starts_with(b"GET ") {
return AsyncMsg::OldTask;
}
let mut end = 4;
let path = loop {
if end == buffer.len() {
return AsyncMsg::OldTask;
}
if buffer[end] == b' ' {
break &buffer[4..end];
}
end += 1;
};
if !buffer[end+1..].starts_with(b"HTTP/1.1\r\n") {
return AsyncMsg::OldTask;
}
let mut streamb = InternalStream { stream: streama, output: vec![] };
let path = if let Ok(path) = std::str::from_utf8(path) {
path
} else {
return AsyncMsg::OldTask;
};
let mut index = web.path.to_string();
index.push_str("/index.html");
let mut e404 = web.path.to_string();
e404.push_str("/404.html");
if "/" == path {
if let Ok(contents) = std::fs::read_to_string(index) {
streamb.push_str("HTTP/1.1 200 OK\nContent-Type: ");
streamb.push_str("text/html; charset=utf-8");
streamb.push_str("\r\n\r\n");
streamb.push_str(&contents);
streamb.send().await.unwrap();
} else {
streamb.push_str("HTTP/1.1 404 NOT FOUND\nContent-Type: ");
streamb.push_str("text/html; charset=utf-8");
streamb.push_str("\r\n\r\n");
if let Ok(cs) = std::fs::read_to_string(e404) {
streamb.push_str(&cs);
} else {
streamb.push_str("404 NOT FOUND");
};
streamb.send().await.unwrap();
}
} else {
let mut page = web.path.to_string();
page.push_str(path);
if let Some(request) = web.urls.get(path) {
{
streamb.push_str("HTTP/1.1 200 OK\nContent-Type: ");
streamb.push_str(request.0);
streamb.push_str("\r\n\r\n");
}
Pin::from(request.1(Stream { internal: Cell::new(Some(streamb)) }))
.await
.unwrap();
} else if let Ok(contents) = std::fs::read_to_string(page) {
streamb.push_str("HTTP/1.1 200 OK\nContent-Type: ");
streamb.push_str("text/html; charset=utf-8");
streamb.push_str("\r\n\r\n");
streamb.push_str(&contents);
streamb.send().await.unwrap();
} else {
streamb.push_str("HTTP/1.1 404 NOT FOUND\nContent-Type: ");
streamb.push_str("text/html; charset=utf-8");
streamb.push_str("\r\n\r\n");
if let Ok(cs) = std::fs::read_to_string(e404) {
streamb.push_str(&cs);
} else {
streamb.push_str("404 NOT FOUND");
};
streamb.send().await.unwrap();
}
};
AsyncMsg::OldTask
}