Expand description
Easily create CGI (RFC 3875) programmes in Rust based on hyper’s http types.
§Installation & Usage
Cargo.toml:
[dependencies]
cgi2 = "0.7"Use the [cgi::main] macro on your main function, taking in a Request and returning a Response.
#[cgi::main]
fn main(request: cgi::Request) -> cgi::Response {
cgi::text_response(200, "Hello World")
}This also works if you return a Result
If your function returns a Result the error is printed to stderr
and an HTTP 500 error is returned.
#[cgi::main]
fn 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
an Request, call your function to create a response, 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.
It is also possible to call the cgi::handle function directly inside your main function:
fn main() {
cgi::handle(|request: cgi::Request| -> cgi::Response {
cgi::empty_response(404)
});
}Several shortcut functions are provided (such as html_response/binary_response).
Re-exports§
pub extern crate http;
Functions§
- binary_
response - Sends
blobwith that status code, and optional content type,Nonefor noContent-Typeheader to be set. - empty_
response - 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. - err_
to_ 500 - handle
- Call a function as a CGI programme.
- html_
response - Converts
textto bytes (UTF8) and sends that as the body with thatstatus_codeand HTMLContent-Typeheader (text/html) - string_
response - Convert to a string and return that with the status code
- text_
response - Serves this content as
text/plaintext response, with that status code
Type Aliases§
Attribute Macros§
- main
- Enables a CGI main function.