HTTP Range规范和相关头部
分享汇总 计网系列文章汇总
原文地址:GitHub
概述
允许服务器只发送给客户端一部分资源
应用
-
断点续传
-
多线程下载
-
视频播放器实时拖动
相应头部字段
-
Accept-Range 表示服务器是否支持Range请求
-
Range 表示客户端发起的是Range请求
-
Content-Range字段表示返回的包体的范围,包体总大小
Range请求
只请求开头的4个字节
root@:~# curl http://protocol.taohui.tech/app/letter.txt -H 'Range: bytes=0-3'
abcdroot
Range条件请求
先获得签名
root@iZ2zeeq1jai7e7ypku5kakZ:~# curl http://protocol.taohui.tech/app/letter.txt -I
HTTP/1.1 200 OK
Server: openresty/1.15.8.3
Date: Fri, 10 Jul 2020 11:40:46 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 27
Last-Modified: Sat, 27 Apr 2019 06:51:31 GMT
Connection: keep-alive
ETag: "5cc3fbf3-1b"
Set-Cookie: protocol=cookieA
Accept-Ranges: bytes
请求时候附上签名
root@iZ2zeeq1jai7e7ypku5kakZ:~# curl http://protocol.taohui.tech/app/letter.txt -H 'Range: bytes=0-4' -H 'If-Match: "5cc3fbf3-1b"'
abcde
签名不匹配的情况
root@iZ2zeeq1jai7e7ypku5kakZ:~# curl http://protocol.taohui.tech/app/letter.txt -H 'Range: bytes=0-4' -H 'If-Match: "5cc3fbf3-2b"' -I
HTTP/1.1 412 Precondition Failed
Server: openresty/1.15.8.3
Date: Fri, 10 Jul 2020 11:42:18 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 179
Connection: keep-alive
服务端响应
正常进行了范围查询
root@iZ2zeeq1jai7e7ypku5kakZ:~# curl http://protocol.taohui.tech/app/letter.txt -H 'Range: bytes=0-3' -I
HTTP/1.1 206 Partial Content
Server: openresty/1.15.8.3
Date: Fri, 10 Jul 2020 11:42:54 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 4
Last-Modified: Sat, 27 Apr 2019 06:51:31 GMT
Connection: keep-alive
ETag: "5cc3fbf3-1b"
Set-Cookie: protocol=cookieA
Content-Range: bytes 0-3/27
查询的范围超出了包体范围
root@iZ2zeeq1jai7e7ypku5kakZ:~# curl http://protocol.taohui.tech/app/letter.txt -H 'Range: bytes=30-40' -I
HTTP/1.1 416 Requested Range Not Satisfiable
Server: openresty/1.15.8.3
Date: Fri, 10 Jul 2020 11:45:15 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 203
Connection: keep-alive
Set-Cookie: protocol=cookieA
Content-Range: bytes */27