Skip to content

Commit

Permalink
HTTP server example.
Browse files Browse the repository at this point in the history
  • Loading branch information
losfair committed May 14, 2019
1 parent 884a7e1 commit 4f77f4d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ wasmer-wasi = { path = "lib/wasi", optional = true }
kwasm-loader = { path = "lib/kwasm-loader" }

[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]
wabt = "0.7.2"
Expand Down
10 changes: 10 additions & 0 deletions examples/http-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "http-server"
version = "0.1.0"
authors = ["Heyang Zhou <[email protected]>"]
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" }
68 changes: 68 additions & 0 deletions examples/http-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 4f77f4d

Please sign in to comment.