1use std::pin::Pin;
2
3use crate::reqres::{HttpRequest, HttpMethod, StatusCode};
4use crate::core::{HttpResult, HttpRead};
5
6pub trait HttpService: Send + Sync + 'static {
10 fn request(&self, route: &str, req: &HttpRequest, body: &mut dyn HttpRead) -> impl Future<Output = HttpResult> + Send;
18
19 fn filter(&self, route: &str, req: &HttpRequest) -> HttpResult<()> {
23 if route != "/" { return Err(StatusCode::NOT_FOUND.into()); }
24 if req.method != HttpMethod::Get { return Err(StatusCode::METHOD_NOT_ALLOWED.into()); }
25 if req.len > 0 { return Err(StatusCode::REQUEST_ENTITY_TOO_LARGE.into()); }
26 Ok(())
27 }
28}
29
30pub trait HttpServiceRaw: Send + Sync + 'static {
36 fn request_raw<'a>(&'a self, route: &'a str, req: &'a HttpRequest, body: &'a mut dyn HttpRead) -> Pin<Box<dyn Future<Output = HttpResult> + Send + 'a>>;
38 fn filter_raw(&self, route: &str, req: &HttpRequest) -> HttpResult<()>;
40}
41
42impl<T: HttpService> HttpServiceRaw for T {
43 fn request_raw<'a>(&'a self, route: &'a str, req: &'a HttpRequest, body: &'a mut dyn HttpRead) -> Pin<Box<dyn Future<Output = HttpResult> + Send + 'a>> {
44 Box::pin(self.request(route, req, body))
45 }
46
47 fn filter_raw(&self, route: &str, req: &HttpRequest) -> HttpResult<()> {
48 self.filter(route, req)
49 }
50}