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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//! Httprouter is a trie based high performance HTTP request router.
//!
//! A trivial example is:
//!  ```ignore
//!  import (
//!      "fmt"
//!      "github.com/julienschmidt/httprouter"
//!      "net/http"
//!      "log"
//!  )
//!
//!  func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
//!      fmt.Fprint(w, "Welcome!\n")
//!  }
//!
//!  func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
//!      fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
//!  }
//!
//!  func main() {
//!      router := httprouter.New()
//!      router.GET("/", Index)
//!      router.GET("/hello/:name", Hello)
//!
//!      log.Fatal(http.ListenAndServe(":8080", router))
//!  }
//!  ```
//!
//! The router matches incoming requests by the request method and the path.
//! If a handle is registered for this path and method, the router delegates the
//! request to that function.
//! For the methods GET, POST, PUT, PATCH, DELETE and OPTIONS shortcut functions exist to
//! register handles, for all other methods router.Handle can be used.
//!
//! The registered path, against which the router matches incoming requests, can
//! contain two types of parameters:
//!  Syntax    Type
//!  :name     named parameter
//!  *name     catch-all parameter
//!
//! Named parameters are dynamic path segments. They match anything until the
//! next '/' or the path end:
//!  Path: /blog/:category/:post
//!
//!  Requests:
//!   /blog/rust/request-routers            match: category="rust", post="request-routers"
//!   /blog/rust/request-routers/           no match, but the router would redirect
//!   /blog/rust/                           no match
//!   /blog/rust/request-routers/comments   no match
//!
//! Catch-all parameters match anything until the path end, including the
//! directory index (the '/' before the catch-all). Since they match anything
//! until the end, catch-all parameters must always be the final path element.
//!  Path: /files/*filepath
//!
//!  Requests:
//!   /files/                             match: filepath="/"
//!   /files/LICENSE                      match: filepath="/LICENSE"
//!   /files/templates/article.html       match: filepath="/templates/article.html"
//!   /files                              no match, but the router would redirect
//!
//! The value of parameters is saved as a slice of the Param struct, consisting
//! each of a key and a value. The slice is passed to the Handle func as a third
//! parameter.
//! There are two ways to retrieve the value of a parameter:
//!  // by the name of the parameter
//!  let user = params.by_name("user") // defined by :user or *user
//!
//!  // by the index of the parameter. This way you can also get the name (key)
//!  thirdKey   := params[2].key   // the name of the 3rd parameter
//!  thirdValue := params[2].value // the value of the 3rd parameter
use crate::path::clean_path;
use crate::tree::{Node, Params, RouteLookup};
use futures::future::{ok, BoxFuture};
use hyper::body::HttpBody;
use hyper::http::{header, StatusCode};
use hyper::{Body, Method, Request, Response};
use std::collections::HashMap;
use std::str;

pub trait Handle {
  fn handle(
    &self,
    req: Request<impl HttpBody>,
    params: Params,
  ) -> BoxFuture<'static, Result<Response<Body>, Box<dyn std::error::Error + Sync + Send>>>;
}

/// Router is container which can be used to dispatch requests to different
/// handler functions via configurable routes
pub struct Router<T> {
  pub trees: HashMap<Method, Node<T>>,

  /// If enabled, adds the matched route path onto the http.Request context
  /// before invoking the handler.
  /// The matched route path is only added to handlers of routes that were
  /// registered when this option was enabled.
  pub save_matched_route_path: bool,

  /// Enables automatic redirection if the current route can't be matched but a
  /// handler for the path with (without) the trailing slash exists.
  /// For example if /foo/ is requested but a route only exists for /foo, the
  /// client is redirected to /foo with http status code 301 for GET requests
  /// and 307 for all other request methods.
  pub redirect_trailing_slash: bool,

  /// If enabled, the router tries to fix the current request path, if no
  /// handle is registered for it.
  /// First superfluous path elements like ../ or // are removed.
  /// Afterwards the router does a case-insensitive lookup of the cleaned path.
  /// If a handle can be found for this route, the router makes a redirection
  /// to the corrected path with status code 301 for GET requests and 307 for
  /// all other request methods.
  /// For example /FOO and /..//Foo could be redirected to /foo.
  /// RedirectTrailingSlash is independent of this option.
  pub redirect_fixed_path: bool,

