From 9720478098638983e9069afc76f3031407d0ce6e Mon Sep 17 00:00:00 2001 From: Adam Burgess Date: Sun, 11 Apr 2021 12:30:48 +1000 Subject: [PATCH] Fix #116: Static brotli overrides dynamic gzip (cherry picked from commit 8e1f281713462e00360a89e22b07b6351da07078) --- script/.travis-before-test.sh | 3 +++ script/.travis-test.sh | 31 ++++++++++++++++++++++++++ script/test_static.conf | 30 +++++++++++++++++++++++++ static/ngx_http_brotli_static_module.c | 6 +++-- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 script/test_static.conf diff --git a/script/.travis-before-test.sh b/script/.travis-before-test.sh index 9f69164..8f93c0f 100755 --- a/script/.travis-before-test.sh +++ b/script/.travis-before-test.sh @@ -4,6 +4,7 @@ set -ex # Setup shortcuts. ROOT=`pwd` FILES=$ROOT/script/test +BROTLI=$ROOT/deps/brotli/out/brotli # Setup directory structure. cd $ROOT/script @@ -20,5 +21,7 @@ curl --compressed -o $FILES/war-and-peace.txt http://www.gutenberg.org/files/260 echo "Kot lomom kolol slona!" > $FILES/small.txt echo "Kot lomom kolol slona!" > $FILES/small.html +$BROTLI $FILES/small.html + # Restore status-quo. cd $ROOT diff --git a/script/.travis-test.sh b/script/.travis-test.sh index 1db8d15..95e2276 100755 --- a/script/.travis-test.sh +++ b/script/.travis-test.sh @@ -47,6 +47,16 @@ expect_br_equal() { fi } +expect_gz_equal() { + expected=$1 + actual_gz=$2 + if gzip -dfk ./${actual_gz}.gz; then + expect_equal $expected $actual_gz + else + add_result "FAIL (decompression)" + fi +} + ################################################################################ # Start default server. @@ -127,6 +137,27 @@ $NGINX -c $ROOT/script/test.conf -s stop ################################################################################ +echo "Starting brotli_static NGINX" +$NGINX -c $ROOT/script/test_static.conf + +# Run tests. +echo $HR + +echo "Test: static .br files are served" +$CURL -H 'Accept-encoding: gzip, br' -o tmp/static-small.html.br $SERVER/small.html +expect_equal $FILES/small.html.br tmp/static-small.html.br + +echo "Test: dynamic gzip is used when .br doesn't exist" +$CURL -H 'Accept-encoding: gzip, br' -o tmp/static-small.txt.gz $SERVER/small.txt +expect_gz_equal $FILES/small.txt tmp/static-small.txt + +echo $HR +echo "Stopping brotli_static NGINX" +# Stop server. +$NGINX -c $ROOT/script/test_static.conf -s stop + +################################################################################ + # Start default server. echo "Statring h2 NGINX" $NGINX -c $ROOT/script/test_h2.conf diff --git a/script/test_static.conf b/script/test_static.conf new file mode 100644 index 0000000..a571e5e --- /dev/null +++ b/script/test_static.conf @@ -0,0 +1,30 @@ +events { + worker_connections 4; +} + +daemon on; +error_log /dev/stdout info; + +http { + access_log ./access.log; + error_log ./error.log; + + gzip on; + gzip_comp_level 1; + gzip_types text/plain text/css; + + brotli_static on; + + server { + listen 8080 default_server; + listen [::]:8080 default_server; + + root ./; + + index index.html; + + location / { + try_files $uri $uri/ =404; + } + } +} diff --git a/static/ngx_http_brotli_static_module.c b/static/ngx_http_brotli_static_module.c index 8f96177..b1117b0 100644 --- a/static/ngx_http_brotli_static_module.c +++ b/static/ngx_http_brotli_static_module.c @@ -136,8 +136,6 @@ static ngx_int_t check_accept_encoding(ngx_http_request_t* req) { static ngx_int_t check_eligility(ngx_http_request_t* req) { if (req != req->main) return NGX_DECLINED; if (check_accept_encoding(req) != NGX_OK) return NGX_DECLINED; - req->gzip_tested = 1; - req->gzip_ok = 0; return NGX_OK; } @@ -244,6 +242,10 @@ static ngx_int_t handler(ngx_http_request_t* req) { } #endif + /* Prevent gzip from overriding */ + req->gzip_tested = 1; + req->gzip_ok = 0; + /* Prepare request push the body. */ req->root_tested = !req->error_page; rc = ngx_http_discard_request_body(req);