Bug: compressed routes do not send Vary: Accept-Encoding
Expected Behaviour
When compression is enabled for a route (@app.get(..., compress=True) or Response(compress=True)), the response body depends on the request's Accept-Encoding, so the response should carry Vary: Accept-Encoding. That tells API Gateway caching, CloudFront and other shared caches to keep gzipped and plain responses apart. This is the same reasoning as the Vary: Origin that CORS responses got in #8638.
Current Behaviour
ResponseBuilder gzips the body and sets Content-Encoding: gzip, but never adds Accept-Encoding to Vary, neither on the compressed response nor on the plain one served to a client without gzip. A cache keyed only on the URL can store the gzipped body and serve it to a client that did not ask for gzip, or keep serving the plain body to everyone.
Code snippet
from aws_lambda_powertools.event_handler import ApiGatewayResolver, Response, content_types
app = ApiGatewayResolver()
@app.get("/report", compress=True)
def report():
return Response(200, content_types.TEXT_PLAIN, "Foo")
result = app({"path": "/report", "httpMethod": "GET", "headers": {"Accept-Encoding": "gzip"}}, None)
result["multiValueHeaders"]
# {'Content-Type': ['text/plain'], 'Content-Encoding': ['gzip']} no Vary
Steps to Reproduce
- Define a route with
compress=True. - Call it with and without
Accept-Encoding: gzip. - Neither response has a
Varyheader.
Possible Solution
When compression is enabled for the route or the response, add Accept-Encoding to Vary, keeping any Vary value the route already set (as the CORS code does for Origin). I can open a PR.
Powertools for AWS Lambda (Python) version
latest (develop, 3.38.0)
AWS Lambda function runtime
3.12
Packaging format used
PyPi
Debugging logs
No response
Thanks @anishmehta24, I agree, same reasoning we used for Vary: Origin in #8638. Reviewing #8711. Goes into 3.40.0.
[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
This is now released under 3.40.0 version!