rest-server: make handle_request a method of ApiConfig

This is what actually defines the API server after all.
The ApiService trait in between is a hyper impl detail.

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2023-01-24 11:26:41 +01:00
parent 5fe0777318
commit 93c027f5cc

View File

@ -241,7 +241,7 @@ impl Service<Request<Body>> for ApiService {
None => self.peer,
};
async move {
let response = match handle_request(Arc::clone(&config), req, &peer).await {
let response = match Arc::clone(&config).handle_request(req, &peer).await {
Ok(response) => response,
Err(err) => {
let (err, code) = match err.downcast_ref::<HttpError>() {
@ -601,8 +601,9 @@ fn extract_compression_method(headers: &http::HeaderMap) -> Option<CompressionMe
None
}
async fn handle_request(
api: Arc<ApiConfig>,
impl ApiConfig {
pub async fn handle_request(
self: Arc<ApiConfig>,
req: Request<Body>,
peer: &std::net::SocketAddr,
) -> Result<Response<Body>, Error> {
@ -620,13 +621,14 @@ async fn handle_request(
.unwrap());
}
let env_type = api.env_type();
let mut rpcenv = RestEnvironment::new(env_type, Arc::clone(&api));
let env_type = self.env_type();
let mut rpcenv = RestEnvironment::new(env_type, Arc::clone(&self));
rpcenv.set_client_ip(Some(*peer));
let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000);
let access_forbidden_time = std::time::Instant::now() + std::time::Duration::from_millis(500);
let access_forbidden_time =
std::time::Instant::now() + std::time::Duration::from_millis(500);
if comp_len >= 1 && components[0] == "api2" {
if comp_len >= 2 {
@ -639,7 +641,7 @@ async fn handle_request(
};
let mut uri_param = HashMap::new();
let api_method = api.find_method(&components[2..], method.clone(), &mut uri_param);
let api_method = self.find_method(&components[2..], method.clone(), &mut uri_param);
let mut auth_required = true;
if let Some(api_method) = api_method {
@ -652,7 +654,7 @@ async fn handle_request(
Box::new(EmptyUserInformation {});
if auth_required {
match api.check_auth(&parts.headers, &method).await {
match self.check_auth(&parts.headers, &method).await {
Ok((authid, info)) => {
rpcenv.set_auth_id(Some(authid));
user_info = info;
@ -691,14 +693,18 @@ async fn handle_request(
user_info.as_ref(),
) {
let err = http_err!(FORBIDDEN, "permission check failed");
tokio::time::sleep_until(Instant::from_std(access_forbidden_time)).await;
tokio::time::sleep_until(Instant::from_std(access_forbidden_time))
.await;
return Ok(formatter.format_error(err));
}
let result = if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
let result =
if api_method.protected && env_type == RpcEnvironmentType::PUBLIC {
proxy_protected_request(api_method, parts, body, peer).await
} else {
handle_api_request(rpcenv, api_method, formatter, parts, body, uri_param)
handle_api_request(
rpcenv, api_method, formatter, parts, body, uri_param,
)
.await
};
@ -725,19 +731,19 @@ async fn handle_request(
}
if comp_len == 0 {
match api.check_auth(&parts.headers, &method).await {
match self.check_auth(&parts.headers, &method).await {
Ok((auth_id, _user_info)) => {
rpcenv.set_auth_id(Some(auth_id));
return Ok(api.get_index(rpcenv, parts).await);
return Ok(self.get_index(rpcenv, parts).await);
}
Err(AuthError::Generic(_)) => {
tokio::time::sleep_until(Instant::from_std(delay_unauth_time)).await;
}
Err(AuthError::NoData) => {}
}
return Ok(api.get_index(rpcenv, parts).await);
return Ok(self.get_index(rpcenv, parts).await);
} else {
let filename = api.find_alias(&components);
let filename = self.find_alias(&components);
let compression = extract_compression_method(&parts.headers);
return handle_static_file_download(filename, compression).await;
}
@ -745,3 +751,4 @@ async fn handle_request(
Err(http_err!(NOT_FOUND, "Path '{}' not found.", path))
}
}