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

Add client Brotli support #5227

Merged
merged 8 commits into from Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/5219.feature
@@ -0,0 +1 @@
Add client brotli compression support (optional with runtime check)
7 changes: 6 additions & 1 deletion aiohttp/client_reqrep.py
Expand Up @@ -49,6 +49,7 @@
set_result,
)
from .http import SERVER_SOFTWARE, HttpVersion10, HttpVersion11, StreamWriter
from .http_parser import HAS_BROTLI
from .log import client_logger
from .streams import StreamReader
from .typedefs import (
Expand Down Expand Up @@ -81,6 +82,10 @@
from .tracing import Trace


def _gen_default_accept_encoding() -> str:
return "gzip, deflate, br" if HAS_BROTLI else "gzip, deflate"


@attr.s(auto_attribs=True, frozen=True, slots=True)
class ContentDisposition:
type: Optional[str]
Expand Down Expand Up @@ -165,7 +170,7 @@ class ClientRequest:

DEFAULT_HEADERS = {
hdrs.ACCEPT: "*/*",
hdrs.ACCEPT_ENCODING: "gzip, deflate",
hdrs.ACCEPT_ENCODING: _gen_default_accept_encoding(),
}

body = b""
Expand Down
8 changes: 8 additions & 0 deletions docs/glossary.rst
Expand Up @@ -27,6 +27,14 @@

https://pypi.python.org/pypi/asyncio/

brotlipy
derlih marked this conversation as resolved.
Show resolved Hide resolved

This library contains Python bindings for the reference Brotli
encoder/decoder. This allows Python software to use the Brotli
compression algorithm directly from Python code.

https://pypi.org/project/brotlipy/

callable

Any object that can be called. Use :func:`callable` to check
Expand Down
6 changes: 6 additions & 0 deletions docs/index.rst
Expand Up @@ -168,6 +168,12 @@ Dependencies

$ pip install aiodns

- *Optional* :term:`brotlipy` for Brotli client compression support.

.. code-block:: bash

$ pip install brotlipy


Communication channels
======================
Expand Down
1 change: 1 addition & 0 deletions requirements/test.txt
@@ -1,5 +1,6 @@

-r base.txt
brotlipy==0.7.0
coverage==5.3
cryptography==3.2.1; platform_machine!="i686" and python_version<"3.9" # no 32-bit wheels; no python 3.9 wheels yet
freezegun==1.0.0
Expand Down
21 changes: 19 additions & 2 deletions tests/test_client_request.py
Expand Up @@ -12,7 +12,12 @@

import aiohttp
from aiohttp import BaseConnector, hdrs, payload
from aiohttp.client_reqrep import ClientRequest, ClientResponse, Fingerprint
from aiohttp.client_reqrep import (
ClientRequest,
ClientResponse,
Fingerprint,
_gen_default_accept_encoding,
)
from aiohttp.test_utils import make_mocked_coro


Expand Down Expand Up @@ -304,7 +309,7 @@ def test_headers(make_request) -> None:

assert "CONTENT-TYPE" in req.headers
assert req.headers["CONTENT-TYPE"] == "text/plain"
assert req.headers["ACCEPT-ENCODING"] == "gzip, deflate"
assert req.headers["ACCEPT-ENCODING"] == "gzip, deflate, br"


def test_headers_list(make_request) -> None:
Expand Down Expand Up @@ -1188,3 +1193,15 @@ def test_loose_cookies_types(loop) -> None:

for loose_cookies_type in accepted_types:
req.update_cookies(cookies=loose_cookies_type)


@pytest.mark.parametrize(
"has_brotli,expected",
[
(False, "gzip, deflate"),
(True, "gzip, deflate, br"),
],
)
def test_gen_default_accept_encoding(has_brotli, expected) -> None:
with mock.patch("aiohttp.client_reqrep.HAS_BROTLI", has_brotli):
assert _gen_default_accept_encoding() == expected
62 changes: 18 additions & 44 deletions tests/test_web_functional.py
Expand Up @@ -11,6 +11,7 @@
from yarl import URL

import aiohttp
import brotli
from aiohttp import FormData, HttpVersion10, HttpVersion11, TraceConfig, multipart, web
from aiohttp.hdrs import CONTENT_LENGTH, CONTENT_TYPE, TRANSFER_ENCODING
from aiohttp.test_utils import make_mocked_coro
Expand Down Expand Up @@ -897,49 +898,23 @@ async def handler(request):
assert resp_data == b"test"


async def test_response_with_precompressed_body_gzip(aiohttp_client) -> None:
async def handler(request):
headers = {"Content-Encoding": "gzip"}
zcomp = zlib.compressobj(wbits=16 + zlib.MAX_WBITS)
data = zcomp.compress(b"mydata") + zcomp.flush()
return web.Response(body=data, headers=headers)

app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)

resp = await client.get("/")
assert 200 == resp.status
data = await resp.read()
assert b"mydata" == data
assert resp.headers.get("Content-Encoding") == "gzip"


async def test_response_with_precompressed_body_deflate(aiohttp_client) -> None:
async def handler(request):
headers = {"Content-Encoding": "deflate"}
zcomp = zlib.compressobj(wbits=zlib.MAX_WBITS)
data = zcomp.compress(b"mydata") + zcomp.flush()
return web.Response(body=data, headers=headers)

app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)

resp = await client.get("/")
assert 200 == resp.status
data = await resp.read()
assert b"mydata" == data
assert resp.headers.get("Content-Encoding") == "deflate"


async def test_response_with_precompressed_body_deflate_no_hdrs(aiohttp_client) -> None:
async def handler(request):
headers = {"Content-Encoding": "deflate"}
@pytest.mark.parametrize(
"compressor,encoding",
[
(zlib.compressobj(wbits=16 + zlib.MAX_WBITS), "gzip"),
(brotli.Compressor(), "br"),
(zlib.compressobj(wbits=zlib.MAX_WBITS), "deflate"),
# Actually, wrong compression format, but
# should be supported for some legacy cases.
zcomp = zlib.compressobj(wbits=-zlib.MAX_WBITS)
data = zcomp.compress(b"mydata") + zcomp.flush()
(zlib.compressobj(wbits=-zlib.MAX_WBITS), "deflate"),
],
)
async def test_response_with_precompressed_body(
aiohttp_client, compressor, encoding
) -> None:
async def handler(request):
headers = {"Content-Encoding": encoding}
data = compressor.compress(b"mydata") + compressor.flush()
return web.Response(body=data, headers=headers)

app = web.Application()
Expand All @@ -950,7 +925,7 @@ async def handler(request):
assert 200 == resp.status
data = await resp.read()
assert b"mydata" == data
assert resp.headers.get("Content-Encoding") == "deflate"
assert resp.headers.get("Content-Encoding") == encoding


async def test_bad_request_payload(aiohttp_client) -> None:
Expand Down Expand Up @@ -1883,8 +1858,7 @@ async def handler(request):


@pytest.mark.parametrize(
"status",
[101, 204],
"status", [101, 204],
)
async def test_response_101_204_no_content_length_http11(
status, aiohttp_client
Expand Down