From 2c090170453bcef3a4c39329b612fd38faa22526 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 4 Oct 2021 14:49:25 +0200 Subject: [PATCH] proxmox-rest-server: pass owned RestEnvironment to get_index This way we avoid pointers with lifetimes. --- .../examples/minimal-rest-server.rs | 6 ++---- proxmox-rest-server/src/api_config.rs | 9 ++++----- proxmox-rest-server/src/rest.rs | 15 ++++----------- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/proxmox-rest-server/examples/minimal-rest-server.rs b/proxmox-rest-server/examples/minimal-rest-server.rs index 1d6a8a2b..b1ef9335 100644 --- a/proxmox-rest-server/examples/minimal-rest-server.rs +++ b/proxmox-rest-server/examples/minimal-rest-server.rs @@ -8,7 +8,7 @@ use lazy_static::lazy_static; use proxmox::api::{api, router::SubdirMap, Router, RpcEnvironmentType, UserInformation}; use proxmox::list_subdirs_api_method; -use proxmox_rest_server::{ApiAuth, ApiConfig, AuthError, RestServer}; +use proxmox_rest_server::{ApiAuth, ApiConfig, AuthError, RestServer, RestEnvironment}; // Create a Dummy User info and auth system // Normally this would check and authenticate the user struct DummyUserInfo; @@ -46,9 +46,7 @@ impl ApiAuth for DummyAuth { // iow. what the user browses to fn get_index<'a>( - _auth_id: Option, - _language: Option, - _api: &'a ApiConfig, + _env: RestEnvironment, _parts: http::request::Parts, ) -> Pin> + Send + 'a>> { Box::pin(async move { diff --git a/proxmox-rest-server/src/api_config.rs b/proxmox-rest-server/src/api_config.rs index 4ff1df5f..c7c71ec0 100644 --- a/proxmox-rest-server/src/api_config.rs +++ b/proxmox-rest-server/src/api_config.rs @@ -16,9 +16,9 @@ use serde::Serialize; use proxmox::api::{ApiMethod, Router, RpcEnvironmentType, UserInformation}; use proxmox::tools::fs::{create_path, CreateOptions}; -use crate::{ApiAuth, AuthError, FileLogger, FileLogOptions, CommandSocket}; +use crate::{ApiAuth, AuthError, FileLogger, FileLogOptions, CommandSocket, RestEnvironment}; -pub type GetIndexFn = &'static (dyn for<'a> Fn(Option, Option, &'a ApiConfig, Parts) -> Pin> + Send + 'a>> + Send + Sync); +pub type GetIndexFn = &'static (dyn Fn(RestEnvironment, Parts) -> Pin> + Send>> + Send + Sync); /// REST server configuration pub struct ApiConfig { @@ -72,11 +72,10 @@ impl ApiConfig { pub(crate) async fn get_index( &self, - auth_id: Option, - language: Option, + rest_env: RestEnvironment, parts: Parts, ) -> Response { - (self.get_index_fn)(auth_id, language, self, parts).await + (self.get_index_fn)(rest_env, parts).await } pub(crate) async fn check_auth( diff --git a/proxmox-rest-server/src/rest.rs b/proxmox-rest-server/src/rest.rs index d0a748c8..063b797f 100644 --- a/proxmox-rest-server/src/rest.rs +++ b/proxmox-rest-server/src/rest.rs @@ -36,7 +36,7 @@ use pbs_tools::stream::AsyncReaderStream; use crate::{ ApiConfig, FileLogger, AuthError, RestEnvironment, CompressionMethod, - extract_cookie, normalize_uri_path, formatter::*, + normalize_uri_path, formatter::*, }; extern "C" { @@ -587,13 +587,6 @@ async fn handle_static_file_download( } } -fn extract_lang_header(headers: &http::HeaderMap) -> Option { - if let Some(Ok(cookie)) = headers.get("COOKIE").map(|v| v.to_str()) { - return extract_cookie(cookie, "PBSLangCookie"); - } - None -} - // FIXME: support handling multiple compression methods fn extract_compression_method(headers: &http::HeaderMap) -> Option { if let Some(Ok(encodings)) = headers.get(header::ACCEPT_ENCODING).map(|v| v.to_str()) { @@ -727,17 +720,17 @@ async fn handle_request( } if comp_len == 0 { - let language = extract_lang_header(&parts.headers); match api.check_auth(&parts.headers, &method).await { Ok((auth_id, _user_info)) => { - return Ok(api.get_index(Some(auth_id), language, parts).await); + rpcenv.set_auth_id(Some(auth_id)); + return Ok(api.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(None, language, parts).await); + return Ok(api.get_index(rpcenv, parts).await); } else { let filename = api.find_alias(&components); let compression = extract_compression_method(&parts.headers);