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
extern crate semver;
use std::io::prelude::*;
use std::collections::HashMap;
use std::error::Error;
use std::net::SocketAddr;
pub use self::typemap::TypeMap;
mod typemap;
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum Scheme {
Http,
Https
}
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum Host<'a> {
Name(&'a str),
Socket(SocketAddr)
}
#[derive(PartialEq, Hash, Eq, Debug, Clone, Copy)]
pub enum Method {
Get,
Post,
Put,
Delete,
Head,
Connect,
Options,
Trace,
Patch,
Purge,
Other(&'static str)
}
pub type Extensions = TypeMap;
pub trait Request {
fn http_version(&self) -> semver::Version;
fn conduit_version(&self) -> semver::Version;
fn method(&self) -> Method;
fn scheme(&self) -> Scheme;
fn host<'a>(&'a self) -> Host<'a>;
fn virtual_root<'a>(&'a self) -> Option<&'a str>;
fn path<'a>(&'a self) -> &'a str;
fn query_string<'a>(&'a self) -> Option<&'a str>;
fn remote_addr(&self) -> SocketAddr;
fn content_length(&self) -> Option<u64>;
fn headers<'a>(&'a self) -> &'a Headers;
fn body<'a>(&'a mut self) -> &'a mut Read;
fn extensions<'a>(&'a self) -> &'a Extensions;
fn mut_extensions<'a>(&'a mut self) -> &'a mut Extensions;
}
pub trait Headers {
fn find(&self, key: &str) -> Option<Vec<&str>>;
fn has(&self, key: &str) -> bool;
fn all(&self) -> Vec<(&str, Vec<&str>)>;
}
pub struct Response {
pub status: (u32, &'static str),
pub headers: HashMap<String, Vec<String>>,
pub body: Box<Read + Send>
}
pub trait Handler: Sync + Send + 'static {
fn call(&self, request: &mut Request) -> Result<Response, Box<Error+Send>>;
}
impl<F, E> Handler for F
where F: Fn(&mut Request) -> Result<Response, E> + Sync + Send + 'static,
E: Error + Send + 'static
{
fn call(&self, request: &mut Request) -> Result<Response, Box<Error+Send>> {
(*self)(request).map_err(|e| Box::new(e) as Box<Error+Send>)
}
}