Fix path issues and add method validation

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 3 years ago
parent b45973e04c
commit 40ee976942
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -436,7 +436,7 @@ dependencies = [
[[package]] [[package]]
name = "multihook" name = "multihook"
version = "0.1.2" version = "0.1.3"
dependencies = [ dependencies = [
"chrono", "chrono",
"colored", "colored",

@ -4,7 +4,7 @@ description = "A webhook server"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
license = "GPL-3.0" license = "GPL-3.0"
readme = "README.md" readme = "README.md"
version = "0.1.2" version = "0.1.3"
edition = "2018" edition = "2018"
repository = "https://github.com/Trivernis/multihook.git" repository = "https://github.com/Trivernis/multihook.git"

@ -1,6 +1,6 @@
use crate::utils::error::MultihookResult; use crate::utils::error::MultihookResult;
use hyper::service::{make_service_fn, service_fn}; use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server}; use hyper::{Body, Method, Request, Response, Server, StatusCode};
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::Infallible; use std::convert::Infallible;
use std::future::Future; use std::future::Future;
@ -9,6 +9,7 @@ use std::pin::Pin;
use std::sync::Arc; use std::sync::Arc;
pub struct HTTPCallback<T1, T2> { pub struct HTTPCallback<T1, T2> {
methods: Vec<Method>,
inner: Arc< inner: Arc<
dyn Fn( dyn Fn(
Request<T1>, Request<T1>,
@ -32,10 +33,27 @@ impl HTTPCallback<Body, Body> {
{ {
Self { Self {
inner: Arc::new(cb), inner: Arc::new(cb),
methods: Vec::new(),
} }
} }
pub fn allow_method(mut self, method: Method) -> Self {
self.methods.push(method);
self
}
pub fn validate_method(&self, method: &Method) -> bool {
self.methods.contains(method)
}
pub async fn execute(&self, req: Request<Body>) -> MultihookResult<Response<Body>> { pub async fn execute(&self, req: Request<Body>) -> MultihookResult<Response<Body>> {
if !self.validate_method(req.method()) {
return Ok(Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
.body(Body::from("Method not allowed"))
.unwrap());
}
self.inner.as_ref()(req).await self.inner.as_ref()(req).await
} }
} }
@ -51,7 +69,7 @@ impl HTTPServer {
} }
async fn execute_callback(&self, req: Request<Body>) -> Result<Response<Body>, Infallible> { async fn execute_callback(&self, req: Request<Body>) -> Result<Response<Body>, Infallible> {
let path = req.uri().path(); let path = &req.uri().path()[1..];
let response = if let Some(cb) = self.routes.get(path) { let response = if let Some(cb) = self.routes.get(path) {
match cb.as_ref().execute(req).await { match cb.as_ref().execute(req).await {
Ok(res) => res, Ok(res) => res,
@ -62,8 +80,8 @@ impl HTTPServer {
} }
} else { } else {
Response::builder() Response::builder()
.status(404) .status(StatusCode::NOT_FOUND)
.body(Body::from("Unknown endpoint")) .body(Body::from("404 - Not Found"))
.unwrap() .unwrap()
}; };

@ -4,7 +4,7 @@ use action::HookAction;
use crate::server::http::{HTTPCallback, HTTPServer}; use crate::server::http::{HTTPCallback, HTTPServer};
use crate::utils::error::MultihookResult; use crate::utils::error::MultihookResult;
use hyper::{Body, Response}; use hyper::{Body, Method, Response};
pub mod action; pub mod action;
pub mod command_template; pub mod command_template;
@ -39,7 +39,8 @@ impl HookServer {
)))) ))))
}) })
} }
}); })
.allow_method(Method::POST);
self.server.add_callback(point, cb); self.server.add_callback(point, cb);
} }

Loading…
Cancel
Save