Implement HTTP 206 content sharding in Node. js (1)

Source: Internet
Author: User

Implement HTTP 206 content sharding in Node. js (1)

  • Download PartialContent.zip-2 KB

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:

 
 
  1. GET /dota2/techies.mp4 HTTP/1.1  
  2. Host: localhost:8000  
  3. Range: bytes=0-1023 

Server return:

 
 
  1. HTTP/1.1 216 Partial Content  
  2. Date: Mon, 15 Sep 2014 22:19:34 GMT  
  3. Content-Type: video/mp4  
  4. Content-Range: bytes 0-1023/2048  
  5. Content-Length: 1024  
  6.    
  7. (Content...) 

Request with no endpoint

Sent by the browser:

 
 
  1. GET /dota2/techies.mp4 HTTP/1.1  
  2. Host: localhost:8000  
  3. Range: bytes=1024- 

Server return:

 
 
  1. HTTP/1.1 216 Partial Content  
  2. Date: Mon, 15 Sep 2014 22:19:34 GMT  
  3. Content-Type: video/mp4  
  4. Content-Range: bytes 1024-2047/2048  
  5. Content-Length: 1024  
  6.    
  7. (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:

 
 
  1. Content-Range: bytes 1024-1535/2048  
  2. Content-Length: 512 

The server returns only half of the remaining body. The range of the next request starts from 1,536th bytes.

 
 
  1. Content-Range: bytes 1024-1279/2048  
  2. 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:

 
 
  1. HTTP/1.1 216 Partial Content  
  2. Date: Mon, 15 Sep 2014 22:19:34 GMT  
  3. Content-Type: video/mp4  
  4. Content-Range: bytes 1536-2047/2048  
  5. Content-Length: 512  
  6.    
  7. (Content...) 

The range of unavailable requests:

Sent by the browser:

 
 
  1. GET /dota2/techies.mp4 HTTP/1.1  
  2. Host: localhost:8000  
  3. Range: bytes=1024-4096 

Server return:

 
 
  1. HTTP/1.1 416 Requested Range Not Satisfiable  
  2. Date: Mon, 15 Sep 2014 22:19:34 GMT  
  3. Content-Range: bytes */2048 

After understanding the workflow and header information, we can use Node. js to implement this mechanism.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.