Skip to content

Fix 416 for suffix Range on zero-length files - #3927

Open
tony1233200 wants to merge 2 commits into
tornadoweb:masterfrom
tony1233200:fix-3926-empty-file-suffix-range
Open

tony1233200 wants to merge 2 commits into
tornadoweb:masterfrom
tony1233200:fix-3926-empty-file-suffix-range

Conversation

@tony1233200

Copy link
Copy Markdown

Fixes #3926.

StaticFileHandler answered Range: bytes=-5 on a zero-length file with 416: the suffix start was clamped to 0, and 0 >= size (0) tripped the unsatisfiable-range check. Per RFC 9110 section 14.1.1 a suffix range longer than the representation is satisfiable (the entire representation is used), so the start >= size check is now skipped for suffix ranges. A request with bytes=-5 on an empty file now gets the same 200 with an empty body as a request with no Range header.

bytes=0- on an empty file still returns 416, and bytes=-0 still returns 416 (zero-length suffix), as before.

Repro (before the fix bytes=-5 returned 416, after it returns 200 with an empty body):

import asyncio, os, tempfile
import tornado.web, tornado.httpclient

async def main():
    d = tempfile.mkdtemp()
    open(os.path.join(d, "empty.txt"), "w").close()
    app = tornado.web.Application([(r"/s/(.*)", tornado.web.StaticFileHandler, {"path": d})])
    server = app.listen(8888, "127.0.0.1")
    client = tornado.httpclient.AsyncHTTPClient()
    for rng in [None, "bytes=-5", "bytes=0-"]:
        r = await client.fetch("http://127.0.0.1:8888/s/empty.txt",
                               headers={"Range": rng} if rng else {}, raise_error=False)
        print(rng, r.code, r.headers.get("Content-Range"))
    server.stop()

asyncio.run(main())

Tests: added test_static_with_range_suffix_on_empty_file (200, empty body, Content-Length: 0, no Content-Range) and test_static_unsatisfiable_range_start_on_empty_file (416 for bytes=0-) next to the existing bytes=-0 test, with a new empty fixture tornado/test/static/empty.txt. All 11 range tests in StaticFileTest pass, as do the full StaticFileTest (40 tests) and httputil_test suites; black and flake8 are clean.

StaticFileHandler rejected a non-zero suffix range (e.g. bytes=-5)
on a zero-length file with 416 because the clamped start (0) was
equal to the size (0). Per RFC 9110 14.1.1 a suffix range longer
than the representation is satisfiable, so skip the start >= size
check for suffix ranges and return the whole (empty) representation
with 200. Fixes tornadoweb#3926.
@bdarnell

Copy link
Copy Markdown
Member

The new tests (and mypy) are failing in CI

@tony1233200

Copy link
Copy Markdown
Author

Thanks for flagging this — both failures traced to the same oversight:

  • The two new tests 404'd because the new empty.txt fixture wasn't listed in setup.py package_data or MANIFEST.in, so the installed package in CI didn't ship it and the range logic never ran. Added it to both lists (per the comment in setup.py that data files must be listed in both places).
  • The mypy error at web.py:3137 was the boolean-alias narrowing issue: is_suffix_range doesn't narrow start, so start += size saw int | None. The suffix adjustment now uses a direct start is not None and start < 0 check; is_suffix_range is still computed before it for the later start >= size test.

Both fixes are pushed in a3e5ef6; CI should be green now.

This branch has not been deployed

No deployments
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.

StaticFileHandler answers 416 to a non-zero suffix Range on a zero-length file

2 participants