1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
use std::io::{Read, Write, stdin};
use std::collections::HashMap;
pub extern crate http;
pub type Request = http::Request<Vec<u8>>;
pub type Response = http::Response<Vec<u8>>;
pub fn handle<F>(func: F)
where F: Fn(Request) -> Response
{
let env_vars: HashMap<String, String> = std::env::vars().collect();
let content_length: usize = env_vars.get("CONTENT_LENGTH").and_then(|cl| cl.parse::<usize>().ok()).unwrap_or(0);
let mut stdin_contents = vec![0; content_length];
stdin().read_exact(&mut stdin_contents).unwrap();
let request = parse_request(env_vars, stdin_contents);
let response = func(request);
let output = serialize_response(response);
std::io::stdout().write_all(&output).unwrap();
}
pub fn empty_response<T>(status_code: T) -> Response
where http::StatusCode: http::HttpTryFrom<T>
{
http::response::Builder::new().status(status_code).body(vec![]).unwrap()
}
pub fn html_response<T, S>(status_code: T, body: S) -> Response
where http::StatusCode: http::HttpTryFrom<T>,
S: Into<String>
{
let body: Vec<u8> = body.into().into_bytes();
http::response::Builder::new()
.status(status_code)
.header(http::header::CONTENT_TYPE, "text/html")
.header(http::header::CONTENT_LENGTH, format!("{}", body.len()).as_str())
.body(body)
.unwrap()
}
pub fn string_response<T, S>(status_code: T, body: S) -> Response
where http::StatusCode: http::HttpTryFrom<T>,
S: Into<String>
{
let body: Vec<u8> = body.into().into_bytes();
http::response::Builder::new()
.status(status_code)
.header(http::header::CONTENT_LENGTH, format!("{}", body.len()).as_str())
.body(body)
.unwrap()
}
pub fn binary_response<T>(status_code: T, body: Vec<u8>) -> Response
where http::StatusCode: http::HttpTryFrom<T>
{
http::response::Builder::new()
.status(status_code)
.header(http::header::CONTENT_LENGTH, format!("{}", body.len()).as_str())
.body(body)
.unwrap()
}
fn parse_request(env_vars: HashMap<String, String>, stdin: Vec<u8>) -> Request {
let mut req = http::Request::builder();
req.method(env_vars["REQUEST_METHOD"].as_str());
let uri = if env_vars.get("QUERY_STRING").unwrap_or(&"".to_owned()) != "" {
format!("{}?{}", env_vars["SCRIPT_NAME"], env_vars["QUERY_STRING"])
} else {
env_vars["SCRIPT_NAME"].to_owned()
};
req.uri(uri.as_str());
if let Some(v) = env_vars.get("SERVER_PROTOCOL") {
if v == "HTTP/0.9" {
req.version(http::version::Version::HTTP_09);
} else if v == "HTTP/1.0" {
req.version(http::version::Version::HTTP_10);
} else if v == "HTTP/1.1" {
req.version(http::version::Version::HTTP_11);
} else {
unimplemented!("Unsupport HTTP SERVER_PROTOCOL {:?}", v);
}
}
for key in env_vars.keys().filter(|k| k.starts_with("HTTP_")) {;
let header: String = key.chars().skip(5).map(|c| if c == '_' { '-' } else { c }).collect();
req.header(header.as_str(), env_vars[key].as_str().trim());
}
add_header(&mut req, &env_vars, "AUTH_TYPE", "X-CGI-Auth-Type");
add_header(&mut req, &env_vars, "CONTENT_LENGTH", "X-CGI-Content-Length");
add_header(&mut req, &env_vars, "CONTENT-TYPE", "X-CGI-Content-Type");
add_header(&mut req, &env_vars, "GATEWAY_INTERFACE", "X-CGI-Gateway-Interface");
add_header(&mut req, &env_vars, "PATH_INFO", "X-CGI-Path-Info");
add_header(&mut req, &env_vars, "PATH_TRANSLATED", "X-CGI-Path-Translated");
add_header(&mut req, &env_vars, "QUERY_STRING", "X-CGI-Query-String");
add_header(&mut req, &env_vars, "REMOTE_ADDR", "X-CGI-Remote-Addr");
add_header(&mut req, &env_vars, "REMOTE_HOST", "X-CGI-Remote-Host");
add_header(&mut req, &env_vars, "REMOTE_IDENT", "X-CGI-Remote-Ident");
add_header(&mut req, &env_vars, "REMOTE_USER", "X-CGI-Remote-User");
add_header(&mut req, &env_vars, "REQUEST_METHOD", "X-CGI-Request-Method");
add_header(&mut req, &env_vars, "SCRIPT_NAME", "X-CGI-Script-Name");
add_header(&mut req, &env_vars, "SERVER_PORT", "X-CGI-Server-Port");
add_header(&mut req, &env_vars, "SERVER_PROTOCOL", "X-CGI-Server-Protocol");
add_header(&mut req, &env_vars, "SERVER_SOFTWARE", "X-CGI-Server-Software");
req.body(stdin).unwrap()
}
fn add_header(req: &mut http::request::Builder, env_vars: &HashMap<String, String>, meta_var: &str, target_header: &str) {
if let Some(var) = env_vars.get(meta_var) {
req.header(target_header, var.as_str());
}
}
fn serialize_response(response: Response) -> Vec<u8> {
let mut output = String::new();
output.push_str("Status: ");
output.push_str(response.status().as_str());
if let Some(reason) = response.status().canonical_reason() {
output.push_str(" ");
output.push_str(reason);
}
output.push_str("\n");
{
let headers = response.headers();
let mut keys: Vec<&http::header::HeaderName> = headers.keys().collect();
keys.sort_by_key(|h| h.as_str());
for key in keys {
output.push_str(key.as_str());
output.push_str(": ");
output.push_str(headers.get(key).unwrap().to_str().unwrap());
output.push_str("\n");
}
}
output.push_str("\n");
let mut output = output.into_bytes();
let (_, mut body) = response.into_parts();
output.append(&mut body);
output
}
#[cfg(test)]
mod tests {
use super::*;
fn env(input: Vec<(&str, &str)>) -> Vec<(String, String)> {
input.into_iter().map(|(a, b)| (a.to_owned(), b.to_owned())).collect()
}
#[test]
fn test_parse_request() {
let env_vars = env(vec![
("REQUEST_METHOD", "GET"), ("SCRIPT_NAME", "/my/path/script"),
("SERVER_PROTOCOL", "HTTP/1.0"), ("HTTP_USER_AGENT", "MyBrowser/1.0"),
("QUERY_STRING", "foo=bar&baz=bop"),
]);
let stdin = Vec::new();
let req = parse_request(env_vars, stdin);
assert_eq!(req.method(), &http::method::Method::GET);
assert_eq!(req.uri(), "/my/path/script?foo=bar&baz=bop");
assert_eq!(req.uri().path(), "/my/path/script");
assert_eq!(req.uri().query(), Some("foo=bar&baz=bop"));
assert_eq!(req.version(), http::version::Version::HTTP_10);
assert_eq!(req.headers()[http::header::USER_AGENT], "MyBrowser/1.0");
assert_eq!(req.body(), &vec![]);
}
fn test_serialized_response(resp: &mut http::response::Builder, body: &str, expected_output: &str) {
let resp: Response = resp.body(String::from(body).into_bytes()).unwrap();
let output = serialize_response(resp);
let expected_output = String::from(expected_output).into_bytes();
if output != expected_output {
println!("output: {}\nexptected: {}", std::str::from_utf8(&output).unwrap(), std::str::from_utf8(&expected_output).unwrap());
}
assert_eq!(output, expected_output);
}
#[test]
fn test_serialized_response1() {
test_serialized_response(
http::Response::builder().status(200),
"Hello World",
"Status: 200 OK\n\nHello World"
);
test_serialized_response(
http::Response::builder().status(200)
.header("Content-Type", "text/html")
.header("Content-Language", "en")
.header("Cache-Control", "max-age=3600"),
"<html><body><h1>Hello</h1></body></html>",
"Status: 200 OK\ncache-control: max-age=3600\ncontent-language: en\ncontent-type: text/html\n\n<html><body><h1>Hello</h1></body></html>"
);
}
}