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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use std::convert::Into;
use std::fmt;
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::path::PathBuf;
use std::net::ToSocketAddrs;

use hyper;
use hyper::method::Method;
use hyper::status::StatusCode;
use hyper::server::Request as HTTPRequest;
use hyper::server::Response as HTTPResponse;

use types::{
    PenError,
        PenHTTPError,
        PenUserError,

    UserError,
    PenResult,
    ViewFunc,
    HTTPErrorHandler,
    UserErrorHandler,
    BeforeRequestFunc,
    AfterRequestFunc,
    TeardownRequestFunc,
};
use wrappers::{
    Request,
    Response,
};
use helpers::{PathBound, send_from_directory_range, redirect};
use serving::run_server;
use routing::{Map, Rule, Matcher};
use http_errors::{HTTPError, NotFound, InternalServerError};
use typemap::ShareMap;

const DEFAULT_THREADS: usize = 15;

pub struct Pen {
    pub root_path: String,
    pub name: String,
    pub static_folder: String,
    pub static_url_path: String,
    pub template_folder: String,
    pub extensions: ShareMap,
    pub url_map: Map,
    view_functions: HashMap<String, ViewFunc>,
    before_request_funcs: Vec<Box<BeforeRequestFunc>>,
    after_request_funcs: Vec<Box<AfterRequestFunc>>,
    teardown_request_funcs: Vec<Box<TeardownRequestFunc>>,
    http_error_handlers: HashMap<u16, Box<HTTPErrorHandler>>,
    user_error_handlers: HashMap<String, Box<UserErrorHandler>>,
}

impl Pen {
    pub fn new(root_path: &str) -> Pen {
        Pen {
            root_path: root_path.to_string(),
            name: root_path.to_string(),
            static_folder: String::from("static"),
            static_url_path: String::from("/static"),
            template_folder: String::from("templates"),
            extensions: ShareMap::custom(),
            url_map: Map::new(),
            view_functions: HashMap::new(),
            before_request_funcs: vec![],
            after_request_funcs: vec![],
            teardown_request_funcs: vec![],
            http_error_handlers: HashMap::new(),
            user_error_handlers: HashMap::new(),
        }
    }

    pub fn is_debug(&self) -> bool { false }
    pub fn is_testing(&self) -> bool { false }

    pub fn route<M: Into<Matcher>, N: AsRef<[Method]>>(&mut self, rule: M, methods: N, endpoint: &str, view_func: ViewFunc) {
        self.add_url_rule(rule.into(), methods.as_ref(), endpoint, view_func);
    }

    pub fn get<M: Into<Matcher>>(&mut self, rule: M, endpoint: &str, view_func: ViewFunc) {
        self.route(rule, &[Method::Get], endpoint, view_func);
    }

    pub fn post<M: Into<Matcher>>(&mut self, rule: M, endpoint: &str, view_func: ViewFunc) {
        self.route(rule, &[Method::Post], endpoint, view_func);
    }

    pub fn delete<M: Into<Matcher>>(&mut self, rule: M, endpoint: &str, view_func: ViewFunc) {
        self.route(rule, &[Method::Delete], endpoint, view_func);
    }

    pub fn patch<M: Into<Matcher>>(&mut self, rule: M, endpoint: &str, view_func: ViewFunc) {
        self.route(rule, &[Method::Patch], endpoint, view_func);
    }

    pub fn put<M: Into<Matcher>>(&mut self, rule: M, endpoint: &str, view_func: ViewFunc) {
        self.route(rule, &[Method::Put], endpoint, view_func);
    }

    pub fn add_url_rule(&mut self, matcher: Matcher, methods: &[Method], endpoint: &str, view_func: ViewFunc) {
        let url_rule = Rule::new(matcher, methods, endpoint);
        self.url_map.add(url_rule);
        self.view_functions.insert(endpoint.to_string(), view_func);
    }

    pub fn enable_static_file_handling(&mut self) {
        let rule = self.static_url_path.clone() + "/<filename:path>";
        self.route(&rule as &str, &[Method::Get], "static", send_app_static_file);
    }

