HTTP server example.

This commit is contained in:
Heyang Zhou
2019-05-13 18:37:22 -07:00
parent 884a7e1713
commit 4f77f4d024
4 changed files with 86 additions and 1 deletions

7
Cargo.lock generated
View File

@ -787,6 +787,13 @@ dependencies = [
"itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "itoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "http-server"
version = "0.1.0"
dependencies = [
"kwasm-net 0.1.0",
]
[[package]] [[package]]
name = "httparse" name = "httparse"
version = "1.3.3" version = "1.3.3"

View File

@ -36,7 +36,7 @@ wasmer-wasi = { path = "lib/wasi", optional = true }
kwasm-loader = { path = "lib/kwasm-loader" } kwasm-loader = { path = "lib/kwasm-loader" }
[workspace] [workspace]
members = ["lib/clif-backend", "lib/singlepass-backend", "lib/runtime", "lib/runtime-abi", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi", "lib/middleware-common", "lib/kwasm-loader", "lib/kwasm-net", "examples/pipe", "examples/wasi-networking", "examples/plugin-for-example"] members = ["lib/clif-backend", "lib/singlepass-backend", "lib/runtime", "lib/runtime-abi", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi", "lib/middleware-common", "lib/kwasm-loader", "lib/kwasm-net", "examples/pipe", "examples/wasi-networking", "examples/http-server", "examples/plugin-for-example"]
[build-dependencies] [build-dependencies]
wabt = "0.7.2" wabt = "0.7.2"

View File

@ -0,0 +1,10 @@
[package]
name = "http-server"
version = "0.1.0"
authors = ["Heyang Zhou <zhy20000919@hotmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
kwasm-net = { path = "../../lib/kwasm-net" }

View File

@ -0,0 +1,68 @@
#![feature(wasi_ext)]
use kwasm_net::{Epoll, Tcp4Listener, TcpStream, schedule};
use std::sync::Arc;
fn serve(stream: Arc<TcpStream>, mut all: Vec<u8>) {
let stream2 = stream.clone();
stream.read_async(
Vec::with_capacity(512),
move |result| {
match result {
Ok(buf) => {
if buf.len() == 0 {
return;
}
all.extend_from_slice(&buf);
if all.len() > 4096 {
println!("header too large");
return;
}
let s = match ::std::str::from_utf8(&all) {
Ok(x) => x,
Err(e) => {
println!("not utf8: {:?}", e);
return;
}
};
if let Some(pos) = s.find("\r\n\r\n") {
stream2.write_all_async(
format!(
"HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nYour headers: \n\n{}\n",
::std::str::from_utf8(&all[..pos]).unwrap()
).into_bytes(),
|result| {}
);
} else {
schedule(|| serve(stream2, all));
}
}
Err(code) => {
println!("failed to read; code = {}", code);
}
}
}
);
}
fn main() {
let epoll = Arc::new(Epoll::new());
let listener = Arc::new(Tcp4Listener::new("0.0.0.0", 2011, 128).unwrap());
listener.accept_async(epoll.clone(), |stream| {
match stream {
Ok(stream) => {
serve(stream, vec![]);
Ok(())
},
Err(code) => {
println!("failed to accept; code = {}", code);
Err(())
}
}
});
println!("start epoll");
unsafe {
epoll.run();
}
}