  /// If enabled, the router checks if another method is allowed for the
  /// current route, if the current request can not be routed.
  /// If this is the case, the request is answered with 'Method Not Allowed'
  /// and HTTP status code 405.
  /// If no other Method is allowed, the request is delegated to the NotFound
  /// handler.
  pub handle_method_not_allowed: bool,

  /// If enabled, the router automatically replies to OPTIONS requests.
  /// Custom OPTIONS handlers take priority over automatic replies.
  pub handle_options: bool,

  /// An optional handler that is called on automatic OPTIONS requests.
  /// The handler is only called if HandleOPTIONS is true and no OPTIONS
  /// handler for the specific path was set.
  /// The "Allowed" header is set before calling the handler.
  pub global_options: Option<T>,

  /// Cached value of global (*) allowed methods
  pub global_allowed: String,

  /// Configurable handler which is called when no matching route is
  /// found.
  pub not_found: Option<T>,

  /// Configurable handler which is called when a request
  /// cannot be routed and HandleMethodNotAllowed is true.
  /// The "Allow" header with allowed request methods is set before the handler
  /// is called.
  pub method_not_allowed: Option<T>,
}

impl<T: Handle> Default for Router<T> {
  fn default() -> Self {
    Router::<T> {
      trees: HashMap::new(),
      redirect_trailing_slash: true,
      redirect_fixed_path: true,
      handle_method_not_allowed: true,
      handle_options: true,
      global_allowed: String::new(),
      global_options: None,
      method_not_allowed: None,
      not_found: None,
      save_matched_route_path: false,
    }
  }
}

impl<T: Handle> Router<T> {
  /// get is a shortcut for router.handle("Method::GET, path, handle)
  pub fn get(&mut self, path: &str, handle: T) {
    self.handle(Method::GET, path, handle);
  }

  /// head is a shortcut for router.handle(Method::HEAD, path, handle)
  pub fn head(&mut self, path: &str, handle: T) {
    self.handle(Method::HEAD, path, handle);
  }

  /// options is a shortcut for router.handle(Method::OPTIONS, path, handle)
  pub fn options(&mut self, path: &str, handle: T) {
    self.handle(Method::OPTIONS, path, handle);
  }

  /// post is a shortcut for router.handle(Method::POST, path, handle)
  pub fn post(&mut self, path: &str, handle: T) {
    self.handle(Method::POST, path, handle);
  }

  /// put is a shortcut for router.handle(Method::POST, path, handle)
  pub fn put(&mut self, path: &str, handle: T) {
    self.handle(Method::PUT, path, handle);
  }

  /// patch is a shortcut for router.handle(Method::PATCH, path, handle)
  pub fn patch(&mut self, path: &str, handle: T) {
    self.handle(Method::PATCH, path, handle);
  }

  /// delete is a shortcut for router.handle(Method::DELETE, path, handle)
  pub fn delete(&mut self, path: &str, handle: T) {
    self.handle(Method::DELETE, path, handle);
  }

  // Handle registers a new request handle with the given path and method.
  //
  // For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
  // functions can be used.
  //
  // This function is intended for bulk loading and to allow the usage of less
  // frequently used, non-standardized or custom methods (e.g. for internal
  // communication with a proxy).
  pub fn handle(&mut self, method: Method, path: &str, handle: T) {
    if !path.starts_with('/') {
      panic!("path must begin with '/' in path '{}'", path);
    }

    if self.save_matched_route_path {
      // TODO
      // handle = r.saveMatchedRoutePath(path, handle)
    }

    self
      .trees
      .entry(method)
      .or_insert_with(Node::default)
      .add_route(path, handle);
  }

