Skip to content

Commit acccd91

Browse files
committed
Update document.
1 parent 0570127 commit acccd91

File tree

2 files changed

+142
-4
lines changed

2 files changed

+142
-4
lines changed

README.md

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,74 @@
88

99
Fastcgi client implemented for Rust.
1010

11-
## Example
11+
## Features
1212

13-
```rust
13+
Support both `async(async-std)` and `sync(std)` clients.
14+
15+
Be default, both `async` and `sync` client are included, if you don't want to include `async` client,
16+
You can specify `default-features = false` in `Cargo.toml`.
17+
18+
## Installation
19+
20+
With [cargo add](https://github.com/killercup/cargo-edit) installed run:
21+
22+
```bash
23+
$ cargo add fastcgi-client
24+
```
25+
26+
## Examples
27+
28+
Async `async-std` client:
29+
30+
```
31+
use fastcgi_client::{AsyncClient, Params};
32+
use std::env;
33+
use async_std::{io, task};
34+
use async_std::net::TcpStream;
35+
36+
task::block_on(async {
37+
let script_filename = env::current_dir()
38+
.unwrap()
39+
.join("tests")
40+
.join("php")
41+
.join("index.php");
42+
let script_filename = script_filename.to_str().unwrap();
43+
let script_name = "/index.php";
44+
45+
// Connect to php-fpm default listening address.
46+
let stream = TcpStream::connect(("127.0.0.1", 9000)).await.unwrap();
47+
let mut client = AsyncClient::new(stream, false);
48+
49+
// Fastcgi params, please reference to nginx-php-fpm config.
50+
let params = Params::with_predefine()
51+
.set_request_method("GET")
52+
.set_script_name(script_name)
53+
.set_script_filename(script_filename)
54+
.set_request_uri(script_name)
55+
.set_document_uri(script_name)
56+
.set_remote_addr("127.0.0.1")
57+
.set_remote_port("12345")
58+
.set_server_addr("127.0.0.1")
59+
.set_server_port("80")
60+
.set_server_name("jmjoy-pc")
61+
.set_content_type("")
62+
.set_content_length("0");
63+
64+
// Fetch fastcgi server(php-fpm) response.
65+
let output = client.do_request(&params, &mut io::empty()).await.unwrap();
66+
67+
// "Content-type: text/html; charset=UTF-8\r\n\r\nhello"
68+
let stdout = String::from_utf8(output.get_stdout().unwrap()).unwrap();
69+
70+
assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
71+
assert!(stdout.contains("hello"));
72+
assert_eq!(output.get_stderr(), None);
73+
});
74+
```
75+
76+
Sync `std` client:
77+
78+
```
1479
use fastcgi_client::{Client, Params};
1580
use std::{env, io};
1681
use std::net::TcpStream;
@@ -41,7 +106,7 @@ let params = Params::with_predefine()
41106
.set_server_name("jmjoy-pc")
42107
.set_content_type("")
43108
.set_content_length("0");
44-
109+
45110
// Fetch fastcgi server(php-fpm) response.
46111
let output = client.do_request(&params, &mut io::empty()).unwrap();
47112
@@ -52,3 +117,7 @@ assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
52117
assert!(stdout.contains("hello"));
53118
assert_eq!(output.get_stderr(), None);
54119
```
120+
121+
## License
122+
[MIT](https://github.com/jmjoy/fastcgi-client-rs/blob/master/LICENSE).
123+

src/lib.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,73 @@
22
//!
33
//! ![fastcgi-client-rs](https://raw.githubusercontent.com/jmjoy/fastcgi-client-rs/master/fastcgi-client-rs.png)
44
//!
5-
//! ### Example
5+
//! ## Features
6+
//!
7+
//! Support both `async(async-std)` and `sync(std)` clients.
8+
//!
9+
//! Be default, both `async` and `sync` client are included, if you don't want to include `async` client,
10+
//! You can specify `default-features = false` in `Cargo.toml`.
11+
//!
12+
//! ## Installation
13+
//!
14+
//! With [cargo add](https://github.com/killercup/cargo-edit) installed run:
15+
//!
16+
//! ```bash
17+
//! $ cargo add fastcgi-client
18+
//! ```
19+
//!
20+
//! ## Examples
21+
//!
22+
//! Async `async-std` client:
23+
//!
24+
//! ```
25+
//! use fastcgi_client::{AsyncClient, Params};
26+
//! use std::env;
27+
//! use async_std::{io, task};
28+
//! use async_std::net::TcpStream;
29+
//!
30+
//! task::block_on(async {
31+
//! let script_filename = env::current_dir()
32+
//! .unwrap()
33+
//! .join("tests")
34+
//! .join("php")
35+
//! .join("index.php");
36+
//! let script_filename = script_filename.to_str().unwrap();
37+
//! let script_name = "/index.php";
38+
//!
39+
//! // Connect to php-fpm default listening address.
40+
//! let stream = TcpStream::connect(("127.0.0.1", 9000)).await.unwrap();
41+
//! let mut client = AsyncClient::new(stream, false);
42+
//!
43+
//! // Fastcgi params, please reference to nginx-php-fpm config.
44+
//! let params = Params::with_predefine()
45+
//! .set_request_method("GET")
46+
//! .set_script_name(script_name)
47+
//! .set_script_filename(script_filename)
48+
//! .set_request_uri(script_name)
49+
//! .set_document_uri(script_name)
50+
//! .set_remote_addr("127.0.0.1")
51+
//! .set_remote_port("12345")
52+
//! .set_server_addr("127.0.0.1")
53+
//! .set_server_port("80")
54+
//! .set_server_name("jmjoy-pc")
55+
//! .set_content_type("")
56+
//! .set_content_length("0");
57+
//!
58+
//! // Fetch fastcgi server(php-fpm) response.
59+
//! let output = client.do_request(&params, &mut io::empty()).await.unwrap();
60+
//!
61+
//! // "Content-type: text/html; charset=UTF-8\r\n\r\nhello"
62+
//! let stdout = String::from_utf8(output.get_stdout().unwrap()).unwrap();
63+
//!
64+
//! assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
65+
//! assert!(stdout.contains("hello"));
66+
//! assert_eq!(output.get_stderr(), None);
67+
//! });
68+
//! ```
69+
//!
70+
//! Sync `std` client:
71+
//!
672
//! ```
773
//! use fastcgi_client::{Client, Params};
874
//! use std::{env, io};
@@ -46,6 +112,9 @@
46112
//! assert_eq!(output.get_stderr(), None);
47113
//! ```
48114
//!
115+
//! ## License
116+
//! [MIT](https://github.com/jmjoy/fastcgi-client-rs/blob/master/LICENSE).
117+
//!
49118
50119
mod client;
51120
mod error;

0 commit comments

Comments
 (0)