Implement HTTP 206 content sharding in Node. js (1)
Introduction
In this article, I will elaborate on the HTTP statusPart 1And use Node. js implements it step by step. we will also use an example based on its usage in the most common scenarios to test the code: An HTML5 page that can start playing video files at any point in time.
Partial ContentBrief Introduction
HTTP 206Partial ContentThe status code and its related message headers provide a mechanism for browsers and other user proxies to receive part of the content from the server rather than all the content. this mechanism is widely used in the transmission of video files supported by most browsers and players such as Windows Media Player and VLC Player.
The basic process can be described in the following steps:
Let's take a look at each key message header in these steps.
Accept-Ranges: bytes)
This is a byte header sent by the server to display the content that can be distributed to the browser by Division. This value declares that each range of requests that can be accepted, in most cases the number of bytesBytes.
Range: bytes) = (start)-(end)
This is the message header that the browser informs the server of part of the content range. note that both the start and end positions are included and start from 0. this message header can also be left empty. Its meaning is as follows:
-
If the end position is removed, the server returns the last available byte from the declared start position to the end position of the entire content.
-
If the start position is removed, the end position parameter can be described as the number of bytes that can be returned by the server from the last available bytes.
Content-Range: bytes) = (start)-(end)/(total)
This message header will appear along with HTTP status code 206. the start and end values show the range of the current content. like the Range message header, both values are included and start from scratch. total this value declares the total number of available bytes.
Content-Range: */(total)
The header information is the same as the preceding one, but it is in another format and is sent only when the HTTP status code 416 is returned. The total number represents the total number of bytes available for the body.
Here is an example with 2048 bytes. Note that the difference between the start point and the focus is omitted.
Open requestThe first 1024 bytes
Sent by the browser:
- GET /dota2/techies.mp4 HTTP/1.1
- Host: localhost:8000
- Range: bytes=0-1023
Server return:
- HTTP/1.1 216 Partial Content
- Date: Mon, 15 Sep 2014 22:19:34 GMT
- Content-Type: video/mp4
- Content-Range: bytes 0-1023/2048
- Content-Length: 1024
-
- (Content...)
Request with no endpoint
Sent by the browser:
- GET /dota2/techies.mp4 HTTP/1.1
- Host: localhost:8000
- Range: bytes=1024-
Server return:
- HTTP/1.1 216 Partial Content
- Date: Mon, 15 Sep 2014 22:19:34 GMT
- Content-Type: video/mp4
- Content-Range: bytes 1024-2047/2048
- Content-Length: 1024
-
- (Content...)
Note:The server does not need to return all remaining bytes in a single responseEspecially when the body is too long or there are other performance considerations. The following two examples are acceptable in this case:
- Content-Range: bytes 1024-1535/2048
- Content-Length: 512
The server returns only half of the remaining body. The range of the next request starts from 1,536th bytes.
- Content-Range: bytes 1024-1279/2048
- Content-Length: 256
The server returns only the 256 bytes of the remaining body. The range of the next request starts from 1,280th bytes.
Server return:
- HTTP/1.1 216 Partial Content
- Date: Mon, 15 Sep 2014 22:19:34 GMT
- Content-Type: video/mp4
- Content-Range: bytes 1536-2047/2048
- Content-Length: 512
-
- (Content...)
The range of unavailable requests:
Sent by the browser:
- GET /dota2/techies.mp4 HTTP/1.1
- Host: localhost:8000
- Range: bytes=1024-4096
Server return:
- HTTP/1.1 416 Requested Range Not Satisfiable
- Date: Mon, 15 Sep 2014 22:19:34 GMT
- Content-Range: bytes */2048
After understanding the workflow and header information, we can use Node. js to implement this mechanism.