diff --git a/Cargo.lock b/Cargo.lock index 3457329..2f48dd1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "ahash" version = "0.7.4" @@ -31,6 +37,7 @@ version = "0.1.0" dependencies = [ "anyhow", "chrono", + "flate2", "handlebars", "handlebars_sprig", "minify", @@ -99,6 +106,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + [[package]] name = "digest" version = "0.8.1" @@ -114,6 +130,18 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "flate2" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e6988e897c1c9c485f43b47a529cef42fde0547f9d8d41a7062518f1d8fc53f" +dependencies = [ + "cfg-if", + "crc32fast", + "libc", + "miniz_oxide", +] + [[package]] name = "generic-array" version = "0.12.4" @@ -209,6 +237,16 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e93bacfc6ce0cf3e41da4d9415904090e1f7ca8d105c1396907f78d8fee42635" +[[package]] +name = "miniz_oxide" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +dependencies = [ + "adler", + "autocfg", +] + [[package]] name = "num-integer" version = "0.1.44" diff --git a/Cargo.toml b/Cargo.toml index d354b16..122c7e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ serde_json = "1.0" toml = "0.5" chrono = {version = "0.4", features = ["serde"]} minify = "1.2" -handlebars_sprig = { version = "0.1.0" } \ No newline at end of file +handlebars_sprig = { version = "0.1.0" } +flate2 = "1.0.22" diff --git a/src/response.rs b/src/response.rs index ebf5198..2417d28 100644 --- a/src/response.rs +++ b/src/response.rs @@ -1,3 +1,7 @@ +use flate2::write::GzEncoder; +use flate2::Compression; +use std::io::{self, Write}; + pub fn not_found(route: String, body: String) { eprintln!("Not Found: {}", route); println!("Content-Type: text/html; charset=utf-8"); @@ -27,3 +31,23 @@ pub fn send_redirect(route: String, location: String, status: String) { eprintln!("redirected {} to {} (Code: {})", route, &location, &status); println!("Status: {}\nLocation: {}\n", status, location) } + +pub fn send_gzip_result( + route: String, + body: String, + content_type: String, + status_opt: Option, +) { + eprintln!("responded: {}", route); + + // Intentionally do not override the Wagi default behavior with a default Bartholomew message. + if let Some(status) = status_opt { + println!("Status: {}", status); + } + println!("Content-Encoding: {}", "gzip"); + println!("Content-Type: {}\n", content_type); + + let mut e = GzEncoder::new(Vec::new(), Compression::default()); + e.write_all(body.as_bytes()).unwrap(); + io::stdout().write_all(&e.finish().unwrap()).unwrap(); +}