fix(routing): implement proper HEAD support for static routes#2674
Open
zain-asif-dev wants to merge 2 commits into
Open
fix(routing): implement proper HEAD support for static routes#2674zain-asif-dev wants to merge 2 commits into
zain-asif-dev wants to merge 2 commits into
Conversation
Static routes previously handled a HEAD request the same way as GET: they would open the underlying file (via io.open()/os.fstat()) and set it as the response stream, even though a HEAD response never includes a body. Since the body is discarded later in the response-handling pipeline without ever being iterated, the opened file handle was never closed, leaking a file descriptor on every HEAD request to a static route. HEAD requests are now served directly from the file's stat() result, without opening a file handle, while still returning the same headers (Content-Length, ETag, Last-Modified, Accept-Ranges, and Content-Range for ranged requests) that the equivalent GET would have produced. As a bonus, static routes now reject methods other than GET, HEAD, and OPTIONS with 405 Method Not Allowed, instead of serving the file's contents regardless of method. The Allow/Access-Control-Allow-Methods header returned for OPTIONS requests now also correctly advertises "GET, HEAD" rather than just "GET". Closes falconry#2337 Signed-off-by: Zain Asif <zainasif.jutt1@gmail.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2674 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 64 64
Lines 7985 8026 +41
Branches 1103 1113 +10
=========================================
+ Hits 7985 8026 +41 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Signed-off-by: Zain Asif <zainasif.jutt1@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #2337.
Static routes (
StaticRoute/StaticRouteAsync, as configured viaApp.add_static_route()) didn't have properHEADsupport:HEADrequest was handled exactly likeGET: the responder always opened the underlying file viaio.open()/os.fstat()and set it asresp.stream, even though aHEADresponse never includes a body. Since the body is discarded later in the response pipeline (resp.streamis never iterated forHEAD), the opened file handle was never closed — everyHEADrequest to a static route leaked a file descriptor.POST,PUT,DELETE, etc.) was also accepted and served the file's contents, instead of being rejected.Allow/Access-Control-Allow-Methodsheader returned forOPTIONSrequests only advertisedGET.Changes
HEADrequests are now served fromos.stat()directly, without ever opening a file handle, while still setting the same headers aGETwould (Content-Length,ETag,Last-Modified,Accept-Ranges, andContent-Range/206for ranged requests)._compute_head_range(), which mirrors the offset/length arithmetic of the existing_set_range()helper, but operates purely on the file size (no file handle required).GET,HEAD, andOPTIONSnow get a405 Method Not Allowedresponse (viafalcon.HTTPMethodNotAllowed(['GET', 'HEAD'])) instead of silently being served the file.OPTIONSresponder now setsAllow: GET, HEADinstead ofAllow: GET.fallback_filename" logic into a small_locate()helper shared between theGETandHEADcode paths, to keep__call__()'s complexity in check.Test plan
patch_opentest fixture intests/test_static.pyto also fakeos.stat()(used by the newHEADcode path), in addition to the existingio.open()/os.fstat()faking used byGET.tests/test_static.py:test_head_request: basicHEADreturns200with correct headers and no body, and never opens a file handle.test_head_request_not_found:HEADfor a missing file returns404.test_head_request_not_modified:HEADhonorsIf-None-Match/ETag and returns304.test_head_request_range(parametrized):HEADwith aRangeheader returns the correctContent-Range/Content-Length/206(or200when no range), with an empty body.test_head_request_range_not_satisfiable:HEADwith an out-of-boundsRangereturns416.test_method_not_allowed(parametrized overPOST/PUT/PATCH/DELETE): returns405withAllow: GET, HEAD.test_options_request(intests/test_static.py) andtest_enabled_cors_static_route(intests/test_cors_middleware.py) to expect the now-correctGET, HEADAllow header.pytest tests/): 3977 passed, 458 skipped, with only one pre-existing failure (test_select_subprotocol_known, an unrelated flaky uvicorn WebSocket test) reproducible onmasteras well.ruff check,ruff format --diff, andmypyall pass clean on the modified files.towncriernews fragment (docs/_newsfragments/2337.bugfix.rst) and verified it renders correctly viatowncrier build --draft.