mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-21 20:51:32 +00:00
HTTP server example.
This commit is contained in:
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -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"
|
||||||
|
@ -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"
|
||||||
|
10
examples/http-server/Cargo.toml
Normal file
10
examples/http-server/Cargo.toml
Normal 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" }
|
68
examples/http-server/src/main.rs
Normal file
68
examples/http-server/src/main.rs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user