Testing HTTP Status: 206 Partial Content and Range Requests
from http://www.cyberciti.biz/cloud-computing/http-status-code-206-commad-line-test/
The HTTP 2xx class of status codes indicates the action requested by the client was received, and processed successfully. HTTP/1.1 200 OK is the standard response for successful HTTP requests. When you type www.cyberciti.biz in the browser you will get this status code. The HTTP/1.1 206 status code allows the client to grab only part of the resource by sending a range header. This is useful for:
- Understanding http headers and protocol.
- Troubleshooting network problems.
- Troubleshooting large download problems.
- Troubleshooting CDN and origin HTTP server problems.
- Test resuming interrupted downloads using tools like lftp or wget or telnet.
- Test and split a large file size into multiple simultaneous streams i.e. download a large file in parts.
Finding out if HTTP 206 is supported or not by the remote server
You need to find file size and whether remote server support HTTP 206 requests or not. Use the curl command to see HTTP header for any resources. Type the following curl command and send a HEAD request for the url:
$ curl -I http://s0.cyberciti.org/images/misc/static/2012/11/ifdata-welcome-0.png
Sample outputs:
HTTP/1.0 200 OK
Content-Type: image/png
Content-Length: 36907
Connection: keep-alive
Server: nginx
Date: Wed, 07 Nov 2012 00:44:47 GMT
X-Whom: l3-com-cyber
Cache-Control: public, max-age=432000000
Expires: Fri, 17 Jul 2026 00:44:46 GMT
Accept-Ranges: bytes
ETag: "278099835"
Last-Modified: Mon, 05 Nov 2012 23:06:34 GMT
Age: 298127
The following two headers gives out information about this image file:
- Accept-Ranges: bytes - The Accept-Ranges header indicate that the server accept range requests for a resource. The unit used by the remote web servers is in bytes. This header tell us that either server support download resume or downloading files in smaller parts simultaneously so that download manager applications can speed up download for you. The Accept-Ranges: none response header indicate that the download is not resumable.
- Content-Length: 36907 - The Content-Length header indicates the size of the entity-body i.e. the size of the actual image file is 36907 bytes (37K).
How do I pass a range header to the url?
Now, you know you can make a range request to the url. You need to send GET request including a range header:
Range: bytes=0-1024
The exact sequence should be as follows. First, send HTTP/1.1 GET request:
GET /images/misc/static/2012/11/ifdata-welcome-0.png HTTP/1.1
Next, send the Host request-header to specifies the Internet host and port number of the resource being requested, as obtained from the original URI given by the user or referring resource:
Host: s0.cyberciti.org
Finally, send Range header request that specifies the range of bytes you want:
Range: bytes=0-1024
telnet command example
The telnet command allow you to communicate with a remote computer/server that is using the Telnet protocol. All Unix like operating systems including MS-Windows versions include Telnet Client. To start Telnet Client and to enter the Telnet prompt, run:
telnet your-server-name-here www
telnet your-server-name-here 80
To connect to remote server s0.cyberciti.org through port number 80, type:
telnet s0.cyberciti.org 80
Sample outputs:
Trying 54.240.168.194...
Connected to d2m4hyssawyie7.cloudfront.net.
Escape character is '^]'.
In this example, make range requests (0-1024 bytes) with s0.cyberciti.org to grab /images/misc/static/2012/11/ifdata-welcome-0.png, type:
GET /images/misc/static/2012/11/ifdata-welcome-0.png HTTP/1.1
Host: s0.cyberciti.org
Range: bytes=0-1024
Sample outputs:
Where,
- Output section #1 - GET request.
- Output section #2 - HTTP Status: 206 partial content and range requests header response.
- Output section #3 - Binary data.
curl command
The curl command is a tool to transfer data from or to a server. It support HTTP/FTPSFTP/FILE retrieval using a byte range i.e a partial document from a HTTP/1.1, FTP or SFTP server or a local FILE. Ranges can be specified in a number of ways. In this example, retrieve ifdata-welcome-0.png using two ranges and assemble it locally using standard Unix commands:
curl --header "Range: bytes=0-20000" http://s0.cyberciti.org/images/misc/static/2012/11/ifdata-welcome-0.png -o part1
curl --header "Range: bytes=20001-36907" http://s0.cyberciti.org/images/misc/static/2012/11/ifdata-welcome-0.png -o part2
cat part1 part2 >> test1.png
gnome-open test1.png
Or use the -r option (pass -v option to see headers):
curl -r 0-20000 http://s0.cyberciti.org/images/misc/static/2012/11/ifdata-welcome-0.png -o part1
curl -r 20001-36907 http://s0.cyberciti.org/images/misc/static/2012/11/ifdata-welcome-0.png -o part2
cat part1 part2 >> test2.png
gnome-open test2.png
How do I enable Accept-Ranges header?
Most web server supports the Byte-Range request out of the box. Apache 2.x user try mod_headers in httpd.conf:
Header set Accept-Ranges bytes
Lighttpd user try the following configuration in lighttpd.conf:
## enabled for all file types ##
server.range-requests = "enable"
## But, disable it for pdf files ##
$HTTP["url"] =~ "\.pdf$" {
server.range-requests = "disable"
}
Not a fan of command line interfaces?
You can view HTTP headers of a page and while browsing. Try the following add-ons:
- Download Firefox - live http header.
- Download Google Chrome - live http header.
- Guide: Apple Safari - developer tools to view HTTP header.
Conclusion
This post explained, how to find out HTTP headers and a response status. You can use 206 HTTP status code to grab a large file in parts. If the offset is valid, the server will return an HTTP 206 status code. If the offset is invalid, the request will return an HTTP 416 status code (Requested Range Not Satisfiable).
备注:
所谓断点续传,也就是要从文件已经下载的地方开始继续下载。在以前版本的 HTTP 协议是不支持断点的,HTTP/1.1 开始就支持了。
一般断点下载时才用到 Range 和 Content-Range 实体头。
Range
用于请求头中,指定第一个字节的位置和最后一个字节的位置,一般格式:
Range:(unit=first byte pos)-[last byte pos]
Content-Range
用于响应头,指定整个实体中的一部分的插入位置,他也指示了整个实体的长度。在服务器向客户返回一个部分响应,它必须描述响应覆盖的范围和整个实体长度。一般格式:
Content-Range: bytes (unit first byte pos) - [last byte pos]/[entity legth]
下面是个例子
Remote Address:xxxxxxxxxxx:8080
Request URL:https://xxxxxxxxxxx/site/getpaid/application/8.4.1/userguides/8.4_Credit_Admin_Guide.pdf
Request Method:GET
Status Code:206 Partial Content
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh-TW;q=0.4
Authorization:Basic V2lsbGlhbS5UYW5nOldpbGxpYW1AMTIz
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__utma=187524920.954782073.1382074758.1382426439.1382429938.3; sguser=employee
Host:xxxxxxxxxxx
Range:bytes=0-32767
Referer:https://xxxxxxxxxxx/site/getpaid/application/8.4.1/userguides/8.4_Credit_Admin_Guide.pdf
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Response Headers
Accept-Ranges:bytes
Connection:Keep-Alive
Content-Length:32767
Content-Range:0-32767/8251214
Content-Type:application/pdf
Date:Wed, 16 Jul 2014 05:51:34 GMT
ETag:"{SHA1{0c268be279f6ff52ce8c8f7f272ef151f1ab657e}}"
Keep-Alive:timeout=300
Last-Modified:Mon, 30 Jun 2014 23:22:03 GMT
Server:Nexus/2.7.1-01
Set-Cookie:rememberMe=deleteMe; Path=/nexus; Max-Age=0; Expires=Tue, 15-Jul-2014 05:51:34 GMT
X-Content-Type-Options:nosniff