From 149ec1d14f43ce125878082dbfe9df0fd5ba8654 Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 23 Nov 2022 11:51:51 -0600 Subject: [PATCH] Minify HTML --- Cargo.lock | 130 ++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 1 + src/config.rs | 8 +-- src/routes/index.rs | 19 ++++++- 4 files changed, 147 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea0bc58..44d7189 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -305,6 +305,7 @@ dependencies = [ "metrics", "metrics-util", "mime", + "minify-html", "opentelemetry", "opentelemetry-otlp", "pin-project-lite", @@ -354,6 +355,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "983cd8b9d4b02a6dc6ffa557262eb5858a27a0038ffffe21a0f133eaa819a164" +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "async-mutex" version = "1.4.0" @@ -572,6 +579,18 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "bitvec" +version = "0.19.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55f93d0ef3363c364d5976646a38f04cf67cfe1d4c8d160cdea02cab2c116b33" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + [[package]] name = "block-buffer" version = "0.10.3" @@ -702,7 +721,7 @@ dependencies = [ "async-trait", "json5", "lazy_static", - "nom", + "nom 7.1.1", "pathdiff", "ron", "rust-ini", @@ -820,6 +839,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "css-minify" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692b185e3b7c9af96b3195f3021f53a931d896968ed2ad3fb1cdb6558b30c9ab" +dependencies = [ + "derive_more", + "indexmap", + "nom 6.1.2", +] + [[package]] name = "darling" version = "0.13.4" @@ -1011,6 +1041,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "funty" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7" + [[package]] name = "futf" version = "0.1.5" @@ -1177,7 +1213,7 @@ dependencies = [ "base64", "byteorder", "flate2", - "nom", + "nom 7.1.1", "num-traits", ] @@ -1453,6 +1489,19 @@ dependencies = [ "spin", ] +[[package]] +name = "lexical-core" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6607c62aa161d23d17a9072cc5da0be67cdfc89d3afb1e8d9c842bebc2525ffe" +dependencies = [ + "arrayvec", + "bitflags", + "cfg-if", + "ryu", + "static_assertions", +] + [[package]] name = "libc" version = "0.2.137" @@ -1647,6 +1696,29 @@ dependencies = [ "unicase", ] +[[package]] +name = "minify-html" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f84854d62363972a73c3d8331b85a479366a0871a83f2a01ac11b9ba787c10" +dependencies = [ + "aho-corasick", + "css-minify", + "lazy_static", + "memchr", + "minify-js", +] + +[[package]] +name = "minify-js" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe033709f5a1159736cf7e22748518ffb75af26f3a6264d52ecc8bb38c68c36" +dependencies = [ + "lazy_static", + "parse-js", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1701,6 +1773,19 @@ dependencies = [ "smallvec", ] +[[package]] +name = "nom" +version = "6.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7413f999671bd4745a7b624bd370a569fb6bc574b23c83a3c5ed2e453f3d5e2" +dependencies = [ + "bitvec", + "funty", + "lexical-core", + "memchr", + "version_check", +] + [[package]] name = "nom" version = "7.1.1" @@ -1719,7 +1804,7 @@ checksum = "37794436ca3029a3089e0b95d42da1f0b565ad271e4d3bb4bad0c7bb70b10605" dependencies = [ "bytecount", "memchr", - "nom", + "nom 7.1.1", ] [[package]] @@ -1978,6 +2063,17 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "parse-js" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66bb85ec60d22b9e6d4adac1e3dbdaf3903a4485f476c5f4dd7ed1285cbf4dad" +dependencies = [ + "aho-corasick", + "lazy_static", + "memchr", +] + [[package]] name = "paste" version = "1.0.9" @@ -2292,6 +2388,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8" + [[package]] name = "radix_trie" version = "0.2.1" @@ -2503,7 +2605,7 @@ dependencies = [ "arc-swap", "fastrand", "lazy_static", - "nom", + "nom 7.1.1", "nom_locate", "num-bigint", "num-integer", @@ -2523,7 +2625,7 @@ dependencies = [ "itertools 0.10.5", "md5", "mime", - "nom", + "nom 7.1.1", "rsass", ] @@ -2769,6 +2871,12 @@ dependencies = [ "der", ] +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + [[package]] name = "string_cache" version = "0.8.4" @@ -2836,6 +2944,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20f34339676cdcab560c9a82300c4c2581f68b9369aedf0fae86f2ff9565ff3e" +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "teloxide" version = "0.11.2" @@ -3635,6 +3749,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "wyz" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214" + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index b753c1f..5f8892a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ lru = "0.8.0" metrics = "0.20.1" metrics-util = "0.14.0" mime = "0.3.16" +minify-html = "0.10.0" opentelemetry = { version = "0.18", features = ["rt-tokio"] } opentelemetry-otlp = "0.11" pin-project-lite = "0.2.9" diff --git a/src/config.rs b/src/config.rs index 04a1427..1d4cf86 100644 --- a/src/config.rs +++ b/src/config.rs @@ -236,20 +236,20 @@ impl Config { Ok(Some((certs, key))) } - pub(crate) fn footer_blurb(&self) -> Option> { + pub(crate) fn footer_blurb(&self) -> Option> { if let Some(blurb) = &self.footer_blurb { if !blurb.is_empty() { - return Some(crate::templates::Html(blurb)); + return Some(crate::templates::Html(ammonia::clean(blurb))); } } None } - pub(crate) fn local_blurb(&self) -> Option> { + pub(crate) fn local_blurb(&self) -> Option> { if let Some(blurb) = &self.local_blurb { if !blurb.is_empty() { - return Some(crate::templates::Html(blurb)); + return Some(crate::templates::Html(ammonia::clean(blurb))); } } diff --git a/src/routes/index.rs b/src/routes/index.rs index 048eb05..835e2b8 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -7,6 +7,19 @@ use actix_web::{web, HttpResponse}; use rand::{seq::SliceRandom, thread_rng}; use std::io::BufWriter; +const MINIFY_CONFIG: minify_html::Cfg = minify_html::Cfg { + do_not_minify_doctype: true, + ensure_spec_compliant_unquoted_attribute_values: true, + keep_closing_tags: true, + keep_html_and_head_opening_tags: false, + keep_spaces_between_attributes: true, + keep_comments: false, + minify_js: true, + minify_css: true, + remove_bangs: true, + remove_processing_instructions: true, +}; + fn open_reg(node: &Node) -> bool { node.instance .as_ref() @@ -59,10 +72,12 @@ pub(crate) async fn route( let mut buf = BufWriter::new(Vec::new()); crate::templates::index(&mut buf, &local, &nodes, &config)?; - let buf = buf.into_inner().map_err(|e| { + let html = buf.into_inner().map_err(|e| { tracing::error!("Error rendering template, {}", e.error()); ErrorKind::FlushBuffer })?; - Ok(HttpResponse::Ok().content_type("text/html").body(buf)) + let html = minify_html::minify(&html, &MINIFY_CONFIG); + + Ok(HttpResponse::Ok().content_type("text/html").body(html)) }