From 18bfcc6e84007df36b7ceaf0ba0172f12b924f75 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 --- script/.travis-before-test.sh | 3 +++ script/.travis-test.sh | 31 ++++++++++++++++++++++++++ static/ngx_http_brotli_static_module.c | 6 +++-- 3 files changed, 38 insertions(+), 2 deletions(-) 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/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);