Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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    

[10 bytes FILE CONTENT HERE]
share|improve this question

migrated from webmasters.stackexchange.com Jul 4 at 3:58

This question came from our site for pro webmasters.

1 Answer 1

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!

share|improve this answer
    
no no, thats not what I asked. I meant what if client asked 2000-4000 while file is only 1000 bytes. So server will return 416, but question is, should server write content too ? –  xmen W.K. Jul 4 at 4:28
    
No, you don't send any content in that case. –  Michael Hampton Jul 4 at 4:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.