Fastcgi client implemented for Rust, power by tokio.
Add dependencies to your Cargo.toml
by cargo add
:
shell
cargo add tokio --features full
cargo add fastcgi-client
Short connection mode:
```rust, norun use fastcgiclient::{Client, Params, Request}; use std::env; use tokio::{io, net::TcpStream};
async fn main() { let scriptfilename = env::currentdir() .unwrap() .join("tests") .join("php") .join("index.php"); let scriptfilename = scriptfilename.tostr().unwrap(); let scriptname = "/index.php";
// Connect to php-fpm default listening address.
let stream = TcpStream::connect(("127.0.0.1", 9000)).await.unwrap();
let mut client = Client::new(stream);
// Fastcgi params, please reference to nginx-php-fpm config.
let params = Params::default()
.set_request_method("GET")
.set_script_name(script_name)
.set_script_filename(script_filename)
.set_request_uri(script_name)
.set_document_uri(script_name)
.set_remote_addr("127.0.0.1")
.set_remote_port("12345")
.set_server_addr("127.0.0.1")
.set_server_port("80")
.set_server_name("jmjoy-pc")
.set_content_type("")
.set_content_length("0");
// Fetch fastcgi server(php-fpm) response.
let output = client.execute_once(Request::new(params, &mut io::empty())).await.unwrap();
// "Content-type: text/html; charset=UTF-8\r\n\r\nhello"
let stdout = String::from_utf8(output.get_stdout().unwrap()).unwrap();
assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
assert!(stdout.contains("hello"));
assert_eq!(output.get_stderr(), None);
} ```
Keep alive mode:
```rust, norun use fastcgiclient::{Client, Params, Request}; use std::env; use tokio::{io, net::TcpStream};
async fn main() { // Connect to php-fpm default listening address. let stream = TcpStream::connect(("127.0.0.1", 9000)).await.unwrap(); let mut client = Client::newkeepalive(stream);
// Fastcgi params, please reference to nginx-php-fpm config.
let params = Params::default();
for _ in (0..3) {
// Fetch fastcgi server(php-fpm) response.
let output = client.execute(Request::new(params.clone(), &mut io::empty())).await.unwrap();
// "Content-type: text/html; charset=UTF-8\r\n\r\nhello"
let stdout = String::from_utf8(output.get_stdout().unwrap()).unwrap();
assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
assert!(stdout.contains("hello"));
assert_eq!(output.get_stderr(), None);
}
} ```
MIT.