From 53b00ba035e990b42f79f70d6b0d5c04289a574b Mon Sep 17 00:00:00 2001 From: nabijaczleweli Date: Tue, 10 Jan 2017 23:57:17 +0100 Subject: [PATCH] Don't encode files bigger than MAX_ENCODING_SIZE I tried to encode a 2.3GiB file and let's just say the results were pretty bad MAX_ENCODING_SIZE is currently set to 100MiB --- src/ops.rs | 5 +++-- src/util/content_encoding.rs | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ops.rs b/src/ops.rs index 4be7eca..ffd9ca4 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -15,7 +15,7 @@ use mime_guess::guess_mime_type_opt; use iron::{headers, status, method, mime, IronResult, Listening, Response, TypeMap, Request, Handler, Iron}; use self::super::util::{url_path, file_hash, is_symlink, encode_str, encode_file, hash_string, html_response, file_binary, percent_decode, response_encoding, detect_file_as_dir, encoding_extension, file_time_modified, human_readable_size, USER_AGENT, ERROR_HTML, INDEX_EXTENSIONS, - MIN_ENCODING_SIZE, DIRECTORY_LISTING_HTML}; + MAX_ENCODING_SIZE, MIN_ENCODING_SIZE, DIRECTORY_LISTING_HTML}; // TODO: ideally this String here would be Encoding instead but hyper is bad @@ -118,7 +118,8 @@ impl HttpHandler { }); println!("{} was served file {} as {}", req.remote_addr, req_p.display(), mime_type); - if self.encode_fs && req_p.metadata().unwrap().len() > MIN_ENCODING_SIZE { + let flen = req_p.metadata().unwrap().len(); + if self.encode_fs && flen > MIN_ENCODING_SIZE && flen < MAX_ENCODING_SIZE { self.handle_get_file_encoded(req, req_p, mime_type) } else { Ok(Response::with((status::Ok, diff --git a/src/util/content_encoding.rs b/src/util/content_encoding.rs index 9908705..d0cf730 100644 --- a/src/util/content_encoding.rs +++ b/src/util/content_encoding.rs @@ -22,6 +22,9 @@ lazy_static! { /// The minimal size at which to encode filesystem files. pub static MIN_ENCODING_SIZE: u64 = 1024; +/// The maximal size at which to encode filesystem files. +pub static MAX_ENCODING_SIZE: u64 = 100 * 1024 * 1024; + /// Find best supported encoding to use, or `None` for identity. pub fn response_encoding(requested: &mut [QualityItem]) -> Option {