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
267
268
269
270
271
272
273
274
use crate::context::{Request, Response};
use crate::file_server::FileServer;
use crate::router::{RouteParams, Router};
use std::sync::Arc;
type FallbackCallback = Arc<dyn Fn(&mut Request) -> Response + Send + Sync>;
/// Configuration for a `vintage` FastCGI Server
#[derive(Clone, Default)]
pub struct ServerConfig {
pub(crate) file_server: Option<FileServer>,
pub(crate) router: Option<Router>,
pub(crate) fallback: Option<FallbackCallback>,
}
impl ServerConfig {
/// Creates a new specification for a FastCGI server
pub fn new() -> Self {
Self::default()
}
/// Adds support for serving static files
///
/// Matches requests that start with `prefix` and uses the rest of the path to lookup a file on
/// at `path`.
///
/// If `prefix` does not begin with a forward slash (e.g. `/static`), it is implied.
/// An empty `path` implies the current working directory.
///
/// # Panics
///
/// Panics if `path` contains invalid utf8 values
pub fn serve_files(mut self, prefix: &'static str, path: &'static str) -> Self {
self.file_server = Some(FileServer::new(prefix, path));
self
}
/// Registers a callback tied to a `method` and a set of `paths`.
///
/// If multiple paths are provided, the callback is triggered if any of them match.
///
/// Paths support basic segment matching.
/// Matched path segments are passed to the callback as a second argument.
///
/// # Path Matching Syntax
///
/// _Segment_ matchers look like `/{id}/whatever`.
/// They match everything until the next `/` or the end of the path.
/// They must match a complete segment.
/// Suffixes/prefixes are not supported.
///
/// ```
/// use vintage::{Response, ServerConfig};
///
/// let config = ServerConfig::new()
/// .on("GET", ["/echo/{name}"], |_req, params| {
/// Response::text(¶ms["name"])
/// });
///
/// let handle = vintage::start(config, "localhost:0").unwrap();
///
/// handle.stop();
/// ```
///
///
/// _Wildcard_ matchers start with a `*` and match everything until the end of the path.
/// As such, they must always appear at the end of the path.
///
/// In the following example, if the request path was `/folder/a/b/c`, `¶ms["subfolders"]` would
/// be `a/b/c`.
///
/// ```
/// use vintage::{Response, ServerConfig};
///
/// let config = ServerConfig::new()
/// .on("GET", ["/folder/{*rest}"], |_req, params| {
/// Response::text(¶ms["rest"])
/// });
///
/// let handle = vintage::start(config, "localhost:0").unwrap();
///
/// handle.stop()
/// ```
pub fn on<C, const N: usize>(
mut self,
method: &'static str,
paths: [&str; N],
callback: C,
) -> Self
where
C: Fn(&mut Request, RouteParams) -> Response,
C: 'static + Send + Sync,
{
let mut router = self.router.unwrap_or_default();
router.register(method, paths, callback);
self.router = Some(router);
self
}
/// Registers a path for the "GET" method
///
/// See [`ServerConfig::on`]
pub fn on_get<C, const N: usize>(self, paths: [&str; N], callback: C) -> Self
where
C: Fn(&mut Request, RouteParams) -> Response,
C: 'static + Send + Sync,
{
self.on("GET", paths, callback)
}
/// Registers a path for the "POST" method
///
/// See [`ServerConfig::on`]
pub fn on_post<C, const N: usize>(self, paths: [&str; N], callback: C) -> Self
where
C: Fn(&mut Request, RouteParams) -> Response,
C: 'static + Send + Sync,
{
self.on("POST", paths, callback)
}
/// Registers a path for the "PUT" method
///
/// See [`ServerConfig::on`]
pub fn on_put<C, const N: usize>(self, paths: [&str; N], callback: C) -> Self
where
C: Fn(&mut Request, RouteParams) -> Response,
C: 'static + Send + Sync,
{
self.on("PUT", paths, callback)
}
/// Registers a path for the "DELETE" method
///
/// See [`ServerConfig::on`]
pub fn on_delete<C, const N: usize>(self, paths: [&str; N], callback: C) -> Self
where
C: Fn(&mut Request, RouteParams) -> Response,
C: 'static + Send + Sync,
{
self.on("DELETE", paths, callback)
}
/// Registers a callback that will be invoked for any unhandled requests
pub fn unhandled<C>(mut self, callback: C) -> Self
where
C: Fn(&mut Request) -> Response,
C: 'static + Send + Sync,
{
self.fallback = Some(Arc::new(callback));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::connection::Connection;
use crate::error::Error;
use crate::record::*;
use assert_matches::assert_matches;
use mio::net::TcpStream;
use std::net::SocketAddr;
macro_rules! records {
($($record:expr),* $(,)?) => {{
#[allow(unused_mut)]
let mut records: Vec<Record> = vec![];
$(
records.push($record.into());
)*
records
}}
}
fn basic_params() -> Params {
Params::default()
.add("REQUEST_METHOD", "GET")
.add("PATH_INFO", "/")
.add("QUERY_STRING", "")
}
// Test that when we send `to_send` records to the server at `address`, we get back the
// `expected` records
#[track_caller]
fn assert_request(address: SocketAddr, to_send: Vec<Record>, mut expected: Vec<Record>) {
let socket = TcpStream::connect(address).unwrap();
let mut connection = Connection::try_from(socket).unwrap();
for record in to_send.iter() {
connection.write_record(record).unwrap();
}
loop {
if expected.is_empty() {
let result = connection.read_record();
assert_matches!(result, Err(Error::UnexpectedSocketClose(_)));
break;
}
match connection.read_record() {
Ok(record) => {
assert_eq!(record, expected.remove(0));
}
Err(err) => panic!("{err}"),
}
}
}
#[test]
fn get_values() {
let server = crate::start(ServerConfig::new(), "localhost:0").unwrap();
assert_request(
server.address(),
records! {
GetValues::default(),
},
records! {
GetValuesResult::default(),
},
);
assert_request(
server.address(),
records! {
GetValues::default().add("FCGI_MPXS_CONNS").add("VALUE_WE_DONT_KNOW"),
},
records! {
GetValuesResult::default().add("FCGI_MPXS_CONNS", "0"),
},
);
}
#[test]
fn unsupported_keepalive() {
let server = crate::start(ServerConfig::new(), "localhost:0").unwrap();
assert_request(
server.address(),
records! {
BeginRequest::new(Role::Responder, true),
basic_params(),
Stdin(vec![])
},
records! {
EndRequest::new(0, ProtocolStatus::MultiplexingUnsupported)
},
);
}
#[test]
fn successful_responder_flow() {
// A server that echoes the body
let config = ServerConfig::new().unhandled(|req| {
let body = std::mem::take(&mut req.body);
Response::default().set_raw_body(body)
});
let server = crate::start(config, "localhost:0").unwrap();
assert_request(
server.address(),
records! {
BeginRequest::new(Role::Responder, false),
basic_params(),
Stdin(b"BAR".to_vec())
},
records! {
Stdout(b"Status: 200\n\nBAR".to_vec()),
EndRequest::new(0, ProtocolStatus::RequestComplete)
},
);
}
}