]> git.proxmox.com Git - proxmox-backup.git/commitdiff
proxmox-rest-server: pass owned RestEnvironment to get_index
authorDietmar Maurer <dietmar@proxmox.com>
Mon, 4 Oct 2021 12:49:25 +0000 (14:49 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 5 Oct 2021 09:12:53 +0000 (11:12 +0200)
This way we avoid pointers with lifetimes.

proxmox-rest-server/examples/minimal-rest-server.rs
proxmox-rest-server/src/api_config.rs
proxmox-rest-server/src/rest.rs
proxmox-restore-daemon/src/main.rs
src/bin/proxmox-backup-api.rs
src/bin/proxmox-backup-proxy.rs

index 1d6a8a2bf8b4954691d036d1f496a4312e34d846..b1ef933530479ce6cc323afe0dc4fdb968167a2b 100644 (file)
@@ -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<String>,
-    _language: Option<String>,
-    _api: &'a ApiConfig,
+    _env: RestEnvironment,
     _parts: http::request::Parts,
 ) -> Pin<Box<dyn Future<Output = http::Response<hyper::Body>> + Send + 'a>> {
     Box::pin(async move {
index 4ff1df5f1eb2afe6c9de434b24a09a8f9a343742..c7c71ec025b5b55192e9804440020eca3db2a33c 100644 (file)
@@ -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<String>, Option<String>, &'a ApiConfig, Parts) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> + Send + Sync);
+pub type GetIndexFn = &'static (dyn Fn(RestEnvironment, Parts) -> Pin<Box<dyn Future<Output = Response<Body>> + 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<String>,
-        language: Option<String>,
+        rest_env: RestEnvironment,
         parts: Parts,
     ) -> Response<Body> {
-        (self.get_index_fn)(auth_id, language, self, parts).await
+        (self.get_index_fn)(rest_env, parts).await
     }
 
     pub(crate) async fn check_auth(
index d0a748c8b2ab0afb4d68ab75d0be168f174b006a..063b797f72a489972c44a00a62286cba11fe4281 100644 (file)
@@ -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<String> {
-    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<CompressionMethod> {
     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);
index 139adebd50c082b4335728c9775d4b7afe7184ee..1e175abd9932957b1dd1aefa5414c45084db566b 100644 (file)
@@ -23,7 +23,7 @@ use hyper::header;
 use proxmox::api::RpcEnvironmentType;
 
 use pbs_client::DEFAULT_VSOCK_PORT;
-use proxmox_rest_server::{ApiConfig, RestServer};
+use proxmox_rest_server::{ApiConfig, RestServer, RestEnvironment};
 
 mod proxmox_restore_daemon;
 use proxmox_restore_daemon::*;
@@ -94,9 +94,7 @@ fn setup_system_env() -> Result<(), Error> {
 }
 
 fn get_index<'a>(
-    _auth_id: Option<String>,
-    _language: Option<String>,
-    _api: &'a ApiConfig,
+    _env: RestEnvironment,
     _parts: Parts,
 ) -> Pin<Box<dyn Future<Output = http::Response<Body>> + Send + 'a>> {
     Box::pin(async move {
index 5d1480b6d560b9fa8328f2de5b94a200e3c706a4..cdfa79f330de0a5a53ad338af92e49fff4130f2b 100644 (file)
@@ -12,7 +12,7 @@ use proxmox::try_block;
 use proxmox::api::RpcEnvironmentType;
 use proxmox::tools::fs::CreateOptions;
 
-use proxmox_rest_server::{daemon, ApiConfig, RestServer};
+use proxmox_rest_server::{daemon, ApiConfig, RestServer, RestEnvironment};
 
 use proxmox_backup::server::auth::default_api_auth;
 use proxmox_backup::auth_helpers::*;
@@ -28,9 +28,7 @@ fn main() {
 }
 
 fn get_index<'a>(
-    _auth_id: Option<String>,
-    _language: Option<String>,
-    _api: &'a ApiConfig,
+    _env: RestEnvironment,
     _parts: Parts,
 ) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> {
     Box::pin(async move {
index 7331b2a4ddf880a20b75019993e52c3ed5e1313d..57bd50bf68b2be5e3a520747c27e87a66b2fe452 100644 (file)
@@ -17,13 +17,15 @@ use tokio_stream::wrappers::ReceiverStream;
 use serde_json::{json, Value};
 
 use proxmox::try_block;
-use proxmox::api::RpcEnvironmentType;
+use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
 use proxmox::sys::linux::socket::set_tcp_keepalive;
 use proxmox::tools::fs::CreateOptions;
 
 use pbs_tools::task_log;
 use pbs_datastore::DataStore;
-use proxmox_rest_server::{rotate_task_log_archive, ApiConfig, RestServer, WorkerTask};
+use proxmox_rest_server::{
+    rotate_task_log_archive, extract_cookie , ApiConfig, RestServer, RestEnvironment, WorkerTask,
+};
 
 use proxmox_backup::{
     server::{
@@ -78,23 +80,31 @@ fn main() -> Result<(), Error> {
     pbs_runtime::main(run())
 }
 
+
+fn extract_lang_header(headers: &http::HeaderMap) -> Option<String> {
+    if let Some(Ok(cookie)) = headers.get("COOKIE").map(|v| v.to_str()) {
+        return extract_cookie(cookie, "PBSLangCookie");
+    }
+    None
+}
+
 fn get_index<'a>(
-    auth_id: Option<String>,
-    language: Option<String>,
-    api: &'a ApiConfig,
+    env: RestEnvironment,
     parts: Parts,
 ) -> Pin<Box<dyn Future<Output = Response<Body>> + Send + 'a>> {
-    Box::pin(get_index_future(auth_id, language, api, parts))
+    Box::pin(get_index_future(env, parts))
 }
 
 async fn get_index_future(
-    auth_id: Option<String>,
-    language: Option<String>,
-    api: &ApiConfig,
+    env: RestEnvironment,
     parts: Parts,
 ) -> Response<Body> {
 
-    // fixme: make all IO async
+    let auth_id = env.get_auth_id();
+    let api = env.api_config();
+    let language = extract_lang_header(&parts.headers);
+
+     // fixme: make all IO async
 
     let (userid, csrf_token) = match auth_id {
         Some(auth_id) => {