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
305
306
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::sync::Arc;

use ferrum::{Request, Response, Handler, FerrumResult, FerrumError};
use ferrum::{header, Method, StatusCode};
use ferrum::typemap::Key;

use recognizer::{Glob, GlobTypes, Recognizer, Recognize, RouteMatch, Params};

pub mod id;
pub use self::id::*;

pub struct RouterInner {
    /// The routers, specialized by method.
    pub routers: HashMap<Method, Vec<Arc<Recognizer>>>,

    /// Routes that accept any method.
    pub wildcard: Vec<Arc<Recognizer>>,

    /// Used in URI generation.
    pub route_ids: HashMap<Id, (String, Arc<Recognizer>)>,
}

/// `Router` provides an interface for creating complex routes as middleware
/// for the Ferrum framework.
pub struct Router {
    inner: Arc<RouterInner>
}

impl Router {
    /// Construct a new, empty `Router`.
    ///
    /// ```
    /// use ferrum_router::Router;
    /// let router = Router::new();
    /// ```
    pub fn new() -> Router {
        Router {
            inner: Arc::new(RouterInner {
                routers: HashMap::new(),
                wildcard: Vec::new(),
                route_ids: HashMap::new(),
            })
        }
    }

    fn mut_inner(&mut self) -> &mut RouterInner {
        Arc::get_mut(&mut self.inner).expect("Cannot modify router at this point.")
    }

    /// Add a new route to a `Router`, matching both a method and glob pattern.
    ///
    /// `route` supports glob patterns based on the rust regex and uses `{name}` (`{name: typename}`,
    /// `{name: pattern}`) for matching storing named segment of the request url in the `Params`
    /// object, which is stored in the request `extensions`.
    ///
    /// For instance, to route `Get` requests on any route matching
    /// `/users/{userid:[0-9]+}/{friendid:[0-9]+}` and store `userid` and `friend` in
    /// the exposed Params object:
    ///
    /// ```ignore
    /// let mut router = Router::new();
    /// router.route(Method::Get, "/users/{userid:[0-9]+}/{friendid:[0-9]+}", controller, "user_friend");
    /// ```
    ///
    /// `route_id` is a optional unique name for your route, and is used when generating an URI with
    /// `url_for`.
    ///
    /// The controller provided to route can be any `Handler`, which allows
    /// extreme flexibility when handling routes. For instance, you could provide
    /// a `Chain`, a `Handler`, which contains an authorization middleware and
    /// a controller function, so that you can confirm that the request is
    /// authorized for this route before handling it.
    pub fn route<G, H, S, T>(&mut self, method: Method, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        let glob = glob.into();
        let types = glob.types().map(|types| types.store());
        let recognizer = Arc::new(
            Recognizer::new(glob.path(), Box::new(handler), types).unwrap()
        );

        if let Some(route_id) = route_id {
            self.route_id(route_id, glob.path(), recognizer.clone());
        }

        self.mut_inner().routers
            .entry(method)
            .or_insert(Vec::new())
            .push(recognizer);
        self
    }

    fn route_id(&mut self, id: Id, glob_path: &[u8], recognizer: Arc<Recognizer>) {
        let inner = self.mut_inner();
        let ref mut route_ids = inner.route_ids;

        match route_ids.get(&id) {
            Some(&(ref other_glob_path, _)) if glob_path != other_glob_path.as_bytes() =>
                panic!("Duplicate route_id: {}", id),
            _ => ()
        };

        route_ids.insert(id, (String::from_utf8_lossy(glob_path).to_string(), recognizer));
    }

