Simple networking sockets

The wasmedge_wasi_socket crate enables Rust developers to create networking applications and compile them into WebAssembly for WasmEdge Runtime. One of the key features of WasmEdge is that it supports non-blocking sockets. That allows even a single threaded WASM application to handle concurrent network requests. For example, while the program is waiting for data to stream in from one connection, it can start or handle another connection.

In this chapter, we will start with simple HTTP client and server examples. Then in the next chapter, we will cover the more complex non-blocking examples.

An HTTP client example

The source code for the HTTP client is available as follows.

  1. use wasmedge_http_req::request;
  2. fn main() {
  3. let mut writer = Vec::new(); //container for body of a response
  4. let res = request::get("http://127.0.0.1:1234/get", &mut writer).unwrap();
  5. println!("GET");
  6. println!("Status: {} {}", res.status_code(), res.reason());
  7. println!("Headers {}", res.headers());
  8. println!("{}", String::from_utf8_lossy(&writer));
  9. let mut writer = Vec::new(); //container for body of a response
  10. const BODY: &[u8; 27] = b"field1=value1&field2=value2";
  11. // let res = request::post("https://httpbin.org/post", BODY, &mut writer).unwrap();
  12. // no https , no dns
  13. let res = request::post("http://127.0.0.1:1234/post", BODY, &mut writer).unwrap();
  14. println!("POST");
  15. println!("Status: {} {}", res.status_code(), res.reason());
  16. println!("Headers {}", res.headers());
  17. println!("{}", String::from_utf8_lossy(&writer));
  18. }

The following command compiles the Rust program.

cargo build --target wasm32-wasi --release

The following command runs the application in WasmEdge.

wasmedge target/wasm32-wasi/release/http_client.wasm

An HTTP server example

The source code for the HTTP server application is available as follows.

  1. use bytecodec::DecodeExt;
  2. use httpcodec::{HttpVersion, ReasonPhrase, Request, RequestDecoder, Response, StatusCode};
  3. use std::io::{Read, Write};
  4. #[cfg(feature = "std")]
  5. use std::net::{Shutdown, TcpListener, TcpStream};
  6. #[cfg(not(feature = "std"))]
  7. use wasmedge_wasi_socket::{Shutdown, TcpListener, TcpStream};
  8. fn handle_http(req: Request<String>) -> bytecodec::Result<Response<String>> {
  9. Ok(Response::new(
  10. HttpVersion::V1_0,
  11. StatusCode::new(200)?,
  12. ReasonPhrase::new("")?,
  13. format!("echo: {}", req.body()),
  14. ))
  15. }
  16. fn handle_client(mut stream: TcpStream) -> std::io::Result<()> {
  17. let mut buff = [0u8; 1024];
  18. let mut data = Vec::new();
  19. loop {
  20. let n = stream.read(&mut buff)?;
  21. data.extend_from_slice(&buff[0..n]);
  22. if n < 1024 {
  23. break;
  24. }
  25. }
  26. let mut decoder =
  27. RequestDecoder::<httpcodec::BodyDecoder<bytecodec::bytes::Utf8Decoder>>::default();
  28. let req = match decoder.decode_from_bytes(data.as_slice()) {
  29. Ok(req) => handle_http(req),
  30. Err(e) => Err(e),
  31. };
  32. let r = match req {
  33. Ok(r) => r,
  34. Err(e) => {
  35. let err = format!("{:?}", e);
  36. Response::new(
  37. HttpVersion::V1_0,
  38. StatusCode::new(500).unwrap(),
  39. ReasonPhrase::new(err.as_str()).unwrap(),
  40. err.clone(),
  41. )
  42. }
  43. };
  44. let write_buf = r.to_string();
  45. stream.write(write_buf.as_bytes())?;
  46. stream.shutdown(Shutdown::Both)?;
  47. Ok(())
  48. }
  49. fn main() -> std::io::Result<()> {
  50. let port = std::env::var("PORT").unwrap_or(1234.to_string());
  51. println!("new connection at {}", port);
  52. let listener = TcpListener::bind(format!("0.0.0.0:{}", port))?;
  53. loop {
  54. let _ = handle_client(listener.accept()?.0);
  55. }
  56. }

The following command compiles the Rust program.

cargo build --target wasm32-wasi --release

The following command runs the application in WasmEdge.

$ wasmedge target/wasm32-wasi/release/http_server.wasm new connection at 1234

To test the HTTP server, you can submit a HTTP request to it via curl.

$ curl -d "name=WasmEdge" -X POST http://127.0.0.1:1234 echo: name=WasmEdge