88
99Fastcgi 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(¶ms, &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+ ```
1479use fastcgi_client::{Client, Params};
1580use std::{env, io};
1681use 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.
46111let output = client.do_request(¶ms, &mut io::empty()).unwrap();
47112
@@ -52,3 +117,7 @@ assert!(stdout.contains("Content-type: text/html; charset=UTF-8"));
52117assert!(stdout.contains("hello"));
53118assert_eq!(output.get_stderr(), None);
54119```
120+
121+ ## License
122+ [ MIT] ( https://github.com/jmjoy/fastcgi-client-rs/blob/master/LICENSE ) .
123+
0 commit comments