Skip to content

Commit

Permalink
Merge pull request postmanlabs#254 from Lukasa/brotli
Browse files Browse the repository at this point in the history
Add a Brotli endpoint.
  • Loading branch information
kennethreitz authored Mar 17, 2017
2 parents 341ddcc + c916b21 commit 36c6818
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
9 changes: 9 additions & 0 deletions httpbin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ def view_deflate_encoded_content():
'origin', 'headers', method=request.method, deflated=True))


@app.route('/brotli')
@filters.brotli
def view_brotli_encoded_content():
"""Returns Brotli-Encoded Data."""

return jsonify(get_dict(
'origin', 'headers', method=request.method, brotli=True))


@app.route('/redirect/<int:n>')
def redirect_n_times(n):
"""302 Redirects n times."""
Expand Down
25 changes: 25 additions & 0 deletions httpbin/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import gzip as gzip2
import zlib

import brotli as _brotli

from six import BytesIO
from decimal import Decimal
from time import time as now
Expand Down Expand Up @@ -88,3 +90,26 @@ def deflate(f, *args, **kwargs):
return data

return deflated_data


@decorator
def brotli(f, *args, **kwargs):
"""Brotli Flask Response Decorator"""

data = f(*args, **kwargs)

if isinstance(data, Response):
content = data.data
else:
content = data

deflated_data = _brotli.compress(content)

if isinstance(data, Response):
data.data = deflated_data
data.headers['Content-Encoding'] = 'br'
data.headers['Content-Length'] = str(len(data.data))

return data

return deflated_data
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
],
packages=find_packages(),
include_package_data = True, # include files listed in MANIFEST.in
install_requires=['Flask','MarkupSafe','decorator','itsdangerous','six'],
install_requires=[
'Flask','MarkupSafe','decorator','itsdangerous','six','brotlipy'
],
)
4 changes: 4 additions & 0 deletions test_httpbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@ def test_gzip(self):
response = self.app.get('/gzip')
self.assertEqual(response.status_code, 200)

def test_brotli(self):
response = self.app.get('/brotli')
self.assertEqual(response.status_code, 200)

def test_digest_auth_with_wrong_password(self):
auth_header = 'Digest username="user",realm="wrong",nonce="wrong",uri="/digest-auth/user/passwd/MD5",response="wrong",opaque="wrong"'
response = self.app.get(
Expand Down

0 comments on commit 36c6818

Please sign in to comment.