Skip to content

Commit

Permalink
fixed "Too much data for declared Content-Length" message (#63)
Browse files Browse the repository at this point in the history
* fixed "Too much data for declared Content-Length" message

* lint

* .
  • Loading branch information
bentheiii authored Sep 1, 2022
1 parent e507eb0 commit 641f89a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* sending unknown context features in a query is no longer an error
* httpx is now a dev-dependency
* Docker container now runs with a limited user
### Fixed
* returning 304 no longer logs an error "Too much data for declared Content-Length"
## 0.5.1
### Added
* Added a generic handler for unhandled errors
Expand Down
26 changes: 25 additions & 1 deletion heksher/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from logging import getLogger

from fastapi import HTTPException
from fastapi.exception_handlers import http_exception_handler
from starlette.responses import JSONResponse
from starlette.datastructures import MutableHeaders
from starlette.requests import Request
from starlette.responses import JSONResponse, Response

from heksher._version import __version__
from heksher.api.v1 import router as v1_router
Expand Down Expand Up @@ -42,6 +45,27 @@ async def handle_exception(request, exc):
return await http_exception_handler(request, exc)


@app.exception_handler(HTTPException)
async def ret(request: Request, exc: HTTPException) -> Response:
# fastapi's default event handler is bugged (https://github.com/tiangolo/fastapi/issues/4946)
headers = getattr(exc, "headers", None)
if headers:
headers = MutableHeaders(headers)
del headers['content-length']
del headers['content-type']
if exc.status_code in {204, 304}:
if headers:
return Response(status_code=exc.status_code, headers=headers)
else:
return Response(status_code=exc.status_code)
if headers:
return JSONResponse(
{"detail": exc.detail}, status_code=exc.status_code, headers=headers
)
else:
return JSONResponse({"detail": exc.detail}, status_code=exc.status_code)


def main():
import uvicorn

Expand Down
4 changes: 2 additions & 2 deletions tests/blackbox/app/test_v1api_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ async def test_query_etag(app_client, setup_rules, metadata):

assert repeat_resp.status_code == 304
assert repeat_resp.headers['ETag'] == etag
assert repeat_resp.content == b'{"detail":"Not Modified"}'
assert not repeat_resp.content


@mark.asyncio
Expand Down Expand Up @@ -284,7 +284,7 @@ async def test_query_etag_wildcard(app_client, setup_rules, metadata):
}, headers={'If-None-Match': '*'})

assert repeat_resp.status_code == 304
assert repeat_resp.content == b'{"detail":"Not Modified"}'
assert not repeat_resp.content


@mark.asyncio
Expand Down

0 comments on commit 641f89a

Please sign in to comment.