14个版本 (8个破坏性版本)

0.9.0 2023年6月30日
0.8.0 2022年8月29日
0.7.0 2021年6月21日
0.6.1 2021年3月1日
0.3.0 2019年7月12日

#305 in 异步

Download history 131/week @ 2024-03-13 105/week @ 2024-03-20 52/week @ 2024-03-27 146/week @ 2024-04-03 114/week @ 2024-04-10 81/week @ 2024-04-17 58/week @ 2024-04-24 55/week @ 2024-05-01 17/week @ 2024-05-08 42/week @ 2024-05-15 78/week @ 2024-05-22 28/week @ 2024-05-29 308/week @ 2024-06-05 150/week @ 2024-06-12 192/week @ 2024-06-19 97/week @ 2024-06-26

每月757次下载
用于 phper-test

Apache-2.0

44KB
904

fastcgi-client-rs

Rust Crate API

Rust实现的FastCGI客户端,由tokio提供动力。

安装

通过Cargo.toml添加依赖项:cargo add

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

示例

短连接模式

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

保持活跃模式

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

许可证

Apache-2.0.

依赖项

~2.7–4.5MB
~72K SLoC