From 8aa67ee758d06791edb85c85d6a52ef49520467e Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 29 Jul 2020 09:38:11 +0200 Subject: [PATCH] bump proxmox to 0.3, cleanup http_err macro usage Also swap the order of a couple of `.map_err().await` to `.await.map_err()` since that's generally more efficient. Signed-off-by: Wolfgang Bumiller --- Cargo.toml | 2 +- src/api2/access.rs | 2 +- src/api2/admin/datastore.rs | 6 +++--- src/api2/helpers.rs | 21 +++++++++++---------- src/api2/reader.rs | 6 +++--- src/server/h2service.rs | 4 ++-- src/server/rest.rs | 20 ++++++++++---------- 7 files changed, 31 insertions(+), 30 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 23063956..6015e3ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ pam-sys = "0.5" percent-encoding = "2.1" pin-utils = "0.1.0" pathpatterns = "0.1.2" -proxmox = { version = "0.2.1", features = [ "sortable-macro", "api-macro", "websocket" ] } +proxmox = { version = "0.3.0", features = [ "sortable-macro", "api-macro", "websocket" ] } #proxmox = { git = "ssh://gitolite3@proxdev.maurer-it.com/rust/proxmox", version = "0.1.2", features = [ "sortable-macro", "api-macro" ] } #proxmox = { path = "../proxmox/proxmox", features = [ "sortable-macro", "api-macro", "websocket" ] } proxmox-fuse = "0.1.0" diff --git a/src/api2/access.rs b/src/api2/access.rs index 46fbd991..7a87a51f 100644 --- a/src/api2/access.rs +++ b/src/api2/access.rs @@ -156,7 +156,7 @@ fn create_ticket( Err(err) => { let client_ip = "unknown"; // $rpcenv->get_client_ip() || ''; log::error!("authentication failure; rhost={} user={} msg={}", client_ip, username, err.to_string()); - Err(http_err!(UNAUTHORIZED, "permission check failed.".into())) + Err(http_err!(UNAUTHORIZED, "permission check failed.")) } } } diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 4fcff86d..17e31f1e 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -846,8 +846,8 @@ fn download_file( path.push(&file_name); let file = tokio::fs::File::open(&path) - .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err))) - .await?; + .await + .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?; let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze())) @@ -954,7 +954,7 @@ fn download_file_decoded( }, "blob" => { let file = std::fs::File::open(&path) - .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?; + .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?; Body::wrap_stream( WrappedReaderStream::new(DataBlobReader::new(file, None)?) diff --git a/src/api2/helpers.rs b/src/api2/helpers.rs index a3c68087..3a11340b 100644 --- a/src/api2/helpers.rs +++ b/src/api2/helpers.rs @@ -1,18 +1,19 @@ use std::path::PathBuf; + use anyhow::Error; -use futures::*; +use futures::stream::TryStreamExt; use hyper::{Body, Response, StatusCode, header}; -use proxmox::http_err; + +use proxmox::http_bail; pub async fn create_download_response(path: PathBuf) -> Result, Error> { - let file = tokio::fs::File::open(path.clone()) - .map_err(move |err| { - match err.kind() { - std::io::ErrorKind::NotFound => http_err!(NOT_FOUND, format!("open file {:?} failed - not found", path.clone())), - _ => http_err!(BAD_REQUEST, format!("open file {:?} failed: {}", path.clone(), err)), - } - }) - .await?; + let file = match tokio::fs::File::open(path.clone()).await { + Ok(file) => file, + Err(ref err) if err.kind() == std::io::ErrorKind::NotFound => { + http_bail!(NOT_FOUND, "open file {:?} failed - not found", path); + } + Err(err) => http_bail!(BAD_REQUEST, "open file {:?} failed: {}", path, err), + }; let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) .map_ok(|bytes| hyper::body::Bytes::from(bytes.freeze())); diff --git a/src/api2/reader.rs b/src/api2/reader.rs index 4c14c97c..15ff15d1 100644 --- a/src/api2/reader.rs +++ b/src/api2/reader.rs @@ -225,8 +225,8 @@ fn download_chunk( env.debug(format!("download chunk {:?}", path)); let data = tokio::fs::read(path) - .map_err(move |err| http_err!(BAD_REQUEST, format!("reading file {:?} failed: {}", path2, err))) - .await?; + .await + .map_err(move |err| http_err!(BAD_REQUEST, "reading file {:?} failed: {}", path2, err))?; let body = Body::from(data); @@ -260,7 +260,7 @@ fn download_chunk_old( let path3 = path.clone(); let response_future = tokio::fs::File::open(path) - .map_err(move |err| http_err!(BAD_REQUEST, format!("open file {:?} failed: {}", path2, err))) + .map_err(move |err| http_err!(BAD_REQUEST, "open file {:?} failed: {}", path2, err)) .and_then(move |file| { env2.debug(format!("download chunk {:?}", path3)); let payload = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new()) diff --git a/src/server/h2service.rs b/src/server/h2service.rs index 453495d0..0bb735b8 100644 --- a/src/server/h2service.rs +++ b/src/server/h2service.rs @@ -44,7 +44,7 @@ impl H2Service { let (path, components) = match tools::normalize_uri_path(parts.uri.path()) { Ok((p,c)) => (p, c), - Err(err) => return future::err(http_err!(BAD_REQUEST, err.to_string())).boxed(), + Err(err) => return future::err(http_err!(BAD_REQUEST, "{}", err)).boxed(), }; self.debug(format!("{} {}", method, path)); @@ -55,7 +55,7 @@ impl H2Service { match self.router.find_method(&components, method, &mut uri_param) { None => { - let err = http_err!(NOT_FOUND, format!("Path '{}' not found.", path).to_string()); + let err = http_err!(NOT_FOUND, "Path '{}' not found.", path); future::ok((formatter.format_error)(err)).boxed() } Some(api_method) => { diff --git a/src/server/rest.rs b/src/server/rest.rs index 09b68489..9547b90e 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -204,13 +204,13 @@ async fn get_request_parameters( } let body = req_body - .map_err(|err| http_err!(BAD_REQUEST, format!("Promlems reading request body: {}", err))) + .map_err(|err| http_err!(BAD_REQUEST, "Promlems reading request body: {}", err)) .try_fold(Vec::new(), |mut acc, chunk| async move { if acc.len() + chunk.len() < 64*1024 { //fimxe: max request body size? acc.extend_from_slice(&*chunk); Ok(acc) } else { - Err(http_err!(BAD_REQUEST, "Request body too large".to_string())) + Err(http_err!(BAD_REQUEST, "Request body too large")) } }).await?; @@ -392,12 +392,12 @@ async fn simple_static_file_download(filename: PathBuf) -> Result let mut file = File::open(filename) .await - .map_err(|err| http_err!(BAD_REQUEST, format!("File open failed: {}", err)))?; + .map_err(|err| http_err!(BAD_REQUEST, "File open failed: {}", err))?; let mut data: Vec = Vec::new(); file.read_to_end(&mut data) .await - .map_err(|err| http_err!(BAD_REQUEST, format!("File read failed: {}", err)))?; + .map_err(|err| http_err!(BAD_REQUEST, "File read failed: {}", err))?; let mut response = Response::new(data.into()); response.headers_mut().insert( @@ -411,7 +411,7 @@ async fn chuncked_static_file_download(filename: PathBuf) -> Result Result Result, Error> { let metadata = tokio::fs::metadata(filename.clone()) - .map_err(|err| http_err!(BAD_REQUEST, format!("File access problems: {}", err))) + .map_err(|err| http_err!(BAD_REQUEST, "File access problems: {}", err)) .await?; if metadata.len() < 1024*32 { @@ -535,7 +535,7 @@ pub async fn handle_request(api: Arc, req: Request) -> Result rpcenv.set_user(Some(username)), Err(err) => { // always delay unauthorized calls by 3 seconds (from start of request) - let err = http_err!(UNAUTHORIZED, format!("authentication failed - {}", err)); + let err = http_err!(UNAUTHORIZED, "authentication failed - {}", err); tokio::time::delay_until(Instant::from_std(delay_unauth_time)).await; return Ok((formatter.format_error)(err)); } @@ -544,13 +544,13 @@ pub async fn handle_request(api: Arc, req: Request) -> Result { - let err = http_err!(NOT_FOUND, format!("Path '{}' not found.", path).to_string()); + let err = http_err!(NOT_FOUND, "Path '{}' not found.", path); return Ok((formatter.format_error)(err)); } Some(api_method) => { let user = rpcenv.get_user(); if !check_api_permission(api_method.access.permission, user.as_deref(), &uri_param, user_info.as_ref()) { - let err = http_err!(FORBIDDEN, format!("permission check failed")); + let err = http_err!(FORBIDDEN, "permission check failed"); tokio::time::delay_until(Instant::from_std(access_forbidden_time)).await; return Ok((formatter.format_error)(err)); } @@ -598,5 +598,5 @@ pub async fn handle_request(api: Arc, req: Request) -> Result