14 releases (8 breaking)

0.9.0 Jun 30, 2023
0.8.0 Aug 29, 2022
0.7.0 Jun 21, 2021
0.6.1 Mar 1, 2021
0.3.0 Jul 12, 2019

#154 in HTTP client

Download history 314/week @ 2024-06-10 194/week @ 2024-06-17 134/week @ 2024-06-24 60/week @ 2024-07-01 23/week @ 2024-07-08 51/week @ 2024-07-15 52/week @ 2024-07-22 186/week @ 2024-07-29 267/week @ 2024-08-05 124/week @ 2024-08-12 90/week @ 2024-08-19 94/week @ 2024-08-26 53/week @ 2024-09-02 25/week @ 2024-09-09 46/week @ 2024-09-16 162/week @ 2024-09-23

301 downloads per month
Used in 5 crates (2 directly)

Apache-2.0

44KB
904 lines

fastcgi-client-rs

Rust Crate API

Fastcgi client implemented for Rust, power by tokio.

Installation

Add dependencies to your Cargo.toml by cargo add:

cargo add tokio --features full
cargo add fastcgi-client

Examples

Short connection mode:

use fastcgi_client::{Client, Params, Request};
use std::env;
use tokio::{io, net::TcpStream};

#[tokio::main]
async fn main() {
    let script_filename = env::current_dir()
        .unwrap()
        .join("tests")
        .join("php")
        .join("index.php");
    let script_filename = script_filename.to_str().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 = Client::new(stream);

    // Fastcgi params, please reference to nginx-php-fpm config.
    let params = Params::default()
        .request_method("GET")
        .script_name(script_name)
        .script_filename(script_filename)
        .request_uri(script_name)
        .document_uri(script_name)
        .remote_addr("127.0.0.1")
        .remote_port(12345)
        .server_addr("127.0.0.1")
        .server_port(80)
        .server_name("jmjoy-pc")
        .content_type("")
        .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.stdout.unwrap()).unwrap();

    assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
    assert!(stdout.contains("hello"));
    assert_eq!(output.stderr, None);
}

Keep alive mode:

use fastcgi_client::{Client, Params, Request};
use std::env;
use tokio::{io, net::TcpStream};

#[tokio::main]
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::new_keep_alive(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.stdout.unwrap()).unwrap();

        assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
        assert!(stdout.contains("hello"));
        assert_eq!(output.stderr, None);
    }
}

License

Apache-2.0.

Dependencies

~2.7–8.5MB
~71K SLoC