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::handler::{self,*};
use http::request::HttpRequest;
use http::HttpMethod;
let mut handler = Handler::new();
handler.get("/", |req: &mut HttpRequest| {
req.respond_str("Hello world! :)")
});
handler.add_default(HttpMethod::GET, handler::cat_handler);
handler.post_interceptor(handler::log_stdout);
Implementations§
Source§impl Handler
impl Handler
pub fn new() -> Self
Sourcepub fn get(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
pub fn get(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
Shortcut for add(HttpMethod::GET
, …)
Sourcepub fn post(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
pub fn post(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
Shortcut for add(HttpMethod::POST
, …)
Sourcepub fn delete(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
pub fn delete(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
Shortcut for add(HttpMethod::DELETE
, …)
Sourcepub fn head(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
pub fn head(&mut self, url: impl Into<UrlMatcher>, f: impl RequestHandler)
Shortcut for add(HttpMethod::HEAD
, …)
Sourcepub fn add(
&mut self,
method: HttpMethod,
url: impl Into<UrlMatcher>,
f: impl RequestHandler,
)
pub fn add( &mut self, method: HttpMethod, url: impl Into<UrlMatcher>, f: impl RequestHandler, )
Sourcepub fn add_default(&mut self, method: HttpMethod, f: impl RequestHandler)
pub fn add_default(&mut self, method: HttpMethod, f: impl RequestHandler)
Sourcepub fn pre_interceptor(&mut self, f: impl Interceptor)
pub fn pre_interceptor(&mut self, f: impl Interceptor)
Add a function to run before the request is processed
Sourcepub fn post_interceptor(&mut self, f: impl Interceptor)
pub fn post_interceptor(&mut self, f: impl Interceptor)
Add a function to run after the request is processed
Sourcepub fn get_handler(
&self,
method: &HttpMethod,
url: &str,
) -> Option<&dyn RequestHandler>
pub fn get_handler( &self, method: &HttpMethod, url: &str, ) -> Option<&dyn RequestHandler>
Get the handler for a certain method and url
Sourcepub fn handle(&self, req: &mut HttpRequest) -> Result<()>
pub fn handle(&self, req: &mut HttpRequest) -> Result<()>
Handles a request if it finds a RequestHandler
for it.
Else, it returns a 403 FORBIDDEN response
Trait Implementations§
Source§impl Default for Handler
impl Default for Handler
Source§fn default() -> Self
fn default() -> Self
Default Handler
§Pre Interceptors
suffix_html
- Set Header: “Accept-Ranges: bytes”
§Handler Functions
-
GET “/”:
root_handler
-
HEAD “/”:
root_handler
§Post Interceptors
Auto Trait Implementations§
impl Freeze for Handler
impl !RefUnwindSafe for Handler
impl Send for Handler
impl Sync for Handler
impl Unpin for Handler
impl !UnwindSafe for Handler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more