#programs #hyper-http #web

rust-cgi

Library to help create CGI programs in Rust

5 releases

0.7.2 May 15, 2025
0.7.1 Feb 3, 2024
0.7.0 Jan 20, 2024
0.6.2 Jan 20, 2024
0.6.1 Jan 20, 2024

#103 in #programs

Download history 1865/week @ 2025-08-13 852/week @ 2025-08-20 1841/week @ 2025-08-27 2100/week @ 2025-09-03 445/week @ 2025-09-10 150/week @ 2025-09-17 262/week @ 2025-09-24 29/week @ 2025-10-01 210/week @ 2025-10-22 304/week @ 2025-10-29 8/week @ 2025-11-12 46/week @ 2025-11-19

54 downloads per month

AGPL-3.0

21KB
393 lines

rust-cgi

This repository is a fork of https://github.com/amandasaurus/rust-cgi, which was unmaintained for a while. However, it has since resumed activity, and absorbed all the interesting patches from this fork. Please go use that crate instead.


lib.rs:

Easily create CGI (RFC 3875) programmes in Rust based on hyper's http types.

Installation & Usage

Cargo.toml:

[dependencies]
rust_cgi = "0.3"

Use the cgi_main! macro, with a function that takes a rust_cgi::Request and returns a rust_cgi::Response.

extern crate rust_cgi as cgi;

cgi::cgi_main! { |request: cgi::Request| -> cgi::Response {
     cgi::text_response(200, "Hello World")
} }

If your function returns a Result, you can use cgi_try_main!:

extern crate rust_cgi as cgi;

cgi::cgi_try_main! { |request: cgi::Request| -> Result<cgi::Response, String> {
    let greeting = std::fs::read_to_string("greeting.txt").map_err(|_| "Couldn't open file")?;

    Ok(cgi::text_response(200, greeting))
} }

It will parse & extract the CGI environmental variables, and the HTTP request body to create Request<u8>, call your function to create a response, and convert your Response into the correct format and print to stdout.

It is also possible to call the rust_cgi::handle ro rust_cgi::try_handle function directly inside your main function:

extern crate rust_cgi as cgi;

fn main() { cgi::handle(|request: cgi::Request| -> cgi::Response {
     cgi::empty_response(404)
})}

Several shortcut functions are provided (such as html_response/binary_response)

Dependencies

~605KB