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
#![allow(dead_code)]
#![allow(unused_mut)]

use std::collections::HashMap;
use regex::Regex;
use http::*;

pub enum REST {
    NONE,
    GET,
    POST,
    PUT,
    DELETE,
    OTHER(String),
}

impl Default for REST {
    fn default() -> REST { REST::NONE }
}

#[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub enum RequestPath {
    Exact(&'static str),
    Partial(&'static str),
    WildCard(&'static str),
}

/* Manual mayham...

impl PartialEq for RequestPath {
    fn eq(&self, other: &RequestPath) -> bool {
        match self {
            &RequestPath::Literal(lit_val) => {
                match other {
                    &RequestPath::Literal(other_val) => lit_val == other_val,
                    _ => false,
                }
            },
            &RequestPath::WildCard(wild_card_val) => {
                match other {
                    &RequestPath::WildCard(other_val) => wild_card_val == other_val,
                    _ => false,
                }
            }
        }
    }
}

impl Eq for RequestPath {}

impl Hash for RequestPath {
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            &RequestPath::Literal(lit_val) => lit_val.hash(state),
            &RequestPath::WildCard(wild_card_val) => wild_card_val.hash(state)
        }
    }
}

 * End of manual mayham
 */

pub type Callback = fn(Request, &mut Response);

pub struct Route {
    get: HashMap<RequestPath, Callback>,
    post: HashMap<RequestPath, Callback>,
    put: HashMap<RequestPath, Callback>,
    delete: HashMap<RequestPath, Callback>,
    others: HashMap<RequestPath, Callback>,
}

pub trait Router {
    fn get(&mut self, uri: RequestPath, callback: Callback);
    fn post(&mut self, uri: RequestPath, callback: Callback);
    fn put(&mut self, uri: RequestPath, callback: Callback);
    fn delete(&mut self, uri: RequestPath, callback: Callback);
    fn other(&mut self, uri: RequestPath, callback: Callback);
}

pub trait RouteHandler {
    fn handle_get(&self, req: Request, resp: &mut Response);
    fn handle_put(&self, req: Request, resp: &mut Response);
    fn handle_post(&self, req: Request, resp: &mut Response);
    fn handle_delete(&self, req: Request, resp: &mut Response);
    fn handle_other(&self, req: Request, resp: &mut Response);
}

impl Route {
    pub fn new() -> Self {
        Route {
            get: HashMap::new(),
            post: HashMap::new(),
            put: HashMap::new(),
            delete: HashMap::new(),
            others: HashMap::new(),
        }
    }

    pub fn from(source: &Route) -> Self {
        Route {
            get: source.get.clone(),
            put: source.put.clone(),
            post: source.post.clone(),
            delete: source.delete.clone(),
            others: source.others.clone(),
        }
    }
}

impl Router for Route {
    //TODO: add special handling for "*"

    fn get(&mut self, uri: RequestPath, callback: Callback) {
        self.get.entry(uri).or_insert(callback);
    }

    fn put(&mut self, uri: RequestPath, callback: Callback) {
        self.put.entry(uri).or_insert( callback);
    }

    fn post(&mut self, uri: RequestPath, callback: Callback) {
        self.post.entry(uri).or_insert(callback);
    }

    fn delete(&mut self, uri: RequestPath, callback: Callback) {
        self.delete.entry(uri).or_insert( callback);
    }

    fn other(&mut self, uri: RequestPath, callback: Callback) {
        self.others.entry(uri).or_insert( callback);
    }
}

impl RouteHandler for Route {
    fn handle_get(&self, req: Request, resp: &mut Response) {
        handle_request_worker(&self.get, req, resp)
    }

    fn handle_put(&self, req: Request, resp: &mut Response) {
        handle_request_worker(&self.put, req, resp)
    }

    fn handle_post(&self, req: Request, resp: &mut Response) {
        handle_request_worker(&self.post, req, resp)
    }

    fn handle_delete(&self, req: Request, resp: &mut Response) {
        handle_request_worker(&self.delete, req, resp)
    }

    fn handle_other(&self, req: Request, resp: &mut Response) {
        handle_request_worker(&self.others, req, resp)
    }
}

fn handle_request_worker(routes: &HashMap<RequestPath, Callback>, req: Request, resp: &mut Response) {
    if let Some(callback) = seek_path(&routes, req.path.clone()) {
        //Callback function will decide what to be written into the response
        callback(req, resp);
    } else {
        resp.status(404);
    }
}

fn seek_path(routes: &HashMap<RequestPath, Callback>, uri: String) -> Option<&Callback> {
    for (req_path, callback) in routes.iter() {
        match req_path.to_owned() {
            RequestPath::Exact(val) => {
                if uri.eq(&val) {
                    return Some(callback);
                }
            },
            RequestPath::Partial(val) => {
                if uri.starts_with(&val) {
                    return Some(callback);
                }
            },
            RequestPath::WildCard(wild) => {
                if let Ok(re) = Regex::new(wild) {
                    if re.is_match(&uri) {
                        return Some(callback);
                    }
                }
            }
        }
    }

    None
}