    /// Like route, but specialized to the `Get` method.
    pub fn get<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        self.route(Method::Get, glob, handler, route_id)
    }

    /// Like route, but specialized to the `Post` method.
    pub fn post<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        self.route(Method::Post, glob, handler, route_id)
    }

    /// Like route, but specialized to the `Put` method.
    pub fn put<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        self.route(Method::Put, glob, handler, route_id)
    }

    /// Like route, but specialized to the `Delete` method.
    pub fn delete<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        self.route(Method::Delete, glob, handler, route_id)
    }

    /// Like route, but specialized to the `Head` method.
    pub fn head<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        self.route(Method::Head, glob, handler, route_id)
    }

    /// Like route, but specialized to the `Patch` method.
    pub fn patch<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        self.route(Method::Patch, glob, handler, route_id)
    }

    /// Like route, but specialized to the `Options` method.
    pub fn options<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        self.route(Method::Options, glob, handler, route_id)
    }

    /// Route will match any method, including gibberish.
    /// In case of ambiguity, handlers specific to methods will be preferred.
    pub fn any<G, H, S, T>(&mut self, glob: G, handler: H, route_id: Option<Id>) -> &mut Router
        where G: Into<Glob<S, T>>,
              H: Handler,
              S: AsRef<[u8]>,
              T: GlobTypes,
    {
        let glob = glob.into();
        let types = glob.types().map(|types| types.store());
        let recognizer = Arc::new(
            Recognizer::new(glob.path(), Box::new(handler), types).unwrap()
        );

        if let Some(route_id) = route_id {
            self.route_id(route_id, glob.path(), recognizer.clone());
        }

        self.mut_inner().wildcard.push(recognizer);
        self
    }

    fn recognize(&self, method: &Method, path: &str) -> Option<RouteMatch> {
        self.inner.routers
            .get(method)
            .and_then(|recognizers| recognizers.recognize(path))
            .or(self.inner.wildcard.recognize(path))
    }

    fn handle_options(&self, path: &str) -> Response {
        static METHODS: &'static [Method] = &[
            Method::Get,
            Method::Post,
            Method::Put,
            Method::Delete,
            Method::Head,
            Method::Patch
        ];

        // Get all the available methods and return them.
        let mut options = vec![];

        for method in METHODS.iter() {
            self.inner.routers.get(method).map(|recognizers| {
                if let Some(_) = recognizers.recognize(path) {
                    options.push(method.clone());
                }
            });
        }
        // If GET is there, HEAD is also there.
        if options.contains(&Method::Get) && !options.contains(&Method::Head) {
            options.push(Method::Head);
        }

        let mut response = Response::new().with_status(StatusCode::Ok);
        response.headers.set(header::Allow(options));
        response
    }

    fn handle_method(&self, request: &mut Request) -> Option<FerrumResult<Response>> {
        if let Some(matched) = self.recognize(&request.method, request.uri.path()) {
            request.extensions.insert::<Router>(matched.params);
            request.extensions.insert::<RouterInner>(self.inner.clone());
            Some(matched.handler.handle(request))
        } else {
            None
        }
    }
}

impl Key for Router {
    type Value = Params;
}

impl Key for RouterInner {
    type Value = Arc<RouterInner>;
}

impl Handler for Router {
    fn handle(&self, request: &mut Request) -> FerrumResult<Response> {
        self.handle_method(request).unwrap_or_else(||
            match request.method {
                Method::Options => Ok(self.handle_options(request.uri.path())),
                // For HEAD, fall back to GET. Hyper ensures no response body is written.
                Method::Head => {
                    request.method = Method::Get;
                    self.handle_method(request).unwrap_or(
                        Err(
                            FerrumError::new(
                                NoRoute,
                                Some(Response::new()
                                    .with_status(StatusCode::NotFound))
                            )
                        )
                    )
                }
                _ => Err(
                    FerrumError::new(
                        NoRoute,
                        Some(Response::new()
                            .with_status(StatusCode::NotFound))
                    )
                )
            }
        )
    }
}

/// The error thrown by router if there is no matching route,
/// it is always accompanied by a NotFound response.
#[derive(Debug, PartialEq, Eq)]
pub struct NoRoute;

impl fmt::Display for NoRoute {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("No matching route found.")
    }
}

impl Error for NoRoute {
    fn description(&self) -> &str { "No Route" }
}

#[cfg(test)]
mod tests;