Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #116: Static brotli overrides dynamic gzip #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions script/.travis-before-test.sh
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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