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
use std::error;
use std::fmt;
use std::io;
use std::io::Error as IoError;
use std::io::BufRead;
use std::io::Read;
use std::process::Command;
use std::process::Stdio;
use Request;
use Response;
use ResponseBody;
#[derive(Debug)]
pub enum CgiError {
BodyAlreadyExtracted,
IoError(IoError),
}
impl From<IoError> for CgiError {
fn from(err: IoError) -> CgiError {
CgiError::IoError(err)
}
}
impl error::Error for CgiError {
#[inline]
fn description(&self) -> &str {
match *self {
CgiError::BodyAlreadyExtracted => {
"the body of the request was already extracted"
},
CgiError::IoError(_) => {
"could not read the body from the request, or could not execute the CGI program"
},
}
}
#[inline]
fn cause(&self) -> Option<&error::Error> {
match *self {
CgiError::IoError(ref e) => Some(e),
_ => None
}
}
}
impl fmt::Display for CgiError {
#[inline]
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(fmt, "{}", error::Error::description(self))
}
}
pub trait CgiRun {
fn start_cgi(self, request: &Request) -> Result<Response, CgiError>;
}
impl CgiRun for Command {
fn start_cgi(mut self, request: &Request) -> Result<Response, CgiError> {
self.env("SERVER_SOFTWARE", "rouille")
.env("SERVER_NAME", "localhost")
.env("GATEWAY_INTERFACE", "CGI/1.1")
.env("SERVER_PROTOCOL", "HTTP/1.1")
.env("SERVER_PORT", "80")
.env("REQUEST_METHOD", request.method())
.env("PATH_INFO", &request.url())
.env("SCRIPT_NAME", "")
.env("QUERY_STRING", request.raw_query_string())
.env("REMOTE_ADDR", &request.remote_addr().to_string())
.env("AUTH_TYPE", "")
.env("REMOTE_USER", "")
.env("CONTENT_TYPE", &request.header("Content-Type").unwrap_or(""))
.env("CONTENT_LENGTH", &request.header("Content-Length").unwrap_or(""))
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.stdin(Stdio::piped());
let mut child = try!(self.spawn());
if let Some(mut body) = request.data() {
try!(io::copy(&mut body, child.stdin.as_mut().unwrap()));
} else {
return Err(CgiError::BodyAlreadyExtracted);
}
let response = {
let mut stdout = io::BufReader::new(child.stdout.take().unwrap());
let mut headers = Vec::new();
let mut status_code = 200;
for header in stdout.by_ref().lines() {
let header = try!(header);
if header.is_empty() { break; }
let mut splits = header.splitn(2, ':');
let header = splits.next().unwrap();
let val = splits.next().unwrap();
let val = &val[1..];
if header == "Status" {
status_code = val[0..3].parse().expect("Status returned by CGI program is invalid");
} else {
headers.push((header.to_owned().into(), val.to_owned().into()));
}
}
Response {
status_code,
headers,
data: ResponseBody::from_reader(stdout),
upgrade: None,
}
};
Ok(response)
}
}