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 support for zstd decoding #3139

Merged
merged 1 commit into from Mar 21, 2024
Merged

Add support for zstd decoding #3139

merged 1 commit into from Mar 21, 2024

Conversation

mbeijen
Copy link
Contributor

@mbeijen mbeijen commented Mar 10, 2024

Summary

This adds support for zstd decoding using the python package zstandard. This is similar to how it is implemented in urllib3. I also chose the optional installation option httpx[zstd] to mimic the same option in urllib3.

zstd decoding is similar to brotli, but in benchmarks it is supposed to be even faster. The zstd compression is described in RFC 8878.

See #1986

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@mbeijen mbeijen force-pushed the zstd branch 2 times, most recently from 835d74a to 01b4c6d Compare March 10, 2024 10:15
httpx/_decoders.py Outdated Show resolved Hide resolved
httpx/_decoders.py Outdated Show resolved Hide resolved
httpx/_compat.py Outdated Show resolved Hide resolved
@mbeijen mbeijen force-pushed the zstd branch 3 times, most recently from 09b31a1 to 4860886 Compare March 10, 2024 10:37
Copy link
Contributor

@Zaczero Zaczero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like those changes!

docs/index.md Outdated Show resolved Hide resolved
@mbeijen mbeijen force-pushed the zstd branch 2 times, most recently from c531b62 to 4f2ed51 Compare March 10, 2024 14:13
@tomchristie
Copy link
Member

This is a lovely bit of functionality. Missing some coverage at the moment.

@mbeijen
Copy link
Contributor Author

mbeijen commented Mar 14, 2024

This is a lovely bit of functionality. Missing some coverage at the moment.

Thanks. I will see if I can add tests for full coverage

@mbeijen mbeijen force-pushed the zstd branch 2 times, most recently from 10f21ba to 96bde3f Compare March 15, 2024 07:32
@mbeijen
Copy link
Contributor Author

mbeijen commented Mar 15, 2024

I've added full test coverage now, again by borrowing a test inspired from urllib3
@tomchristie please consider my PR again

@tomchristie
Copy link
Member

please consider my PR again

Sure thing. (thanks)

  • Are we crediting the urllib3 team sufficiently/appropriately here, perhaps a code comment in places where we're drawing from their work?
  • Could you provide an example URL in this thread to test against?
  • Let's get a CHANGELOG.md entry added.

@mbeijen
Copy link
Contributor Author

mbeijen commented Mar 19, 2024

  • Are we crediting the urllib3 team sufficiently/appropriately here, perhaps a code comment in places where we're drawing from their work?

I've added a code comment in the appropriate places. In the docs and README there is already a credit given to urllib3 as a source for much of the lower level details, this is not much different. It is fair to add some extra credit here as well as a pointer for a reader to check the urllib3 implementatition in case of changes.

  • Could you provide an example URL in this thread to test against?

Sure! Support for zstd is not really common on web sites yet. libcurl has support for some years now. Chrome has it now enabled by default so I will assume this might help? Also, Mozilla changed its stance on zstd from 'defer' to 'positive' in October, ref: mozilla/standards-positions#775 (comment) -- it is particularly well-suited for compressing JSON data.

Sites that support it include instagram.com, facebook.com, caddyserver.com

httpx -v --http2 https://caddyserver.com/  | grep encoding:
accept-encoding: gzip, deflate, br, zstd
content-encoding: zstd
  • Let's get a CHANGELOG.md entry added.

I've added an entry.

This adds support for zstd decoding using the python package zstandard.
This is similar to how it is implemented in urllib3. I also chose the
optional installation option httpx[zstd] to mimic the same option in
urllib3.

zstd decoding is similar to brotli, but in benchmarks it is supposed to
be even faster. The zstd compression is described in RFC 8878.

See encode#1986

Co-authored-by: Kamil Monicz <kamil@monicz.dev>
Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice work ☺️

$ httpx -v https://caddyserver.com
* Connecting to 'caddyserver.com'
* Connected to '165.227.20.207' on port 443
* SSL established using 'TLSv1.3' / 'TLS_AES_128_GCM_SHA256'
* Selected ALPN protocol: 'http/1.1'
* Server certificate:
*   subject:
*     commonName: 'caddyserver.com'
*   issuer:
*     countryName: 'US'
*     organizationName: "Let's Encrypt"
*     commonName: 'R3'
*   version: 3
*   serialNumber: '0334F48787F76D10C8FF7D436F4AB8C56B0F'
*   notBefore: 'Feb 26 23:44:40 2024 GMT'
*   notAfter: 'May 26 23:44:39 2024 GMT'
*   subjectAltName:
*     DNS: 'caddyserver.com'
*   OCSP:
*     'http://r3.o.lencr.org'
*   caIssuers:
*     'http://r3.i.lencr.org/'
GET / HTTP/1.1
Host: caddyserver.com
Accept: */*
Accept-Encoding: gzip, deflate, br, zstd
Connection: keep-alive
User-Agent: python-httpx/0.27.0

HTTP/1.1 200 OK
Alt-Svc: h3=":443"; ma=2592000
Content-Encoding: zstd
Content-Type: text/html; charset=utf-8
Server: Caddy
Vary: Accept-Encoding
Date: Thu, 21 Mar 2024 10:12:38 GMT
Transfer-Encoding: chunked

<!DOCTYPE html>
<html>
    <head>
        <title>Caddy - The Ultimate Server with Automatic HTTPS</title>

@tomchristie tomchristie merged commit 392dbe4 into encode:master Mar 21, 2024
5 checks passed
shepilov-vladislav pushed a commit to shepilov-vladislav/httpx that referenced this pull request Mar 28, 2024
This adds support for zstd decoding using the python package zstandard.
This is similar to how it is implemented in urllib3. I also chose the
optional installation option httpx[zstd] to mimic the same option in
urllib3.

zstd decoding is similar to brotli, but in benchmarks it is supposed to
be even faster. The zstd compression is described in RFC 8878.

See encode#1986

Co-authored-by: Kamil Monicz <kamil@monicz.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants