This is a Rust library for handling SCGI requests and responses in an async context.
This library will work with any async runtime that uses the futures-io
library I/O traits.
This crate provides two main tools:
- The [ScgiRequest
] type to read & write SCGI requests.
- The [read_request
] function to read an SCGI request from a socket.
```rust use std::str::from_utf8;
use asyncscgi::{ScgiHeaders, ScgiRequest}; use futureslite::{AsyncReadExt, AsyncWriteExt}; use smol::net::TcpStream;
fn main() -> anyhow::Result<()> { smol::blockon(async { let mut stream = TcpStream::connect("127.0.0.1:12345").await?; let mut headers = ScgiHeaders::new(); headers.insert("PATHINFO".toowned(), "/".toowned()); headers.insert("SERVERNAME".toowned(), "example.com".toowned()); let body = b"Hello world!"; let req = ScgiRequest { headers, body: body.tovec(), }; stream.writeall(&req.encode()).await?; let mut resp = vec![]; stream.readtoend(&mut resp).await?; let respstr = fromutf8(&resp)?; println!("{}", respstr); Ok(()) }) } ```
```rust use futureslite::{AsyncWriteExt, StreamExt}; use smol::io::BufReader; use smol::net::TcpListener; use std::str::fromutf8;
fn main() -> anyhow::Result<()> { smol::blockon(async { let listener = TcpListener::bind("127.0.0.1:12345").await?; let mut incoming = listener.incoming(); while let Some(stream) = incoming.next().await { let mut stream = BufReader::new(stream?); let req = asyncscgi::readrequest(&mut stream).await?; println!("Headers: {:?}", req.headers); println!("Body: {}", fromutf8(&req.body).unwrap()); stream.write_all(b"Hello Client!").await?; } Ok(()) }) } ```