Skip to content

Commit

Permalink
add fn to send gzip encoded result
Browse files Browse the repository at this point in the history
  • Loading branch information
rajatjindal committed Feb 19, 2022
1 parent bd04234 commit d83a8df
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
handlebars_sprig = { version = "0.1.0" }
flate2 = "1.0.22"
24 changes: 24 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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<String>,
) {
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();
}

0 comments on commit d83a8df

Please sign in to comment.