    pub fn before_request<F: Fn(&mut Request) -> Option<PenResult> + Send + Sync + 'static>(&mut self, f: F) {
        self.before_request_funcs.push(Box::new(f));
    }

    pub fn after_request<F: Fn(&Request, &mut Response) + Send + Sync + 'static>(&mut self, f: F) {
        self.after_request_funcs.push(Box::new(f));
    }

    pub fn teardown_request<F: Fn(Option<&PenError>) + Send + Sync + 'static>(&mut self, f: F) {
        self.teardown_request_funcs.push(Box::new(f));
    }

    pub fn register_http_error_handler<F: Fn(HTTPError) -> PenResult + Send + Sync + 'static>(&mut self, status_code: u16, f: F) {
        self.http_error_handlers.insert(status_code, Box::new(f));
    }

    pub fn register_user_error_handler<F: Fn(UserError) -> PenResult + Send + Sync + 'static>(&mut self, error_desc: &str, f: F) {
        self.user_error_handlers.insert(error_desc.to_string(), Box::new(f));
    }

    pub fn httperrorhandler<F: Fn(HTTPError) -> PenResult + Send + Sync + 'static>(&mut self, status_code: u16, f: F) {
        self.register_http_error_handler(status_code, f);
    }

    pub fn usererrorhandler<F: Fn(UserError) -> PenResult + Send + Sync + 'static>(&mut self, error_desc: &str, f: F) {
        self.register_user_error_handler(error_desc, f);
    }

    fn preprocess_request(&self, request: &mut Request) -> Option<PenResult> {
        for func in &self.before_request_funcs {
            if let Some(result) = func(request) {
                return Some(result);
            }
        }
        None
    }

    fn dispatch_request(&self, request: &mut Request) -> PenResult {
        if let Some(ref routing_error) = request.routing_error {
            Err(PenHTTPError(routing_error.clone()))
        }
        else if let Some((ref redirect_url, redirect_code)) = request.routing_redirect {
            redirect(redirect_url, redirect_code)
        }
        else if let Some(default_options_response) = self.make_default_options_response(request) {
            Ok(default_options_response)
        }
        else {
            match self.view_functions.get(&request.endpoint().unwrap()) {
                Some(&view_func) => view_func(request),
                None => Err(PenHTTPError(NotFound)),
            }
        }
    }

    fn make_default_options_response(&self, request: &Request) -> Option<Response> {
        if let Some(ref rule) = request.url_rule {
            if rule.provide_automatic_options && request.method() == Method::Options {
                let url_adapter = request.url_adapter();
                let mut response = Response::new_empty();
                response.headers.set(hyper::header::Allow(url_adapter.allowed_methods()));
                Some(response)
            }
            else { None }
        } else { None }
    }

    fn process_response(&self, request: &Request, response: &mut Response) {
        for func in self.after_request_funcs.iter().rev() { func(request, response); }
    }

    fn do_teardown_request(&self, e: Option<&PenError>) {
        for func in self.teardown_request_funcs.iter().rev() { func(e); }
    }

    fn handle_all_error(&self, e: PenError) -> PenResult {
        match e {
            PenHTTPError(e) => self.handle_http_error(e),
            PenUserError(e) => self.handle_user_error(e),
        }
    }

    fn handle_user_error(&self, e: UserError) -> PenResult {
        if let Some(handler) = self.user_error_handlers.get(&e.desc) {
            handler(e)
        } else { Err(PenUserError(e)) }
    }

    fn handle_http_error(&self, e: HTTPError) -> PenResult {
        if let Some(handler) = self.http_error_handlers.get(&e.code()) {
            handler(e)
        } else { Ok(e.to_response()) }
    }

    fn handle_error(&self, request: &Request, e: &PenError) -> Response {
        self.log_error(request, e);
        if let Ok(response) = self.handle_http_error(InternalServerError) {
            response
        } else {
            InternalServerError.to_response()
        }
    }

    fn log_error(&self, request: &Request, e: &PenError) {
        eprintln!("Error on {} [{}]: {}", request.path(), request.method(), e.description());
    }

    fn full_dispatch_request(&self, request: &mut Request) -> Result<Response, PenError> {
        let result = match self.preprocess_request(request) {
            Some(result) => result,
            None => self.dispatch_request(request),
        };
        let rv = match result {
            Ok(response) => Ok(response),
            Err(e) => self.handle_all_error(e),
        };
        match rv {
            Ok(mut response) => {
                self.process_response(request, &mut response);
                Ok(response)
            },
            Err(e) => Err(e),
        }
    }

    pub fn handle_request(&self, request: &mut Request) -> Response {
        request.match_request();
        match self.full_dispatch_request(request) {
            Ok(response) => {
                self.do_teardown_request(None);
                response
            },
            Err(e) => {
                let response = self.handle_error(request, &e);
                self.do_teardown_request(Some(&e));
                response
            }
        }
    }

    pub fn run<A: ToSocketAddrs>(self, addr: A) {
        run_server(self, addr, DEFAULT_THREADS);
    }

    pub fn run_threads<A: ToSocketAddrs>(self, addr: A, threads: usize) {
        run_server(self, addr, threads);
    }
}

impl hyper::server::Handler for Pen {
    fn handle(&self, req: HTTPRequest, mut res: HTTPResponse) {
        match Request::new(self, req) {
            Ok(mut request) => {
                let response = self.handle_request(&mut request);
                response.write(request.method(), res);
            }
            Err(_) => {
                *res.status_mut() = StatusCode::BadRequest;
                if let Ok(w) = res.start() {
                    let _ = w.end();
                }
            }
        };
    }
}

impl PathBound for Pen {
    fn open_resource(&self, resource: &str) -> File {
        let mut pathbuf = PathBuf::from(&self.root_path);
        pathbuf.push(resource);
        File::open(&pathbuf.as_path()).unwrap()
    }
}

impl fmt::Display for Pen {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "<Pen application {}>", self.name)
    }
}

impl fmt::Debug for Pen {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "<Pen application {}>", self.name)
    }
}

fn send_app_static_file(request: &mut Request) -> PenResult {
    let mut static_path = PathBuf::from(&request.app.root_path);
    static_path.push(&request.app.static_folder);
    send_from_directory_range(static_path.to_str().unwrap(), &request.view_args["filename"], false, request.headers().get())
}