Struct http_srv::request::handler::Handler

source ·
pub struct Handler { /* private fields */ }
Expand description

Handler

Matches requests by their method and url, and executes different handler functions for them.

§Example

use http_srv::request::handler::{self,*};
use http_srv::request::RequestMethod;

let mut handler = Handler::new();
handler.get("/", |req| {
    req.respond_buf(b"Hello world! :)")
});
handler.add_default(RequestMethod::GET, handler::cat_handler);
handler.post_interceptor(handler::log_stdout);

Implementations§

source§

impl Handler

source

pub fn new() -> Self

source

pub fn get(&mut self, url: &str, f: impl HandlerFunc)

Shortcut for add(RequestMethod::GET, …)

source

pub fn post(&mut self, url: &str, f: impl HandlerFunc)

Shortcut for add(RequestMethod::POST, …)

source

pub fn delete(&mut self, url: &str, f: impl HandlerFunc)

Shortcut for add(RequestMethod::DELETE, …)

source

pub fn head(&mut self, url: &str, f: impl HandlerFunc)

Shortcut for add(RequestMethod::HEAD, …)

source

pub fn add(&mut self, method: RequestMethod, url: &str, f: impl HandlerFunc)

Adds a handler for a request type

  • method: HTTP method to match
  • url: URL for the handler
  • f: Handler for the request
source

pub fn add_default(&mut self, method: RequestMethod, f: impl HandlerFunc)

Adds a default handler for all requests of a certain type

source

pub fn pre_interceptor(&mut self, f: impl Interceptor)

Add a function to run before the request is processed

source

pub fn post_interceptor(&mut self, f: impl Interceptor)

Add a function to run after the request is processed

source

pub fn get_handler( &self, method: &RequestMethod, url: &str ) -> Option<&impl HandlerFunc>

Get the handler for a certain method and url

source

pub fn handle(&self, req: &mut HttpRequest) -> Result<()>

Handles a request if it finds a HandlerFunc for it. Else, it returns a 403 FORBIDDEN response

Trait Implementations§

source§

impl Default for Handler

source§

fn default() -> Self

Default Handler

§Pre Interceptors
§Handler Functions
§Post Interceptors

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.