From acccd91138f6b430a90f2378de7f0caed3564a9f Mon Sep 17 00:00:00 2001 From: __JM_Joy__ <918734043@qq.com> Date: Sun, 17 Nov 2019 17:58:37 +0800 Subject: [PATCH] Update document. --- README.md | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 142 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 295fc8a..b8256e5 100644 --- a/README.md +++ b/README.md @@ -8,9 +8,74 @@ Fastcgi client implemented for Rust. -## Example +## Features -```rust +Support both `async(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](https://github.com/killercup/cargo-edit) installed run: + +```bash +$ 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(¶ms, &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; @@ -41,7 +106,7 @@ let params = Params::with_predefine() .set_server_name("jmjoy-pc") .set_content_type("") .set_content_length("0"); - + // Fetch fastcgi server(php-fpm) response. let output = client.do_request(¶ms, &mut io::empty()).unwrap(); @@ -52,3 +117,7 @@ assert!(stdout.contains("Content-type: text/html; charset=UTF-8")); assert!(stdout.contains("hello")); assert_eq!(output.get_stderr(), None); ``` + +## License +[MIT](https://github.com/jmjoy/fastcgi-client-rs/blob/master/LICENSE). + diff --git a/src/lib.rs b/src/lib.rs index 8556d21..b9c5f6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,73 @@ //! //! ![fastcgi-client-rs](https://raw.githubusercontent.com/jmjoy/fastcgi-client-rs/master/fastcgi-client-rs.png) //! -//! ### Example +//! ## Features +//! +//! Support both `async(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](https://github.com/killercup/cargo-edit) installed run: +//! +//! ```bash +//! $ 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(¶ms, &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}; @@ -46,6 +112,9 @@ //! assert_eq!(output.get_stderr(), None); //! ``` //! +//! ## License +//! [MIT](https://github.com/jmjoy/fastcgi-client-rs/blob/master/LICENSE). +//! mod client; mod error;