| 1 |
%% Copyright (c) Loïc Hoguin <[email protected]> |
| 2 |
%% |
| 3 |
%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 |
%% purpose with or without fee is hereby granted, provided that the above |
| 5 |
%% copyright notice and this permission notice appear in all copies. |
| 6 |
%% |
| 7 |
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 |
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 |
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 |
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 |
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 |
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 |
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 |
|
| 15 |
-module(compress_SUITE). |
| 16 |
-compile(export_all). |
| 17 |
-compile(nowarn_export_all). |
| 18 |
|
| 19 |
-import(ct_helper, [config/2]). |
| 20 |
-import(ct_helper, [doc/1]). |
| 21 |
-import(cowboy_test, [gun_open/1]). |
| 22 |
|
| 23 |
%% ct. |
| 24 |
|
| 25 |
all() -> |
| 26 |
All = [ |
| 27 |
{group, http_compress}, |
| 28 |
{group, https_compress}, |
| 29 |
{group, h2_compress}, |
| 30 |
{group, h2c_compress} |
| 31 |
% {group, h3_compress} |
| 32 |
], |
| 33 |
%% Don't run HTTP/3 tests on Windows for now. |
| 34 |
case os:type() of |
| 35 |
{win32, _} -> |
| 36 |
All -- [{group, h3_compress}]; |
| 37 |
_ -> |
| 38 |
All |
| 39 |
end. |
| 40 |
|
| 41 |
groups() -> |
| 42 |
cowboy_test:common_groups(ct_helper:all(?MODULE)). |
| 43 |
|
| 44 |
init_per_group(Name, Config) -> |
| 45 |
cowboy_test:init_common_groups(Name, Config, ?MODULE). |
| 46 |
|
| 47 |
end_per_group(Name, _) -> |
| 48 |
cowboy_test:stop_group(Name). |
| 49 |
|
| 50 |
%% Routes. |
| 51 |
|
| 52 |
init_dispatch(_Config) -> |
| 53 |
cowboy_router:compile([{"[...]", [ |
| 54 |
{"/reply/:what", compress_h, reply}, |
| 55 |
{"/stream_reply/:what", compress_h, stream_reply} |
| 56 |
]}]). |
| 57 |
|
| 58 |
%% Internal. |
| 59 |
|
| 60 |
do_get(Path, ReqHeaders, Config) -> |
| 61 |
ConnPid = gun_open(Config), |
| 62 |
Ref = gun:get(ConnPid, Path, ReqHeaders), |
| 63 |
{response, IsFin, Status, RespHeaders} = gun:await(ConnPid, Ref), |
| 64 |
{ok, Body} = case IsFin of |
| 65 |
nofin -> gun:await_body(ConnPid, Ref); |
| 66 |
fin -> {ok, <<>>} |
| 67 |
end, |
| 68 |
gun:close(ConnPid), |
| 69 |
{Status, RespHeaders, Body}. |
| 70 |
|
| 71 |
%% Tests. |
| 72 |
|
| 73 |
gzip_accept_encoding_malformed(Config) -> |
| 74 |
doc("Send malformed accept-encoding; get an uncompressed response."), |
| 75 |
{200, Headers, _} = do_get("/reply/large", |
| 76 |
[{<<"accept-encoding">>, <<";">>}], Config), |
| 77 |
false = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 78 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 79 |
{_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 80 |
ok. |
| 81 |
|
| 82 |
gzip_accept_encoding_missing(Config) -> |
| 83 |
doc("Don't send accept-encoding; get an uncompressed response."), |
| 84 |
{200, Headers, _} = do_get("/reply/large", |
| 85 |
[], Config), |
| 86 |
false = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 87 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 88 |
{_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 89 |
ok. |
| 90 |
|
| 91 |
gzip_accept_encoding_no_gzip(Config) -> |
| 92 |
doc("Send accept-encoding: compress (unsupported by Cowboy); get an uncompressed response."), |
| 93 |
{200, Headers, _} = do_get("/reply/large", |
| 94 |
[{<<"accept-encoding">>, <<"compress">>}], Config), |
| 95 |
false = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 96 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 97 |
{_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 98 |
ok. |
| 99 |
|
| 100 |
gzip_accept_encoding_not_supported(Config) -> |
| 101 |
doc("Send unsupported accept-encoding; get an uncompressed response."), |
| 102 |
{200, Headers, _} = do_get("/reply/large", |
| 103 |
[{<<"accept-encoding">>, <<"application/gzip">>}], Config), |
| 104 |
false = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 105 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 106 |
{_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 107 |
ok. |
| 108 |
|
| 109 |
gzip_reply_content_encoding(Config) -> |
| 110 |
doc("Reply with content-encoding header; get an uncompressed response."), |
| 111 |
{200, Headers, _} = do_get("/reply/content-encoding", |
| 112 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 113 |
%% We set the content-encoding to compress; without actually compressing. |
| 114 |
{_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 115 |
%% The reply didn't include a vary header. |
| 116 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 117 |
{_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 118 |
ok. |
| 119 |
|
| 120 |
gzip_reply_etag(Config) -> |
| 121 |
doc("Reply with etag header; get an uncompressed response."), |
| 122 |
{200, Headers, _} = do_get("/reply/etag", |
| 123 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 124 |
%% We set a strong etag. |
| 125 |
{_, <<"\"STRONK\"">>} = lists:keyfind(<<"etag">>, 1, Headers), |
| 126 |
%% The reply didn't include a vary header. |
| 127 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 128 |
{_, <<"100000">>} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 129 |
ok. |
| 130 |
|
| 131 |
gzip_reply_large_body(Config) -> |
| 132 |
doc("Reply a large body; get a gzipped response."), |
| 133 |
{200, Headers, GzBody} = do_get("/reply/large", |
| 134 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 135 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 136 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 137 |
{_, Length} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 138 |
ct:log("Original length: 100000; compressed: ~s.", [Length]), |
| 139 |
_ = zlib:gunzip(GzBody), |
| 140 |
ok. |
| 141 |
|
| 142 |
gzip_reply_sendfile(Config) -> |
| 143 |
doc("Reply using sendfile; get an uncompressed response."), |
| 144 |
{200, Headers, Body} = do_get("/reply/sendfile", |
| 145 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 146 |
false = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 147 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 148 |
ct:log("Body received:~n~p~n", [Body]), |
| 149 |
ok. |
| 150 |
|
| 151 |
gzip_reply_small_body(Config) -> |
| 152 |
doc("Reply a small body; get an uncompressed response."), |
| 153 |
{200, Headers, _} = do_get("/reply/small", |
| 154 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 155 |
false = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 156 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 157 |
{_, <<"100">>} = lists:keyfind(<<"content-length">>, 1, Headers), |
| 158 |
ok. |
| 159 |
|
| 160 |
gzip_stream_reply(Config) -> |
| 161 |
doc("Stream reply; get a gzipped response."), |
| 162 |
{200, Headers, GzBody} = do_get("/stream_reply/large", |
| 163 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 164 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 165 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 166 |
_ = zlib:gunzip(GzBody), |
| 167 |
ok. |
| 168 |
|
| 169 |
gzip_stream_reply_sendfile(Config) -> |
| 170 |
doc("Stream reply using sendfile for some chunks; get a gzipped response."), |
| 171 |
{200, Headers, GzBody} = do_get("/stream_reply/sendfile", |
| 172 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 173 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 174 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 175 |
_ = zlib:gunzip(GzBody), |
| 176 |
ok. |
| 177 |
|
| 178 |
gzip_stream_reply_sendfile_fin(Config) -> |
| 179 |
doc("Stream reply using sendfile for some chunks; get a gzipped response."), |
| 180 |
{200, Headers, GzBody} = do_get("/stream_reply/sendfile_fin", |
| 181 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 182 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 183 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 184 |
_ = zlib:gunzip(GzBody), |
| 185 |
ok. |
| 186 |
|
| 187 |
gzip_stream_reply_content_encoding(Config) -> |
| 188 |
doc("Stream reply with content-encoding header; get an uncompressed response."), |
| 189 |
{200, Headers, Body} = do_get("/stream_reply/content-encoding", |
| 190 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 191 |
{_, <<"compress">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 192 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 193 |
100000 = iolist_size(Body), |
| 194 |
ok. |
| 195 |
|
| 196 |
gzip_stream_reply_etag(Config) -> |
| 197 |
doc("Stream reply with etag header; get an uncompressed response."), |
| 198 |
{200, Headers, Body} = do_get("/stream_reply/etag", |
| 199 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 200 |
{_, <<"\"STRONK\"">>} = lists:keyfind(<<"etag">>, 1, Headers), |
| 201 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 202 |
100000 = iolist_size(Body), |
| 203 |
ok. |
| 204 |
|
| 205 |
opts_compress_buffering_false(Config0) -> |
| 206 |
doc("Confirm that the compress_buffering option can be set to false, " |
| 207 |
"which is the default."), |
| 208 |
Fun = case config(ref, Config0) of |
| 209 |
https_compress -> init_https; |
| 210 |
h2_compress -> init_http2; |
| 211 |
_ -> init_http |
| 212 |
end, |
| 213 |
Config = cowboy_test:Fun(?FUNCTION_NAME, #{ |
| 214 |
env => #{dispatch => init_dispatch(Config0)}, |
| 215 |
stream_handlers => [cowboy_compress_h, cowboy_stream_h], |
| 216 |
compress_buffering => false |
| 217 |
}, Config0), |
| 218 |
try |
| 219 |
ConnPid = gun_open(Config), |
| 220 |
Ref = gun:get(ConnPid, "/stream_reply/delayed", |
| 221 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 222 |
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref), |
| 223 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 224 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 225 |
Z = zlib:open(), |
| 226 |
zlib:inflateInit(Z, 31), |
| 227 |
{data, nofin, Data1} = gun:await(ConnPid, Ref, 500), |
| 228 |
<<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)), |
| 229 |
timer:sleep(1000), |
| 230 |
{data, nofin, Data2} = gun:await(ConnPid, Ref, 500), |
| 231 |
<<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)), |
| 232 |
gun:close(ConnPid) |
| 233 |
after |
| 234 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 235 |
end. |
| 236 |
|
| 237 |
opts_compress_buffering_true(Config0) -> |
| 238 |
doc("Confirm that the compress_buffering option can be set to true, " |
| 239 |
"and that the data received is buffered."), |
| 240 |
Fun = case config(ref, Config0) of |
| 241 |
https_compress -> init_https; |
| 242 |
h2_compress -> init_http2; |
| 243 |
_ -> init_http |
| 244 |
end, |
| 245 |
Config = cowboy_test:Fun(?FUNCTION_NAME, #{ |
| 246 |
env => #{dispatch => init_dispatch(Config0)}, |
| 247 |
stream_handlers => [cowboy_compress_h, cowboy_stream_h], |
| 248 |
compress_buffering => true |
| 249 |
}, Config0), |
| 250 |
try |
| 251 |
ConnPid = gun_open(Config), |
| 252 |
Ref = gun:get(ConnPid, "/stream_reply/delayed", |
| 253 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 254 |
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref), |
| 255 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 256 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 257 |
Z = zlib:open(), |
| 258 |
zlib:inflateInit(Z, 31), |
| 259 |
%% The data gets buffered because it is too small. |
| 260 |
%% In zlib versions before OTP 20.1 the gzip header was also buffered. |
| 261 |
<<>> = case gun:await(ConnPid, Ref, 500) of |
| 262 |
{data, nofin, Data1} -> |
| 263 |
iolist_to_binary(zlib:inflate(Z, Data1)); |
| 264 |
{error, timeout} -> |
| 265 |
<<>> |
| 266 |
end, |
| 267 |
gun:close(ConnPid) |
| 268 |
after |
| 269 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 270 |
end. |
| 271 |
|
| 272 |
set_options_compress_buffering_false(Config0) -> |
| 273 |
doc("Confirm that the compress_buffering option can be dynamically " |
| 274 |
"set to false by a handler and that the data received is not buffered."), |
| 275 |
Fun = case config(ref, Config0) of |
| 276 |
https_compress -> init_https; |
| 277 |
h2_compress -> init_http2; |
| 278 |
_ -> init_http |
| 279 |
end, |
| 280 |
Config = cowboy_test:Fun(?FUNCTION_NAME, #{ |
| 281 |
env => #{dispatch => init_dispatch(Config0)}, |
| 282 |
stream_handlers => [cowboy_compress_h, cowboy_stream_h], |
| 283 |
compress_buffering => true |
| 284 |
}, Config0), |
| 285 |
try |
| 286 |
ConnPid = gun_open(Config), |
| 287 |
Ref = gun:get(ConnPid, "/stream_reply/set_options_buffering_false", |
| 288 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 289 |
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref), |
| 290 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 291 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 292 |
Z = zlib:open(), |
| 293 |
zlib:inflateInit(Z, 31), |
| 294 |
{data, nofin, Data1} = gun:await(ConnPid, Ref, 500), |
| 295 |
<<"data: Hello!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data1)), |
| 296 |
timer:sleep(1000), |
| 297 |
{data, nofin, Data2} = gun:await(ConnPid, Ref, 500), |
| 298 |
<<"data: World!\r\n\r\n">> = iolist_to_binary(zlib:inflate(Z, Data2)), |
| 299 |
gun:close(ConnPid) |
| 300 |
after |
| 301 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 302 |
end. |
| 303 |
|
| 304 |
set_options_compress_buffering_true(Config0) -> |
| 305 |
doc("Confirm that the compress_buffering option can be dynamically " |
| 306 |
"set to true by a handler and that the data received is buffered."), |
| 307 |
Fun = case config(ref, Config0) of |
| 308 |
https_compress -> init_https; |
| 309 |
h2_compress -> init_http2; |
| 310 |
_ -> init_http |
| 311 |
end, |
| 312 |
Config = cowboy_test:Fun(?FUNCTION_NAME, #{ |
| 313 |
env => #{dispatch => init_dispatch(Config0)}, |
| 314 |
stream_handlers => [cowboy_compress_h, cowboy_stream_h], |
| 315 |
compress_buffering => false |
| 316 |
}, Config0), |
| 317 |
try |
| 318 |
ConnPid = gun_open(Config), |
| 319 |
Ref = gun:get(ConnPid, "/stream_reply/set_options_buffering_true", |
| 320 |
[{<<"accept-encoding">>, <<"gzip">>}]), |
| 321 |
{response, nofin, 200, Headers} = gun:await(ConnPid, Ref), |
| 322 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 323 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 324 |
Z = zlib:open(), |
| 325 |
zlib:inflateInit(Z, 31), |
| 326 |
%% The data gets buffered because it is too small. |
| 327 |
%% In zlib versions before OTP 20.1 the gzip header was also buffered. |
| 328 |
<<>> = case gun:await(ConnPid, Ref, 500) of |
| 329 |
{data, nofin, Data1} -> |
| 330 |
iolist_to_binary(zlib:inflate(Z, Data1)); |
| 331 |
{error, timeout} -> |
| 332 |
<<>> |
| 333 |
end, |
| 334 |
gun:close(ConnPid) |
| 335 |
after |
| 336 |
cowboy:stop_listener(?FUNCTION_NAME) |
| 337 |
end. |
| 338 |
|
| 339 |
set_options_compress_threshold_0(Config) -> |
| 340 |
doc("Confirm that the compress_threshold option can be dynamically " |
| 341 |
"set to change how large response bodies must be to be compressed."), |
| 342 |
{200, Headers, GzBody} = do_get("/reply/set_options_threshold0", |
| 343 |
[{<<"accept-encoding">>, <<"gzip">>}], Config), |
| 344 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 345 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 346 |
_ = zlib:gunzip(GzBody), |
| 347 |
ok. |
| 348 |
|
| 349 |
vary_accept(Config) -> |
| 350 |
doc("Add accept-encoding to vary when the response has a 'vary: accept' header."), |
| 351 |
{200, Headers, _} = do_get("/reply/vary", [ |
| 352 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 353 |
{<<"x-test-vary">>, <<"accept">>} |
| 354 |
], Config), |
| 355 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 356 |
{_, <<"accept, accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 357 |
ok. |
| 358 |
|
| 359 |
vary_accept_accept_encoding(Config) -> |
| 360 |
doc("Don't change the vary value when the response has a 'vary: accept, accept-encoding' header."), |
| 361 |
{200, Headers, _} = do_get("/reply/vary", [ |
| 362 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 363 |
{<<"x-test-vary">>, <<"accept, accept-encoding">>} |
| 364 |
], Config), |
| 365 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 366 |
{_, <<"accept, accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 367 |
ok. |
| 368 |
|
| 369 |
vary_empty(Config) -> |
| 370 |
doc("Add accept-encoding to vary when the response has an empty vary header."), |
| 371 |
{200, Headers, _} = do_get("/reply/vary", [ |
| 372 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 373 |
{<<"x-test-vary">>, <<>>} |
| 374 |
], Config), |
| 375 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 376 |
{_, <<"accept-encoding">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 377 |
ok. |
| 378 |
|
| 379 |
vary_wildcard(Config) -> |
| 380 |
doc("Don't change the vary value when the response has a 'vary: *' header."), |
| 381 |
{200, Headers, _} = do_get("/reply/vary", [ |
| 382 |
{<<"accept-encoding">>, <<"gzip">>}, |
| 383 |
{<<"x-test-vary">>, <<"*">>} |
| 384 |
], Config), |
| 385 |
{_, <<"gzip">>} = lists:keyfind(<<"content-encoding">>, 1, Headers), |
| 386 |
{_, <<"*">>} = lists:keyfind(<<"vary">>, 1, Headers), |
| 387 |
ok. |