Skip to content

range request on empty file should return 200 instead of 416 #1937

@sourtin

Description

@sourtin

When attempting to access any range on an empty file, Werkzeug returns a 416 Requested Range Not Satisfiable error. Whilst this is perhaps reasonable, it is inconsistent with behaviour for non-empty files. For example, the range bytes=0-999999 on a small file, say 10 bytes in length, will succeed with a Content-Range: bytes 0-9/10 response header.

The issue is that Content-Range is inclusive, and so ostensibly the only way to succeed for an empty file would be to return the (invalid) response header Content-Range: bytes 0--1/0. As such, Werkzeug's behaviour is technically correct with respect to the specification, but I would argue it is undesirable/unexpected.

Two potential alternative approaches are:

  1. Respond with Content-Range: bytes */0 to indicate an unknown range.
  2. 'Ignore' the range request in this case and return 200 OK.

Using curl -ir 0-0 against an empty file shows that different servers take different approaches. For sourtin.github.io/empty.txt, it gives a 416 error but responds with bytes */0, whereas for Apache/2.4.18 it gives 200 OK and ignores the range (i.e. approach 2).

Adding a special case is in its own way undesirable, but if either approach is acceptable I'm happy to submit an appropriate PR. I'd lean towards approach 2, personally.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions