Permalink
Browse files

Add pre-gzip support

Serve pre-gzipped content of requested file if present on disk with
appropriate headers
  • Loading branch information...
hdickson
hdickson committed Sep 21, 2014
1 parent cdf2231 commit 5f36d03fa35b09972552e0c1e7fcf226387bda42
@@ -16,7 +16,7 @@ test-python:
. .virts/$(pyversion)/bin/activate && pip install -e '.' > /dev/null
. .virts/$(pyversion)/bin/activate && nosetests tests/
stylecheck:
. .virts/dev/bin/activate && flake8 static tests --max-complexity=12
. .virts/dev/bin/activate && flake8 static tests --max-complexity=14
test:
. .virts/dev/bin/activate && nosetests -xs --with-coverage --cover-package static --cover-html --cover-html-dir coverage-html tests/
viewcoverage:
@@ -100,6 +100,10 @@ def __call__(self, environ, start_response):
return self.moved_permanently(environ, start_response, headers)
else:
full_path = self._full_path(path_info + self.index_file)
prezipped = ('gzip' in environ['HTTP_ACCEPT_ENCODING']
and path.exists(full_path + '.gz'))
if prezipped:
full_path += '.gz'
content_type = self._guess_type(full_path)
try:
etag, last_modified = self._conditions(full_path, environ)
@@ -115,6 +119,9 @@ def __call__(self, environ, start_response):
return self.not_modified(environ, start_response, headers)
file_like = self._file_like(full_path)
headers.append(('Content-Type', content_type))
if prezipped:
headers.extend([('Content-Encoding', 'gzip'),
('Vary', 'Accept-Encoding')])
start_response("200 OK", headers)
if environ['REQUEST_METHOD'] == 'GET':
return self._body(full_path, environ, file_like)
@@ -0,0 +1 @@
nogzipversion
@@ -0,0 +1 @@
static
Binary file not shown.
@@ -210,3 +210,29 @@ def test_client_gets_a_404_for_a_missing_file(self):
self.assert_response(
'GET', '/no-such-file.txt', {},
404)


class StaticClingWithPrezipping(Intercepted):

def get_app(self):
return static.Cling('tests/data/prezip')

def test_client_gets_prezipped_content(self):
self.assert_response(
'GET', '/static.txt', {'Accept-Encoding': 'gzip, deflate'},
200,
response_headers={'Content-Encoding': 'gzip'},
file_content="tests/data/prezip/static.txt.gz")

def test_client_gets_non_prezipped_when_no_accept_encoding_present(self):
self.assert_response(
'GET', '/static.txt', {},
200,
file_content="tests/data/prezip/static.txt")

def test_client_gets_non_prezipped_when_prezipped_file_not_exist(self):
self.assert_response(
'GET', '/nogzipversionpresent.txt',
{'Accept-Encoding': 'gzip, deflate'},
200,
file_content="tests/data/prezip/nogzipversionpresent.txt")

0 comments on commit 5f36d03

Please sign in to comment.