77 lines
2.2 KiB
Rust
77 lines
2.2 KiB
Rust
// Copyright (C) 2023-2099 The crate authors.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
// under the terms of the GNU Affero General Public License as published by the
|
|
// Free Software Foundation, either version 3 of the License, or (at your
|
|
// option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
// for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
use crate::error::Error;
|
|
use crate::webhook::WebHook;
|
|
|
|
use std::convert::Infallible;
|
|
use std::str::from_utf8;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use hyper::{body, header, Body, Method, Request, Response};
|
|
use log::{debug, error};
|
|
use tokio::sync::mpsc::UnboundedSender;
|
|
|
|
fn error_res<E: std::fmt::Debug>(e: E) -> Result<Response<Body>, Infallible> {
|
|
error!("error response: {:?}", e);
|
|
|
|
let text = format!("{:?}", e);
|
|
let res = Response::builder()
|
|
.status(400)
|
|
.body(Body::from(Vec::from(text.as_bytes())))
|
|
.unwrap();
|
|
Ok(res)
|
|
}
|
|
|
|
async fn webhooks_inner(req: Request<Body>) -> Result<WebHook, Error> {
|
|
match req.method() {
|
|
&Method::POST => (),
|
|
_ => return Err(Error::MethodMismatch),
|
|
}
|
|
|
|
debug!("Headers: {:?}", req.headers());
|
|
|
|
let headers = req.headers();
|
|
if let Some(content_type) = headers.get(header::CONTENT_TYPE) &&
|
|
let Some(token) = headers.get("X-Gitlab-Token") {
|
|
if content_type != "application/json" {
|
|
return Err(Error::InvalidContentType);
|
|
}
|
|
|
|
if token != "secret" {
|
|
return Err(Error::InvalidToken);
|
|
}
|
|
}
|
|
|
|
let tmp = body::to_bytes(req.into_body()).await?;
|
|
let text: &str = from_utf8(&tmp)?;
|
|
Ok(serde_json::from_str(text)?)
|
|
}
|
|
|
|
pub async fn webhooks(
|
|
req: Request<Body>,
|
|
value_tx: Arc<Mutex<UnboundedSender<WebHook>>>,
|
|
) -> Result<Response<Body>, Infallible> {
|
|
match webhooks_inner(req).await {
|
|
Ok(wh) => {
|
|
debug!("Passed: {:?}", wh);
|
|
|
|
value_tx.lock().unwrap().send(wh).unwrap();
|
|
|
|
Ok(Response::new("Hello world".into()))
|
|
}
|
|
Err(err) => error_res(err),
|
|
}
|
|
}
|