fastcgi client rs

其他类别 2025-08-20

fastcgi-client-rs

FastCGI客户端为Rust,Tokio实施了Power。

安装

将依赖项cargo add Cargo.toml

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-8rnrnhello"
    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-8rnrnhello"
        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。

下载源码

通过命令行克隆项目:

git clone https://github.com/jmjoy/fastcgi-client-rs.git