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

remove Transfer-Encoding from Proxy responses #24

Merged
merged 5 commits into from
Oct 23, 2024

Conversation

bentsku
Copy link
Contributor

@bentsku bentsku commented Oct 21, 2024

Motivation

We've had an issue with the Mailhog extension when using HTTP/1.1 (https://github.com/localstack/localstack-extensions/blob/main/mailhog), where the content would get double Transfer-Encoding: chunked header.

This was investigated, and we found out that Twisted would always automatically add the Transfer-Encoding: chunked header value if no content-length is set, even if there's already a value. Werkzeug has the same behavior, but hypercorn does not.

After investigation, PEP3333 (https://peps.python.org/pep-3333/#id34) actually says that a WSGI application should not set the header itself; as it is the responsibility of the web server to manage this. It is true that it felt wrong from the application itself to dictate the server behavior. I believe GZIP is a bit of a different beast, as it relates more to the content itself of the response than the way the server will send it back.

(Note: applications and middleware must not apply any kind of Transfer-Encoding to their output, such as chunking or gzipping; as “hop-by-hop” operations, these encodings are the province of the actual web server/gateway. See Other HTTP Features below, for more details.)

So the more natural fix is to remove the Transfer-Encoding: chunked value from the header returned by the proxy, as the webserver will take care of that. We have specific tests around the automatic adding of this header when the request does not have content-length and have a generator as response.

Changes

  • add tests for Proxy and ProxyHandler behavior around transfer-encoding, asserting that the proxy itself does not have Transfer-Encoding in its response headers to proxy forward, and that the ProxyHandler properly returns chunked response
  • selectively filter the Transfer-Encoding: chunked value out of Transfer-Encoding so that we do not return it, but keep gzip

\cc @lukqw

@bentsku bentsku self-assigned this Oct 21, 2024
@bentsku bentsku requested a review from thrau October 21, 2024 21:34
response = requests.get(proxy.url)

if chunked:
assert response.headers["Transfer-Encoding"] == "chunked"
Copy link
Contributor Author

@bentsku bentsku Oct 22, 2024

Choose a reason for hiding this comment

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

this assertion (checking that the encoding is chunked) is somewhat testing the behavior of Werkzeug instead of the ProxyHandler itself, as it is the decision of Werkzeug to set the Transfer-Encoding to chunked if the data is a generator. I added the test more as documentation around the behavior and to put it to contrast the Proxy behavior itself

Copy link
Member

@thrau thrau 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 catch and 🕵️ work! The added complexity isn't great, but understand why it's needed. Header manipulation has caused us a lot of issues in the past, so let's keep a close eye on any impact it may have!

@thrau thrau merged commit 9cd9dfd into main Oct 23, 2024
4 checks passed
@thrau thrau deleted the fix-proxy-double-transfer-encoding branch October 23, 2024 12:41
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.

2 participants