Fastcgi client implemented for Rust.
Support both async(futures, async-std)
and sync(std)
clients.
Be default, both async
and sync
client are included, if you don't want to include async
client,
You can specify default-features = false
in Cargo.toml
.
With cargo add installed run:
bash
$ cargo add fastcgi-client
Async async-std
client:
``` use fastcgiclient::{AsyncClient, Params}; use std::env; use asyncstd::{io, task}; use async_std::net::TcpStream;
task::blockon(async { let scriptfilename = env::currentdir() .unwrap() .join("tests") .join("php") .join("index.php"); let scriptfilename = scriptfilename.tostr().unwrap(); let script_name = "/index.php";
// Connect to php-fpm default listening address.
let stream = TcpStream::connect(("127.0.0.1", 9000)).await.unwrap();
let mut client = AsyncClient::new(stream, false);
// Fastcgi params, please reference to nginx-php-fpm config.
let params = Params::with_predefine()
.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.do_request(¶ms, &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);
}); ```
Sync std
client:
``` use fastcgi_client::{Client, Params}; use std::{env, io}; use std::net::TcpStream;
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)).unwrap(); let mut client = Client::new(stream, false);
// Fastcgi params, please reference to nginx-php-fpm config. let params = Params::withpredefine() .setrequestmethod("GET") .setscriptname(scriptname) .setscriptfilename(scriptfilename) .setrequesturi(scriptname) .setdocumenturi(scriptname) .setremoteaddr("127.0.0.1") .setremoteport("12345") .setserveraddr("127.0.0.1") .setserverport("80") .setservername("jmjoy-pc") .setcontenttype("") .setcontent_length("0");
// Fetch fastcgi server(php-fpm) response. let output = client.do_request(¶ms, &mut io::empty()).unwrap();
// "Content-type: text/html; charset=UTF-8\r\n\r\nhello" let stdout = String::fromutf8(output.getstdout().unwrap()).unwrap();
assert!(stdout.contains("Content-type: text/html; charset=UTF-8")); assert!(stdout.contains("hello")); asserteq!(output.getstderr(), None); ```
MIT.