Skip to content
Browse files

Check #deflate.min_compress_size option for large files

Now large files are compressed only if their size is greater than
the #deflate.min_compress_size option.
  • Loading branch information...
1 parent a3edbee commit 90e36db8faa0a218a4bd1b995227249f7a021128 @capflam capflam committed Jul 10, 2014
Showing with 52 additions and 22 deletions.
  1. +19 −12 src/yaws_server.erl
  2. +22 −10 test/t5/app_test.erl
  3. +11 −0 test/t5/yaws.conf.in
View
31 src/yaws_server.erl
@@ -3639,26 +3639,31 @@ handle_crash(ARG, L) ->
end.
%% Ret: true | false | {data, Data}
-decide_deflate(false, _, _, _, _, _) ->
+decide_deflate(false, _, _, _, _, _, _) ->
?Debug("Compression not supported by the server~n", []),
false;
-decide_deflate(_, _, _, _, identity, _) ->
+decide_deflate(_, _, _, _, _, identity, _) ->
?Debug("No compression: Encoding=identity~n", []),
false;
-decide_deflate(_, _, _, _, deflate, _) ->
+decide_deflate(_, _, _, _, _, deflate, _) ->
?Debug("Compression already handled: Encoding=deflate~n", []),
false;
-decide_deflate(true, SC, Arg, Data, decide, Mode) ->
- Bin = to_binary(Data),
+decide_deflate(true, SC, Arg, Sz, Data, decide, Mode) ->
DOpts = SC#sconf.deflate_options,
if
- Mode == final andalso size(Bin) == 0 ->
+ Mode == final andalso size(Data) == 0 ->
?Debug("No data to be compressed~n",[]),
false;
Mode == final andalso
DOpts#deflate.min_compress_size /= nolimit andalso
- size(Bin) < DOpts#deflate.min_compress_size ->
+ size(Data) < DOpts#deflate.min_compress_size ->
+ ?Debug("Data too small to be compressed~n",[]),
+ false;
+
+ is_integer(Sz) andalso
+ DOpts#deflate.min_compress_size /= nolimit andalso
+ Sz < DOpts#deflate.min_compress_size ->
?Debug("Data too small to be compressed~n",[]),
false;
@@ -3674,7 +3679,7 @@ decide_deflate(true, SC, Arg, Data, decide, Mode) ->
true when Mode =:= final ->
?Debug("Compress data~n", []),
yaws:outh_set_content_encoding(deflate),
- {ok, DB} = yaws_zlib:gzip(to_binary(Data), DOpts),
+ {ok, DB} = yaws_zlib:gzip(Data, DOpts),
{data, DB};
true -> %% Mode == stream | {file,_,_}
?Debug("Compress streamed data~n", []),
@@ -3734,7 +3739,8 @@ deliver_accumulated(Arg, Sock, ContentLength, Mode) ->
yaws:outh_set_transfer_encoding_off(),
{discard, []};
_ ->
- deflate_accumulated(Arg, Cont, ContentLength, Mode)
+ deflate_accumulated(Arg, to_binary(Cont),
+ ContentLength, Mode)
end,
{StatusLine, Headers} = yaws:outh_serialize(),
@@ -3752,7 +3758,8 @@ deflate_accumulated(Arg, Content, ContentLength, Mode) ->
Enc = yaws:outh_get_content_encoding(),
DOpts = SC#sconf.deflate_options,
{Result, Data, Size} =
- case decide_deflate(?sc_has_deflate(SC), SC, Arg, Content, Enc, Mode) of
+ case decide_deflate(?sc_has_deflate(SC), SC, Arg, ContentLength,
+ Content, Enc, Mode) of
{data, Bin} ->
%% implies Mode==final
{undefined, Bin, binary_size(Bin)};
@@ -3761,7 +3768,7 @@ deflate_accumulated(Arg, Content, ContentLength, Mode) ->
Z = zlib:open(),
{ok, Priv, Bin} =
yaws_zlib:gzipDeflate(Z,yaws_zlib:gzipInit(Z,DOpts),
- to_binary(Content),none),
+ Content,none),
{{Z, Priv}, Bin, undefined};
true ->
%% implies Mode=={file,_,_} and use_gzip_static==true
@@ -3775,7 +3782,7 @@ deflate_accumulated(Arg, Content, ContentLength, Mode) ->
Z = zlib:open(),
{ok, Priv, Bin} =
yaws_zlib:gzipDeflate(Z,yaws_zlib:gzipInit(Z,DOpts),
- to_binary(Content),none),
+ Content,none),
{{Z, Priv}, Bin, undefined}
end;
View
32 test/t5/app_test.erl
@@ -64,7 +64,7 @@ deflate_enabled() ->
?line "Accept-Encoding" = proplists:get_value("Vary", Hdrs1),
?line true = is_binary(zlib:gunzip(Body1)),
- %% Partial content is not compressed for small (and catched) files
+ %% Partial content is not compressed for small (and cached) files
Uri2 = "http://localhost:8001/1000.txt",
?line {ok, "206", Hdrs2, _} =
ibrowse:send_req(Uri2, [{"Accept-Encoding", "gzip, deflate"},
@@ -211,7 +211,7 @@ deflate_mime_types() ->
deflate_compress_size() ->
io:format(" deflate_compress_size\n", []),
- %% Static content (cached)
+ %% Small static content (cached)
Uri1 = "http://localhost:8005/1000.txt",
?line {ok, "200", Hdrs1, _} =
ibrowse:send_req(Uri1, [{"Accept-Encoding", "gzip, deflate"}], get),
@@ -223,17 +223,29 @@ deflate_compress_size() ->
?line "gzip" = proplists:get_value("Content-Encoding", Hdrs2),
?line true = is_binary(zlib:gunzip(Body2)),
- %% Dynamic content
- Uri3 = "http://localhost:8005/smalltest",
- ?line {ok, "200", Hdrs3, _} =
+ %% Large static content (cached)
+ Uri3 = "http://localhost:8005/10000.txt",
+ ?line {ok, "200", Hdrs3, Body3} =
ibrowse:send_req(Uri3, [{"Accept-Encoding", "gzip, deflate"}], get),
- ?line undefined = proplists:get_value("Content-Encoding", Hdrs3),
+ ?line "gzip" = proplists:get_value("Content-Encoding", Hdrs3),
+ ?line true = is_binary(zlib:gunzip(Body3)),
- Uri4 = "http://localhost:8005/bigtest",
- ?line {ok, "200", Hdrs4, Body4} =
+ Uri4 = "http://localhost:8008/10000.txt",
+ ?line {ok, "200", Hdrs4, _} =
ibrowse:send_req(Uri4, [{"Accept-Encoding", "gzip, deflate"}], get),
- ?line "gzip" = proplists:get_value("Content-Encoding", Hdrs4),
- ?line true = is_binary(zlib:gunzip(Body4)),
+ ?line undefined = proplists:get_value("Content-Encoding", Hdrs4),
+
+ %% Dynamic content
+ Uri5 = "http://localhost:8005/smalltest",
+ ?line {ok, "200", Hdrs5, _} =
+ ibrowse:send_req(Uri5, [{"Accept-Encoding", "gzip, deflate"}], get),
+ ?line undefined = proplists:get_value("Content-Encoding", Hdrs5),
+
+ Uri6 = "http://localhost:8005/bigtest",
+ ?line {ok, "200", Hdrs6, Body6} =
+ ibrowse:send_req(Uri6, [{"Accept-Encoding", "gzip, deflate"}], get),
+ ?line "gzip" = proplists:get_value("Content-Encoding", Hdrs6),
+ ?line true = is_binary(zlib:gunzip(Body6)),
ok.
View
11 test/t5/yaws.conf.in
@@ -97,3 +97,14 @@ max_size_cached_file = 5120000
use_gzip_static = true
</deflate>
</server>
+
+
+<server localhost>
+ port = 8008
+ listen = 0.0.0.0
+ deflate = true
+ docroot = @abs_builddir@/docroot-test @abs_top_srcdir@/www
+ <deflate>
+ min_compress_size = 20480000
+ </deflate>
+</server>

0 comments on commit 90e36db

Please sign in to comment.
Something went wrong with that request. Please try again.