While writing my HTTP/1.1 server, I get stuck dealing multiple ranges request.
Section 14.35.1 of RFC 2616 refers some examples but doesn't clarify server behaviour. For instance:
GET /some/resource HTTP/1.1
...
Range: bytes=200-400,100-300,500-600
...
Should I return this exact sequence of bytes?
Or should I merge all ranges, sending 100-400,500-600
?
Or sending all in between, 100-600
?
Worst, when checking Content-Range
response header (Section 14.16), only a single range may be returned, so I wonder how would a server response to example in Section 14.35.1 bytes=0-0,-1
!!!
How should my server handle such requests?
Partial request responsesA range request that is out of bounds will result in a 416 Requested Range Not Satisfiable status, meaning that none of the range values overlap the extent of the resource. For example, the first-byte-pos of every range might be greater than the resource length.
They can be divided in several groups: General headers, like Via , apply to the message as a whole. Request headers, like User-Agent or Accept , modify the request by specifying it further (like Accept-Language ), by giving context (like Referer ), or by conditionally restricting it (like If-None ).
The Accept-Ranges HTTP response header is a marker used by the server to advertise its support for partial requests from the client for file downloads. The value of this field indicates the unit that can be used to define a range.
The Content-Length header is mandatory for messages with entity bodies, unless the message is transported using chunked encoding. Content-Length is needed to detect premature message truncation when servers crash and to properly segment messages that share a persistent connection.
I just had a look at how other servers that support the Range
header field might respond and did a quick curl
to example.com:
~# curl -s -D - -H "Range: bytes=100-200, 300-400" http://www.example.com
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5
Content-Length: 385
Server: ECS (fll/0761)
--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 100-200/1270
eta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="vieport" content
--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 300-400/1270
-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: "Open Sans", "Helvetica
--3d6b6a416f9b5--
Apparently, what your looking for is the Content-Type: multipart/byteranges; boundary
response header. Googling exactly that turned up a W3C document with appendices to RFC 2616
When an HTTP 206 (Partial Content) response message includes the content of multiple ranges (a response to a request for multiple non-overlapping ranges), these are transmitted as a multipart message-body. The media type for this purpose is called "
multipart/byteranges
".
Themultipart/byteranges
media type includes two or more parts, each with its ownContent-Type
andContent-Range
fields. The required boundary parameter specifies the boundary string used to separate each body-part.
So there you go.
By the way, the server at example.com does not check for overlapping byte ranges and sends you exactly the ranges that you requested...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With