From 705802e58435d91b32f01c56209534be3ba02fed Mon Sep 17 00:00:00 2001 From: Aiden McClelland <3732071+dr-bonez@users.noreply.github.com> Date: Wed, 8 Mar 2023 12:48:21 -0700 Subject: [PATCH] gzip and brotli (#2186) --- backend/Cargo.lock | 51 ++++++++++++++++++++++ backend/Cargo.toml | 5 +++ backend/src/net/static_server.rs | 72 +++++++++++++++++++++++++------- 3 files changed, 113 insertions(+), 15 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 74b525f7d..c6fc37f7c 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -60,6 +60,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "alloc-no-stdlib" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc7bb162ec39d46ab1ca8c77bf72e890535becd1751bb45f64c597edb4c8c6b3" + +[[package]] +name = "alloc-stdlib" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94fb8275041c72129eb51b7d0322c29b8387a0386127718b096429201a5d6ece" +dependencies = [ + "alloc-no-stdlib", +] + [[package]] name = "android_system_properties" version = "0.1.5" @@ -130,6 +145,20 @@ dependencies = [ "futures-core", ] +[[package]] +name = "async-compression" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942c7cd7ae39e91bde4820d74132e9862e62c2f386c3aa90ccf55949f5bad63a" +dependencies = [ + "brotli", + "flate2", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", +] + [[package]] name = "async-stream" version = "0.3.3" @@ -417,6 +446,27 @@ dependencies = [ "serde_with 1.14.0", ] +[[package]] +name = "brotli" +version = "3.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a0b1dbcc8ae29329621f8d4f0d835787c1c38bb1401979b49d13b0b305ff68" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", + "brotli-decompressor", +] + +[[package]] +name = "brotli-decompressor" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b6561fd3f895a11e8f72af2cb7d22e08366bebc2b6b57f7744c4bda27034744" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + [[package]] name = "bstr" version = "0.2.17" @@ -1266,6 +1316,7 @@ name = "embassy-os" version = "0.3.4" dependencies = [ "aes", + "async-compression", "async-stream", "async-trait", "avahi-sys", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 0962bbefe..1b45c8efa 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -48,6 +48,11 @@ unstable = ["patch-db/unstable"] [dependencies] aes = { version = "0.7.5", features = ["ctr"] } +async-compression = { version = "0.3.15", features = [ + "gzip", + "brotli", + "tokio", +] } async-stream = "0.3.3" async-trait = "0.1.56" avahi-sys = { git = "https://github.com/Start9Labs/avahi-sys", version = "0.10.0", branch = "feature/dynamic-linking", features = [ diff --git a/backend/src/net/static_server.rs b/backend/src/net/static_server.rs index c9a95763c..23b9055bd 100644 --- a/backend/src/net/static_server.rs +++ b/backend/src/net/static_server.rs @@ -3,14 +3,18 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::UNIX_EPOCH; +use async_compression::tokio::bufread::BrotliEncoder; +use async_compression::tokio::bufread::GzipEncoder; use color_eyre::eyre::eyre; use digest::Digest; use futures::FutureExt; +use http::header::ACCEPT_ENCODING; use http::response::Builder; use hyper::{Body, Method, Request, Response, StatusCode}; use rpc_toolkit::rpc_handler; use tokio::fs::File; -use tokio_util::codec::{BytesCodec, FramedRead}; +use tokio::io::BufReader; +use tokio_util::io::ReaderStream; use crate::context::{DiagnosticContext, InstallContext, RpcContext, SetupContext}; use crate::core::rpc_continuations::RequestGuid; @@ -225,11 +229,20 @@ async fn alt_ui(req: Request
, ui_mode: UiMode) -> Result