http/1.1 Range and Content-range

Source: Internet
Author: User
Tags ranges

http/1.1 Range and Content-range (note: reproduced in http://www.91linux.com/html/article/program/cpp/20090411/16420.html)

Suppose you want to develop a multi-threaded download tool, you will naturally think of splitting the file into multiple parts, such as 4 parts, and then create 4 threads, each thread is responsible for downloading a section, if the file size is 403 Byte, then you can split the way: 0-99 (the first 100 bytes), 100-199 (second 100 bytes), 200-299 (third 100 bytes), 300-402 (last 103 bytes).

Split complete, each thread is aware of its own tasks, such as the task of thread 3 is responsible for downloading the 200-299 part of the file, the problem is: Thread 3 sends a request message, can guarantee only the request file 200-299 bytes, and does not interfere with other threads of the task. At this point, we can use the range header of the HTTP1.1. The Range header field can request one or more child ranges of an entity, and a value of range of 0 indicates the first byte, which is that range calculates the number of bytes starting from 0:
Represents the first 500 bytes: range:bytes=0-499
Represents a second 500 byte: range:bytes=500-999
Represents the last 500 bytes: range:bytes=-500
Represents the range after 500 bytes: range:bytes=500-
First and last byte: Range:bytes=0-0,-1
Specify several ranges at the same time: range:bytes=500-600,601-999
Therefore, the request message sent by thread 3 must have this line:
range:bytes=200-299

The server receives a request message for thread 3 and finds that it is a GET request with a range header, and if everything is OK, the server's response message will have the following line:
http/1.1 206 OK
Indicates that the processing request was successful, and that the response message had this line
Content-range:bytes 200-299/403
The 403 followed by the slash represents the size of the file, and the usual content-range usage is:
. The first bytes:
Content-range:bytes 0-499/1234

. The second bytes:
Content-range:bytes 500-999/1234

. All except for the first bytes:
Content-range:bytes 500-1233/1234

. The Last bytes:
Content-range:bytes 734-1233/1234

http/1.1 Range and Content-range

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.