2022-04-06 14:55:39 +00:00
|
|
|
use anyhow::Error;
|
2019-05-08 15:36:19 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2021-01-25 13:42:49 +00:00
|
|
|
use std::pin::Pin;
|
2019-05-08 15:36:19 +00:00
|
|
|
use std::sync::Arc;
|
2019-08-23 12:11:14 +00:00
|
|
|
use std::task::{Context, Poll};
|
2019-05-08 15:36:19 +00:00
|
|
|
|
|
|
|
use futures::*;
|
|
|
|
use hyper::{Body, Request, Response, StatusCode};
|
|
|
|
|
2021-10-08 09:19:37 +00:00
|
|
|
use proxmox_router::http_err;
|
2022-04-06 14:55:39 +00:00
|
|
|
use proxmox_router::{ApiResponseFuture, HttpError, Router, RpcEnvironment};
|
2019-11-21 13:14:54 +00:00
|
|
|
|
2021-09-23 10:38:09 +00:00
|
|
|
use crate::formatter::*;
|
2022-04-06 14:55:39 +00:00
|
|
|
use crate::{normalize_uri_path, WorkerTask};
|
2021-09-21 05:58:43 +00:00
|
|
|
|
2019-06-26 15:29:12 +00:00
|
|
|
/// Hyper Service implementation to handle stateful H2 connections.
|
|
|
|
///
|
|
|
|
/// We use this kind of service to handle backup protocol
|
|
|
|
/// connections. State is stored inside the generic ``rpcenv``. Logs
|
|
|
|
/// goes into the ``WorkerTask`` log.
|
|
|
|
pub struct H2Service<E> {
|
|
|
|
router: &'static Router,
|
|
|
|
rpcenv: E,
|
2019-05-08 15:36:19 +00:00
|
|
|
worker: Arc<WorkerTask>,
|
2019-05-29 07:35:21 +00:00
|
|
|
debug: bool,
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 14:55:39 +00:00
|
|
|
impl<E: RpcEnvironment + Clone> H2Service<E> {
|
2019-06-26 15:29:12 +00:00
|
|
|
pub fn new(rpcenv: E, worker: Arc<WorkerTask>, router: &'static Router, debug: bool) -> Self {
|
2022-04-06 14:55:39 +00:00
|
|
|
Self {
|
|
|
|
rpcenv,
|
|
|
|
worker,
|
|
|
|
router,
|
|
|
|
debug,
|
|
|
|
}
|
2019-05-29 07:35:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug<S: AsRef<str>>(&self, msg: S) {
|
2022-04-06 14:55:39 +00:00
|
|
|
if self.debug {
|
|
|
|
self.worker.log_message(msg);
|
|
|
|
}
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 08:59:45 +00:00
|
|
|
fn handle_request(&self, req: Request<Body>) -> ApiResponseFuture {
|
2019-05-08 15:36:19 +00:00
|
|
|
let (parts, body) = req.into_parts();
|
|
|
|
|
|
|
|
let method = parts.method.clone();
|
|
|
|
|
2021-09-21 05:58:45 +00:00
|
|
|
let (path, components) = match normalize_uri_path(parts.uri.path()) {
|
2022-04-06 14:55:39 +00:00
|
|
|
Ok((p, c)) => (p, c),
|
2020-07-29 07:38:11 +00:00
|
|
|
Err(err) => return future::err(http_err!(BAD_REQUEST, "{}", err)).boxed(),
|
2019-05-08 15:36:19 +00:00
|
|
|
};
|
|
|
|
|
2019-05-29 08:17:38 +00:00
|
|
|
self.debug(format!("{} {}", method, path));
|
2019-05-08 15:36:19 +00:00
|
|
|
|
|
|
|
let mut uri_param = HashMap::new();
|
|
|
|
|
2021-09-27 10:59:06 +00:00
|
|
|
let formatter = JSON_FORMATTER;
|
2019-06-26 15:29:12 +00:00
|
|
|
|
|
|
|
match self.router.find_method(&components, method, &mut uri_param) {
|
2019-11-21 08:36:41 +00:00
|
|
|
None => {
|
2020-07-29 07:38:11 +00:00
|
|
|
let err = http_err!(NOT_FOUND, "Path '{}' not found.", path);
|
2021-09-27 10:59:06 +00:00
|
|
|
future::ok(formatter.format_error(err)).boxed()
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
2022-04-06 14:55:39 +00:00
|
|
|
Some(api_method) => crate::rest::handle_api_request(
|
|
|
|
self.rpcenv.clone(),
|
|
|
|
api_method,
|
|
|
|
formatter,
|
|
|
|
parts,
|
|
|
|
body,
|
|
|
|
uri_param,
|
|
|
|
)
|
|
|
|
.boxed(),
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 14:55:39 +00:00
|
|
|
fn log_response(
|
|
|
|
worker: Arc<WorkerTask>,
|
|
|
|
method: hyper::Method,
|
|
|
|
path: &str,
|
|
|
|
resp: &Response<Body>,
|
|
|
|
) {
|
2019-05-08 15:36:19 +00:00
|
|
|
let status = resp.status();
|
|
|
|
|
|
|
|
if !status.is_success() {
|
|
|
|
let reason = status.canonical_reason().unwrap_or("unknown reason");
|
|
|
|
|
|
|
|
let mut message = "request failed";
|
|
|
|
if let Some(data) = resp.extensions().get::<ErrorMessageExtension>() {
|
|
|
|
message = &data.0;
|
|
|
|
}
|
|
|
|
|
2021-09-24 07:30:00 +00:00
|
|
|
worker.log_message(format!(
|
|
|
|
"{} {}: {} {}: {}",
|
|
|
|
method.as_str(),
|
|
|
|
path,
|
|
|
|
status.as_str(),
|
|
|
|
reason,
|
|
|
|
message
|
|
|
|
));
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 14:55:39 +00:00
|
|
|
impl<E: RpcEnvironment + Clone> tower_service::Service<Request<Body>> for H2Service<E> {
|
2019-08-23 12:11:14 +00:00
|
|
|
type Response = Response<Body>;
|
2019-05-30 06:10:06 +00:00
|
|
|
type Error = Error;
|
2021-01-25 13:42:49 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
2019-05-08 15:36:19 +00:00
|
|
|
|
2019-08-23 12:11:14 +00:00
|
|
|
fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
2019-05-08 15:36:19 +00:00
|
|
|
let path = req.uri().path().to_owned();
|
|
|
|
let method = req.method().clone();
|
|
|
|
let worker = self.worker.clone();
|
|
|
|
|
2021-01-15 13:38:27 +00:00
|
|
|
self.handle_request(req)
|
2019-08-23 12:11:14 +00:00
|
|
|
.map(move |result| match result {
|
2019-05-08 15:36:19 +00:00
|
|
|
Ok(res) => {
|
|
|
|
Self::log_response(worker, method, &path, &res);
|
2019-05-30 06:10:06 +00:00
|
|
|
Ok::<_, Error>(res)
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2022-04-06 14:55:39 +00:00
|
|
|
if let Some(apierr) = err.downcast_ref::<HttpError>() {
|
2019-05-08 15:36:19 +00:00
|
|
|
let mut resp = Response::new(Body::from(apierr.message.clone()));
|
2022-04-06 14:55:39 +00:00
|
|
|
resp.extensions_mut()
|
|
|
|
.insert(ErrorMessageExtension(apierr.message.clone()));
|
2019-05-08 15:36:19 +00:00
|
|
|
*resp.status_mut() = apierr.code;
|
|
|
|
Self::log_response(worker, method, &path, &resp);
|
|
|
|
Ok(resp)
|
|
|
|
} else {
|
|
|
|
let mut resp = Response::new(Body::from(err.to_string()));
|
2022-04-06 14:55:39 +00:00
|
|
|
resp.extensions_mut()
|
|
|
|
.insert(ErrorMessageExtension(err.to_string()));
|
2019-05-08 15:36:19 +00:00
|
|
|
*resp.status_mut() = StatusCode::BAD_REQUEST;
|
|
|
|
Self::log_response(worker, method, &path, &resp);
|
|
|
|
Ok(resp)
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 12:11:14 +00:00
|
|
|
})
|
|
|
|
.boxed()
|
2019-05-08 15:36:19 +00:00
|
|
|
}
|
|
|
|
}
|