  /// Lookup allows the manual lookup of a method + path combo.
  /// This is e.g. useful to build a framework around this router.
  /// If the path was found, it returns the handle function and the path parameter
  /// values. Otherwise the third return value indicates whether a redirection to
  /// the same path with an extra / without the trailing slash should be performed.
  pub fn lookup(&mut self, method: &Method, path: &str) -> Result<RouteLookup<T>, bool> {
    self
      .trees
      .get_mut(method)
      .map(|n| n.get_value(path))
      .unwrap_or(Err(false))
  }

  // TODO
  pub fn serve_files() {
    unimplemented!()
  }

  // returns a list of the allowed methods for a specific path
  // eg: 'GET, PATCH, OPTIONS'
  pub fn allowed(&self, path: &str, req_method: &Method) -> String {
    let mut allowed: Vec<String> = Vec::new();
    match path {
      "*" => {
        for method in self.trees.keys() {
          if method != "OPTIONS" {
            allowed.push(method.to_string());
          }
        }
      }
      _ => {
        for method in self.trees.keys() {
          if method == req_method || method == "OPTIONS" {
            continue;
          }

          if let Some(tree) = self.trees.get(method) {
            let handler = tree.get_value(path);

            if handler.is_ok() {
              allowed.push(method.to_string());
            }
          };
        }
      }
    };

    if !allowed.is_empty() {
      allowed.push(String::from("OPTIONS"))
    }

    allowed.join(", ")
  }

  pub fn serve_http(
    &self,
    req: Request<impl HttpBody>,
  ) -> BoxFuture<'static, Result<Response<Body>, Box<dyn std::error::Error + Sync + Send>>> {
    let root = self.trees.get(req.method());
    let path = req.uri().path();
    if let Some(root) = root {
      match root.get_value(path) {
        Ok(lookup) => {
          return lookup.value.handle(req, lookup.params);
        }
        Err(tsr) => {
          if req.method() != Method::CONNECT && path != "/" {
            let code = match *req.method() {
              // Moved Permanently, request with GET method
              Method::GET => StatusCode::MOVED_PERMANENTLY,
              // Permanent Redirect, request with same method
              _ => StatusCode::PERMANENT_REDIRECT,
            };

            if tsr && self.redirect_trailing_slash {
              let path = if path.len() > 1 && path.ends_with('/') {
                path[..path.len() - 1].to_string()
              } else {
                path.to_string() + "/"
              };

              return Box::pin(ok(
                Response::builder()
                  .header(header::LOCATION, path.as_str())
                  .status(code)
                  .body(Body::empty())
                  .unwrap(),
              ));
            };

            if self.redirect_fixed_path {
              if let Some(fixed_path) = root.find_case_insensitive_path(
                &clean_path(req.uri().path()),
                self.redirect_trailing_slash,
              ) {
                return Box::pin(ok(
                  Response::builder()
                    .header(header::LOCATION, fixed_path.as_str())
                    .status(code)
                    .body(Body::empty())
                    .unwrap(),
                ));
              }
            };
          };
        }
      }
    };

    if req.method() == Method::OPTIONS && self.handle_options {
      let allow = self.allowed(path, &Method::OPTIONS);
      if allow != "" {
        match &self.global_options {
          Some(handler) => return handler.handle(req, Params::default()),
          None => {
            return Box::pin(ok(
              Response::builder()
                .header(header::ALLOW, allow)
                .body(Body::empty())
                .unwrap(),
            ));
          }
        };
      }
    } else if self.handle_method_not_allowed {
      let allow = self.allowed(path, req.method());

      if !allow.is_empty() {
        if let Some(ref method_not_allowed) = self.method_not_allowed {
          return method_not_allowed.handle(req, Params::default());
        }
        return Box::pin(ok(
          Response::builder()
            .header(header::ALLOW, allow)
            .status(StatusCode::METHOD_NOT_ALLOWED)
            .body(Body::empty())
            .unwrap(),
        ));
      }
    };

    match &self.not_found {
      Some(handler) => handler.handle(req, Params::default()),
      None => Box::pin(ok(
        Response::builder().status(404).body(Body::empty()).unwrap(),
      )),
    }
  }
}