async-scgi

Crates.ioasync-scgi
lib.rsasync-scgi
version1.0.0
sourcesrc
created_at2021-10-26 15:14:06.453635
updated_at2024-08-31 21:46:20.084892
descriptionAsync SCGI Client & Server
homepagehttps://sr.ht/~zethra/async-scgi/
repositoryhttps://sr.ht/~zethra/async-scgi/
max_upload_size
id471968
size50,146
Sashanoraa (gaykitty)

documentation

https://docs.rs/async-scgi

README

async-scgi

License Cargo Documentation

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.

Client Example

use std::str::from_utf8;

use async_scgi::{ScgiHeaders, ScgiRequest};
use futures_lite::{AsyncReadExt, AsyncWriteExt};
use smol::net::TcpStream;

fn main() -> anyhow::Result<()> {
    smol::block_on(async {
        let mut stream = TcpStream::connect("127.0.0.1:12345").await?;
        let mut headers = ScgiHeaders::new();
        headers.insert("PATH_INFO".to_owned(), "/".to_owned());
        headers.insert("SERVER_NAME".to_owned(), "example.com".to_owned());
        let body = b"Hello world!";
        let req = ScgiRequest {
            headers,
            body: body.to_vec(),
        };
        stream.write_all(&req.encode()).await?;
        let mut resp = vec![];
        stream.read_to_end(&mut resp).await?;
        let resp_str = from_utf8(&resp)?;
        println!("{}", resp_str);
        Ok(())
    })
}

Server Example

use futures_lite::{AsyncWriteExt, StreamExt};
use smol::io::BufReader;
use smol::net::TcpListener;
use std::str::from_utf8;

fn main() -> anyhow::Result<()> {
    smol::block_on(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 = async_scgi::read_request(&mut stream).await?;
            println!("Headers: {:?}", req.headers);
            println!("Body: {}", from_utf8(&req.body).unwrap());
            stream.write_all(b"Hello Client!").await?;
        }
        Ok(())
    })
}
Commit count: 0

cargo fmt