What is expected behaviour from Server returning Http 416

xmen asked:

I have a server and sometimes when client ask for a file and if its request header Range may get over the length of file, so the server returns 416 status code. But I’m wondering should it write file content too ? or simply return 416 with no content ?

Explaination on W3 Website doesn’t clearify enough.

What I understood from the W3 web, that it should write Content-Range header(starting with 0?).
So if content length is 10 bytes. The response should look like

HTTP/1.1 416
Content-Range: bytes 0-9/10
Content-Length: 10    


My answer:

You should be serving a 206 in this case.

RFC 7233 makes it clear that if any part of the requested range is satisfiable then that is what you send back, even if part of the range is not satisfiable.

From section 2.1:

If a valid byte-range-set includes at least one byte-range-spec with a first-byte-pos that is less than the current length of the representation, or at least one suffix-byte-range-spec with a non-zero suffix-length, then the byte-range-set is satisfiable. Otherwise, the byte-range-set is unsatisfiable.

So if your file is 1000 bytes, and the client requests bytes=501-2000, you will return a 206 and serve bytes 501-1000. (And remember that ranges start from zero, so watch out for off-by-one errors.)

In the case where no part of the range is satisfiable, you send a 416 error, but you do not send any file content. The whole point of the error is that there is nothing to send!

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.