Skip to content

Commit

Permalink
Don't encode files bigger than MAX_ENCODING_SIZE
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nabijaczleweli committed Jan 10, 2017
1 parent 98fa387 commit 53b00ba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/util/content_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Encoding>]) -> Option<Encoding> {
Expand Down

0 comments on commit 53b00ba

Please sign in to comment.