#fastcgi #fcgi #client #tokio #php

kvarn-fastcgi-client

Fastcgi client implemented for Rust

1 unstable release

0.1.0 Nov 5, 2021

#4 in #fcgi

Download history 6/week @ 2022-11-29 21/week @ 2022-12-06 9/week @ 2022-12-13 17/week @ 2022-12-20 11/week @ 2022-12-27 4/week @ 2023-01-03 20/week @ 2023-01-10 8/week @ 2023-01-17 32/week @ 2023-01-24 21/week @ 2023-01-31 14/week @ 2023-02-07 16/week @ 2023-02-14 17/week @ 2023-02-21 9/week @ 2023-02-28 12/week @ 2023-03-07 22/week @ 2023-03-14

64 downloads per month
Used in kvarn-extensions

MIT license

31KB
743 lines

fastcgi-client-rs

Rust Crate API

Fastcgi client implemented for Rust, power by tokio.

Installation

Add this to your Cargo.toml:

[dependencies]
fastcgi-client = "0.6"
tokio = { version = "1", features = ["full"] }

Examples

use fastcgi_client::{Client, Params, Request};
use std::env;
use tokio::{io, task};
use tokio::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, false);

    // 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(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);
}

License

MIT.

Dependencies

~3–8MB
~127K SLoC