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
use ::util::{open_with_metadata, OpenWithMetadataFuture, RequestedPath};
use futures::{Async::*, Future, Poll};
use http::{Method, Request};
use std::convert::AsRef;
use std::fs::Metadata;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
use tokio::fs::File;
#[derive(Debug)]
pub enum ResolveResult {
MethodNotMatched,
UriNotMatched,
NotFound,
PermissionDenied,
IsDirectory,
Found(File, Metadata),
}
enum ResolveState {
MethodNotMatched,
UriNotMatched,
WaitOpen(OpenWithMetadataFuture),
WaitOpenIndex(OpenWithMetadataFuture),
}
fn map_open_err(err: Error) -> Poll<ResolveResult, Error> {
match err.kind() {
ErrorKind::NotFound => Ok(Ready(ResolveResult::NotFound)),
ErrorKind::PermissionDenied => Ok(Ready(ResolveResult::PermissionDenied)),
_ => Err(err),
}
}
pub struct ResolveFuture {
full_path: Option<PathBuf>,
is_dir_request: bool,
state: ResolveState,
}
impl Future for ResolveFuture {
type Item = ResolveResult;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
loop {
self.state = match self.state {
ResolveState::MethodNotMatched => {
return Ok(Ready(ResolveResult::MethodNotMatched));
},
ResolveState::UriNotMatched => {
return Ok(Ready(ResolveResult::UriNotMatched));
},
ResolveState::WaitOpen(ref mut future) => {
let (file, metadata) = match future.poll() {
Ok(Ready(pair)) => pair,
Ok(NotReady) => return Ok(NotReady),
Err(err) => return map_open_err(err),
};
if self.is_dir_request && !metadata.is_dir() {
return Ok(Ready(ResolveResult::NotFound));
}
if !self.is_dir_request && metadata.is_dir() {
return Ok(Ready(ResolveResult::IsDirectory));
}
if !self.is_dir_request {
return Ok(Ready(ResolveResult::Found(file, metadata)));
}
let mut full_path = self.full_path.take().expect("invalid state");
full_path.push("index.html");
ResolveState::WaitOpenIndex(open_with_metadata(full_path))
},
ResolveState::WaitOpenIndex(ref mut future) => {
let (file, metadata) = match future.poll() {
Ok(Ready(pair)) => pair,
Ok(NotReady) => return Ok(NotReady),
Err(err) => return map_open_err(err),
};
if metadata.is_dir() {
return Ok(Ready(ResolveResult::NotFound));
}
return Ok(Ready(ResolveResult::Found(file, metadata)));
},
}
}
}
}
pub fn resolve<B, P: AsRef<Path>>(root: P, req: &Request<B>) -> ResolveFuture {
match *req.method() {
Method::HEAD | Method::GET => {},
_ => {
return ResolveFuture {
full_path: None,
is_dir_request: false,
state: ResolveState::MethodNotMatched,
};
},
}
if req.uri().scheme_part().is_some() || req.uri().host().is_some() {
return ResolveFuture {
full_path: None,
is_dir_request: false,
state: ResolveState::UriNotMatched,
};
}
let RequestedPath { full_path, is_dir_request } =
RequestedPath::resolve(root.as_ref(), req.uri().path());
let state = ResolveState::WaitOpen(open_with_metadata(full_path.clone()));
let full_path = Some(full_path);
ResolveFuture { full_path, is_dir_request, state }
}