From 376d90679cf69cf5505519c6bdde9cd3d39c8372 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Tue, 6 Apr 2021 11:03:44 +0200 Subject: [PATCH] server/rest: compress api calls Signed-off-by: Dominik Csapak --- src/server/rest.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/server/rest.rs b/src/server/rest.rs index 02258eab..61513e4b 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -39,7 +39,7 @@ use crate::api2::types::{Authid, Userid}; use crate::auth_helpers::*; use crate::config::cached_user_info::CachedUserInfo; use crate::tools; -use crate::tools::compression::CompressionMethod; +use crate::tools::compression::{CompressionMethod, DeflateEncoder, Level}; use crate::tools::FileLogger; extern "C" { @@ -397,6 +397,7 @@ pub async fn handle_api_request, ) -> Result, Error> { let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000); + let compression = extract_compression_method(&parts.headers); let result = match info.handler { ApiHandler::AsyncHttp(handler) => { @@ -417,7 +418,7 @@ pub async fn handle_api_request resp, Err(err) => { if let Some(httperr) = err.downcast_ref::() { @@ -429,6 +430,24 @@ pub async fn handle_api_request { + resp.headers_mut().insert( + header::CONTENT_ENCODING, + CompressionMethod::Deflate.content_encoding(), + ); + resp.map(|body| { + Body::wrap_stream(DeflateEncoder::with_quality( + body.map_err(|err| { + proxmox::io_format_err!("error during compression: {}", err) + }), + Level::Fastest, + )) + }) + } + None => resp, + }; + if info.reload_timezone { unsafe { tzset();