Skip to content

Commit

Permalink
Fix google#116: Static brotli overrides dynamic gzip
Browse files Browse the repository at this point in the history
(cherry picked from commit 8e1f281)
  • Loading branch information
adamburgess authored and andrerom committed Nov 21, 2022
1 parent 6e975bc commit 9720478
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
3 changes: 3 additions & 0 deletions script/.travis-before-test.sh
Expand Up @@ -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
Expand All @@ -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 "<html>Kot lomom kolol slona!</html>" > $FILES/small.html

$BROTLI $FILES/small.html

# Restore status-quo.
cd $ROOT
31 changes: 31 additions & 0 deletions script/.travis-test.sh
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions 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;
}
}
}
6 changes: 4 additions & 2 deletions static/ngx_http_brotli_static_module.c
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9720478

Please sign in to comment.