HTTP/1.1 response to multiple range

2019-04-05 06:57发布

问题:

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?

回答1:

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".
The multipart/byteranges media type includes two or more parts, each with its own Content-Type and Content-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...