use std::net::IpAddr;
use bytes::Bytes;
use http_body_util::Full;
use hyper::body::Incoming;
use hyper::{Method, Request, Response, StatusCode};
use crate::server::handlers::{
empty_response, handle_cancel, handle_download, handle_info, handle_prepare_download,
handle_prepare_upload, handle_register, handle_upload, handle_web_portal, ServerState,
};
#[derive(Clone, Debug)]
pub struct ClientConnectionInfo {
pub remote_ip: IpAddr,
pub cert_fingerprint: Option<String>,
}
pub async fn route_request(
req: Request<Incoming>,
client: ClientConnectionInfo,
state: &ServerState,
) -> Result<Response<Full<Bytes>>, hyper::Error> {
let method = req.method();
let path = req.uri().path();
let response = match (method, path) {
(&Method::GET, "/") | (&Method::GET, "/index.html") => {
handle_web_portal(&req, state).await
}
(&Method::GET, "/api/localsend/v2/info") | (&Method::GET, "/api/localsend/v1/info") => {
handle_info(state).await
}
(&Method::POST, "/api/localsend/v2/register") => {
handle_register(req, client.remote_ip, client.cert_fingerprint, state).await
}
(&Method::POST, "/api/localsend/v2/prepare-upload") => {
handle_prepare_upload(req, client.remote_ip, client.cert_fingerprint, state).await
}
(&Method::POST, "/api/localsend/v2/upload") => {
handle_upload(req, client.remote_ip, state).await
}
(&Method::POST, "/api/localsend/v2/prepare-download") => {
handle_prepare_download(req, client.remote_ip, state).await
}
(&Method::GET, "/api/localsend/v2/download") | (&Method::GET, "/download") => {
handle_download(req, client.remote_ip, state).await
}
(&Method::POST, "/api/localsend/v2/cancel") => {
handle_cancel(req, client.remote_ip, state).await
}
_ => empty_response(StatusCode::NOT_FOUND),
};
Ok(response)
}