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
#![doc(html_root_url = "https://marad.github.io/hyper-router/doc/hyper_router")]

//! # Hyper Router
//!
//! This cargo is a small extension to the great Hyper HTTP library. It basically is
//! adds the ability to define routes to request handlers and then query for the handlers
//! by request path.
//!
//! ## Usage
//!
//! To use the library just add: 
//! 
//! ```
//! hyper-router = "*"
//! ```
//!
//! to your dependencies.
//!
//! ```rust
//! extern crate hyper;
//! extern crate hyper_router;
//!
//! use hyper::server::{Server, Request, Response};
//! use hyper::method::Method::Get;
//! use hyper_router::{Route, RouterBuilder, Path};
//!
//! fn basic_handler(_: Request, res: Response) {
//!   res.send(b"Hello World!").unwrap();
//! }
//!
//! fn main() {
//!   let router = RouterBuilder::new()
//!     .add(Route {
//!       method: Get,
//!       path: Path::new("/greet"),
//!       handler: basic_handler
//!     })
//!     .build();
//!
//!   Server::http("0.0.0.0:8080").unwrap()
//!     .handle(move |request: Request, response: Response| {
//!       let handler = router.find_handler(&request);
//!       handler(request, response);
//!     }).unwrap();
//! }
//! ```
//!
//! This code will start Hyper server and add use router to find handlers for request.
//! We create the `Route` so that when we visit path `/greet` the `basic_handler` handler
//! will be called.
//!
//! ## Things to note
//!
//! * `Path::new` method accepts regular expressions so you can match every path you please.
//! * If you have request matching multiple paths the one that was first `add`ed will be chosen.
//! * This library is in an early stage of development so there may be breaking changes comming
//! (but I'll try as hard as I can not to break backwards compatibility or break it just a little - 
//! I promise I'll try!).
//!
//! # Further Development
//!
//! * add the ability to distinguish requests by query parameters.
//! * maybe some small API changes/upgrades
//! * thinking about returning `Result<Handler, StatusCode>` in case of any error in
//! `find_handler` (and will probably go with it). This will actually enable users to server their
//! own error pages.
//!
//! # Waiting for your feedback
//!
//! I've created this little tool to help myself learn Rust and to avoid using big frameworks
//! like Iron or rustful. I just want to keep things simple.
//!
//! Obviously I could make some errors or bad design choices so I'm waiting for your feedback!
//! You may create an issue at [project's bug tracker](https://github.com/marad/hyper-router/issues).

extern crate hyper;
use hyper::uri::RequestUri::AbsolutePath;
use hyper::server::{Request, Response};

mod path;
mod route;
mod builder;
pub mod handlers;

pub use self::path::Path;
pub use self::route::Route;
pub use self::builder::RouterBuilder;

pub type Handler = fn(Request, Response);

/// This is the one. The router.
///
/// Example usage:
///
/// ```rust
/// let router = RouterBuilder::new()
///     .add(Route {
///         method: Get,
///         path: Path::new(r"/person/\d+"),
///         handler: some_handler
///     })
///     .build();
///
///  // later when processing request:
///  let handler = router.find_handler(&request);
///  handler(request, response);
/// ```
#[derive(Debug)]
pub struct Router {
    routes: Vec<Route>
}

impl Router {
    /// Finds handler for given Hyper request.
    ///
    /// If the request does not match any route than default 404 handler is returned.
    /// If the request match some routes but http method does not match (used GET but routes are
    /// defined for POST) than default method not supported handler is returned.
    /// Finally in case of error - internal server error handler is returned.
    pub fn find_handler(&self, request: &Request) -> Handler {
        if let AbsolutePath(request_path) = request.uri.clone() {
            let matching_routes = self.find_matching_routes(&request_path);
            match matching_routes.len() {
                x if x <= 0 => handlers::default_404_handler,
                1 => {
                    let method = request.method.clone();
                    matching_routes.iter()
                        .find(|route| route.method == method)
                        .map(|route| route.handler)
                        .unwrap_or(handlers::method_not_supported_handler)
                }
                _ => handlers::internal_server_error_handler
            }
        } else {
            handlers::not_implemented_handler
        }
    }

    /// Returns vector of `Route`s that match to given path.
    pub fn find_matching_routes(&self, request_path: &str) -> Vec<&Route> {
        self.routes.iter()
            .filter(|route| {
                route.path.matcher.is_match(&request_path)
            })
            .collect()
    }
}