[][src]Struct httprouter::router::Router

pub struct Router<T> {
    pub trees: HashMap<Method, Node<T>>,
    pub save_matched_route_path: bool,
    pub redirect_trailing_slash: bool,
    pub redirect_fixed_path: bool,
    pub handle_method_not_allowed: bool,
    pub handle_options: bool,
    pub global_options: Option<T>,
    pub global_allowed: String,
    pub not_found: Option<T>,
    pub method_not_allowed: Option<T>,
}

Router is container which can be used to dispatch requests to different handler functions via configurable routes

Fields

trees: HashMap<Method, Node<T>>save_matched_route_path: bool

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.

redirect_trailing_slash: 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.

redirect_fixed_path: 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.

handle_method_not_allowed: 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.

handle_options: bool

If enabled, the router automatically replies to OPTIONS requests. Custom OPTIONS handlers take priority over automatic replies.

global_options: Option<T>

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.

global_allowed: String

Cached value of global (*) allowed methods

not_found: Option<T>

Configurable handler which is called when no matching route is found.

method_not_allowed: 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.

Implementations

impl<T: Handle> Router<T>[src]

pub fn get(&mut self, path: &str, handle: T)[src]

get is a shortcut for router.handle("Method::GET, path, handle)

pub fn head(&mut self, path: &str, handle: T)[src]

head is a shortcut for router.handle(Method::HEAD, path, handle)

pub fn options(&mut self, path: &str, handle: T)[src]

options is a shortcut for router.handle(Method::OPTIONS, path, handle)

pub fn post(&mut self, path: &str, handle: T)[src]

post is a shortcut for router.handle(Method::POST, path, handle)

pub fn put(&mut self, path: &str, handle: T)[src]

put is a shortcut for router.handle(Method::POST, path, handle)

pub fn patch(&mut self, path: &str, handle: T)[src]

patch is a shortcut for router.handle(Method::PATCH, path, handle)

pub fn delete(&mut self, path: &str, handle: T)[src]

delete is a shortcut for router.handle(Method::DELETE, path, handle)

pub fn handle(&mut self, method: Method, path: &str, handle: T)[src]

pub fn lookup(
    &mut self,
    method: &Method,
    path: &str
) -> Result<RouteLookup<'_, T>, bool>
[src]

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 serve_files()[src]

pub fn allowed(&self, path: &str, req_method: &Method) -> String[src]

pub fn serve_http(
    &self,
    req: Request<impl HttpBody>
) -> BoxFuture<'static, Result<Response<Body>, Box<dyn Error + Sync + Send>>>
[src]

Trait Implementations

impl<T: Handle> Default for Router<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Router<T> where
    T: RefUnwindSafe

impl<T> Send for Router<T> where
    T: Send

impl<T> Sync for Router<T> where
    T: Sync

impl<T> Unpin for Router<T> where
    T: Unpin

impl<T> UnwindSafe for Router<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.