Expand description
Easily create CGI (RFC 3875) programmes in Rust based on hyper’s http
types.
§Installation & Usage
Cargo.toml
:
[dependencies]
cgi = "*"
Use the cgi_main!
macro, with a function that takes a cgi::Request
and returns a
cgi::Response
.
extern crate 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 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 cgi::handle
ro cgi::try_handle
function directly
inside your main
function:
ⓘ
extern crate 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
)
Re-exports§
pub extern crate http;
Macros§
- cgi_
main - Create a
main
function for a CGI script - cgi_
try_ main - Create a CGI main function based on a function which returns a
Result<cgi::Response, _>
Functions§
- binary_
response - Sends
blob
with that status code, and optional content type,None
for noContent-Type
header 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
text
to bytes (UTF8) and sends that as the body with thatstatus_code
and HTMLContent-Type
header (text/html
) - string_
response - Convert to a string and return that with the status code
- text_
response - Serves this content as
text/plain
text response, with that status code - try_
handle - Call a function as a CGI programme.