Skip to content

Create CGI programmes in Rust with hyper's http types

License

Notifications You must be signed in to change notification settings

mredlek/rust-cgi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-cgi

Easily create CGI[1][2] programmes[3] in Rust based on http types.

crates.io released version badge
crates.io released licencegpl

Installation & Usage

Cargo.toml:

[dependencies]
cgi = "0.3"

In the main function, call only cgi::handle(…​), with a function that takes a cgi::Request and returns cgi::Response.

extern crate cgi;

fn main() { cgi::handle(|request: cgi::Request| -> cgi::Response {
    ...
})}
extern crate cgi;

fn main() { cgi::handle(|request: cgi::Request| -> cgi::Response {
    cgi::html_response(200, "<html><body><h1>Hello World!</h1></body></html>")
})}

It will parse & extract the CGI environmental variables, and HTTP request body to create Request, and convert your Response into the correct format and print to stdout. If this programme is not called as CGI (e.g. missing required environmental variables), it will panic.

Response Shortcuts

Several shortcuts create shortcuts easily:

cgi:empty_response(status_code)

A HTTP Reponse with no body and that HTTP status code, e.g. return cgi::empty_response(404); to return a HTTP 404 Not Found.

cgi::html_response(status_code, text)

Converts text to bytes (UTF8) and sends that as the body with that status_code and HTML Content-Type header.

cgi::string_response(status_code, text)

Converts text to bytes (UTF8), and sends that as the body with that status_code, e.g. `return

cgi::string_response(200, "Hello World!")`

returns a simple plain text response.

cgi::binary_response(status_code, blob)

Sends blob with that status code.

Re-exports

http is re-exported, (as cgi::http).

cgi::Response/Request are http::Response<Vec<u8>>/Request<Vec<u8>>.

Running locally

Python provides a simple CGI webserver. The binaries must be in a cgi-bin directory. Run this in your project root directory (i.e. where Cargo.toml is), and open http://localhost:8000/.

ln -s ./target/debug/ ./cgi-bin/
python3 -m http.server --cgi

See also

Things using this

  • 'Suggestions welcome!'

Why?

CGI is old, and easy to deploy. Just drop a binary in the right place, and Apache (or whatever) will serve it up. Rust is fast, so for simple things, there should be less downsides to spinning up a custom HTTP server.

Copyright

Copyright GNU Affero GPL v3 (or later). See the file LICENCE


1. Retro!
2. Common Gateway Interface 1.1, RFC 3875
3. Yes, I’m spelling it programme, the correct way.

About

Create CGI programmes in Rust with hyper's http types

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 100.0%