The FastCGI Rust implementation.
Description
gfcgi is a native Rust library for FastCGI.
This library supports multithreaded socket listeners and HTTP-instances multiplexed onto a single connection.
About FastCGI
FastCGI is a great solution to handling HTTP-requests without overhead. Completely supporting HTTP or HTTPS by any of the leading popular web-servers (Apache HTTPd, nginx, etc).
Example
Import the library within your code.
extern crate gfcgi;
use std::io::{Read, Write};
use std::thread;
An example of a router struct
#[derive(Clone)]
struct Router;
impl Router
{
fn new() -> Self
{
Router{}
}
}
Implement gfcgi::Handler
trait for your router, all code in process
method is optional
impl gfcgi::Handler for Router
{
fn process(&self, request: &mut gfcgi::Request, response: &mut gfcgi::Response)
{
// get a header
println!("{:?}", request.header_utf8(b"HTTP_HOST"));
// read content
let mut buf = Vec::new();
request.read_to_end(&mut buf).unwrap();
println!("{:?}", String::from_utf8(buf));
// set header
response.status(200);
response.header_utf8("Content-type", "text/plain");
// send content
response.write(b"hello world!").expect("send body");
}
}
Now run listener
, you can spawn threads if the spawn
feature is set in Cargo.toml
fn main()
{
let client = gfcgi::Client::new("127.0.0.1:4128");
// run listener
client.run(Router::new());
if cfg!(feature = "spawn") {
client.run(Router::new()); // spawn worker
}
thread::park(); // keep main process
}
Planned
- Role
- responder
- filter
- authorizer
- Header
- get_values
- get_values_result
- unknown_type
- begin_request
- abort_request
- end_request
- params
- stdin
- data
- stdout
- stderr
Trace
socket
stream
connection
handler
request
| → read headers
| → [read body]
response
| ← write headers
| ← [write body]