#fastcgi #php #futures #async-std #php-fpm

fastcgi-client

Fastcgi client implemented for Rust

6 releases (breaking)

0.5.0 Dec 8, 2019
0.4.0 Nov 17, 2019
0.3.0 Jul 12, 2019
0.2.0 Jul 7, 2019
0.0.0 Jun 8, 2019

#265 in Asynchronous

Download history 14/week @ 2020-11-02 8/week @ 2020-11-09 9/week @ 2020-11-16 29/week @ 2020-11-23 47/week @ 2020-11-30 47/week @ 2020-12-07 33/week @ 2020-12-14 2/week @ 2020-12-21 2/week @ 2020-12-28 19/week @ 2021-01-04 34/week @ 2021-01-11 8/week @ 2021-01-18 7/week @ 2021-01-25 4/week @ 2021-02-01 37/week @ 2021-02-08 46/week @ 2021-02-15

67 downloads per month

MIT license

42KB
918 lines

fastcgi-client-rs

Build Status Crate API

fastcgi-client-rs

Fastcgi client implemented for Rust.

Features

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.

Installation

With cargo add installed run:

$ cargo add fastcgi-client

Examples

Async async-std client:

use fastcgi_client::{AsyncClient, Params};
use std::env;
use async_std::{io, task};
use async_std::net::TcpStream;

task::block_on(async {
    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 = 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(&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);
});

Sync std client:

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

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)).unwrap();
let mut client = Client::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(&params, &mut io::empty()).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

~1.5MB
~27K SLoC