目录 搜索
Guides Access control CORS Authentication Browser detection using the user agent Caching Caching FAQ Compression Conditional requests Connection management in HTTP 1.x Content negotiation Content negotiation: List of default Accept values Cookies CSP Messages Overview Protocol upgrade mechanism Proxy servers and tunneling Proxy servers and tunneling: Proxy Auto-Configuration (PAC) file Public Key Pinning Range requests Redirections Resources and specifications Resources and URIs Response codes Server-Side Access Control Session Guides: Basics Basics of HTTP Choosing between www and non-www URLs Data URIs Evolution of HTTP Identifying resources on the Web MIME Types MIME types: Complete list of MIME types CSP Content-Security-Policy Content-Security-Policy-Report-Only CSP: base-uri CSP: block-all-mixed-content CSP: child-src CSP: connect-src CSP: default-src CSP: font-src CSP: form-action CSP: frame-ancestors CSP: frame-src CSP: img-src CSP: manifest-src CSP: media-src CSP: object-src CSP: plugin-types CSP: referrer CSP: report-uri CSP: require-sri-for CSP: sandbox CSP: script-src CSP: style-src CSP: upgrade-insecure-requests CSP: worker-src Headers Accept Accept-Charset Accept-Encoding Accept-Language Accept-Ranges Access-Control-Allow-Credentials Access-Control-Allow-Headers Access-Control-Allow-Methods Access-Control-Allow-Origin Access-Control-Expose-Headers Access-Control-Max-Age Access-Control-Request-Headers Access-Control-Request-Method Age Allow Authorization Cache-Control Connection Content-Disposition Content-Encoding Content-Language Content-Length Content-Location Content-Range Content-Type Cookie Cookie2 Date DNT ETag Expect Expires Forwarded From Headers Host If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since Keep-Alive Large-Allocation Last-Modified Location Origin Pragma Proxy-Authenticate Proxy-Authorization Public-Key-Pins Public-Key-Pins-Report-Only Range Referer Referrer-Policy Retry-After Server Set-Cookie Set-Cookie2 SourceMap Strict-Transport-Security TE Tk Trailer Transfer-Encoding Upgrade-Insecure-Requests User-Agent User-Agent: Firefox Vary Via Warning WWW-Authenticate X-Content-Type-Options X-DNS-Prefetch-Control X-Forwarded-For X-Forwarded-Host X-Forwarded-Proto X-Frame-Options X-XSS-Protection Methods CONNECT DELETE GET HEAD Methods OPTIONS PATCH POST PUT Status 100 Continue 101 Switching Protocols 200 OK 201 Created 202 Accepted 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 307 Temporary Redirect 308 Permanent Redirect 400 Bad Request 401 Unauthorized 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Payload Too Large 414 URI Too Long 415 Unsupported Media Type 416 Range Not Satisfiable 417 Expectation Failed 426 Upgrade Required 428 Precondition Required 429 Too Many Requests 431 Request Header Fields Too Large 451 Unavailable For Legal Reasons 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 511 Network Authentication Required Status
文字

HTTP范围请求允许将HTTP消息的一部分从服务器发送到客户端。例如,部分请求可用于大型媒体或下载具有暂停和恢复功能的文件。

检查服务器是否支持部分请求

如果Accept-Ranges存在于HTTP响应中(并且其值不是“ none”),则服务器支持范围请求。例如,您可以通过发出一个HEAD请求和cURL 来检查。

curl -I http://i.imgur.com/z4d4kWk.jpg

HTTP/1.1 200 OK...Accept-Ranges: bytes
Content-Length: 146515

在此响应中,Accept-Ranges: bytes指示字节可用作单位来定义范围。这里Content-Length头部也很有用,因为它表示要检索的图像的完整大小。

如果网站忽略Accept-Ranges头部,他们可能不支持部分请求。有些网站还明确发送“ none”作为价值,表示不支持。在某些应用程序中,下载管理器在这种情况下禁用它们的暂停按钮。

curl -I https://www.youtube.com/watch?v=EwTZ2xpQwpA

HTTP/1.1 200 OK...Accept-Ranges: none

从服务器请求特定范围

如果服务器支持范围请求,则可以使用Range标题发出此类请求。它表示服务器应该返回的文档的部分。

单个零件范围

我们可以从资源请求一个范围。再次,我们可以使用cURL来测试请求。“ -H”选项将向请求附加一个标题行,在这种情况下,该Range标题是请求前1024个字节的标题。

curl http://i.imgur.com/z4d4kWk.jpg -i -H "Range: bytes=0-1023"

发出的请求如下所示:

GET /z4d4kWk.jpg HTTP/1.1Host: i.imgur.com
Range: bytes=0-1023

服务器响应206 Partial Content状态:

HTTP/1.1 206 Partial Content
Content-Range: bytes 0-1023/146515Content-Length: 1024...(binary content)

Content-Length报头现在指示所请求的范围的大小(而不是图像的全尺寸)。的Content-Range响应报头指示,其中在全资源此部分消息所属。

多部分范围

Range头还允许您在多文档中同时获得多个范围。范围用逗号分隔。

curl http://www.example.com -i -H "Range: bytes=0-50, 100-150"

服务器响应206 Partial Content状态和Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5标题,指示多部分字节范围如下。每部分包含其自己的Content-TypeContent-Range领域以及所需的边界参数指定用于每个本体部分分开的边界字符串。

HTTP/1.1 206 Partial Content
Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5
Content-Length: 282--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 0-50/1270<!doctype html><html><head>    <title>Example Do--3d6b6a416f9b5
Content-Type: text/html
Content-Range: bytes 100-150/1270eta http-equiv="Content-type" content="text/html; c--3d6b6a416f9b5--

有条件的范围请求

当恢复请求资源的更多部分时,您需要确保自从收到最后一个片段以来,存储的资源未被修改。

If-RangeHTTP请求报头使得范围请求的条件:如果条件被满足,该范围的请求将被发出,并且服务器发送回一个206 Partial Content与适当的身体的答案。如果条件未满足,则会返回具有200 OK状态的完整资源。这个头文件可以与Last-Modified验证器一起使用,也可以与一个ETag,但不与两者一起使用。

If-Range: Wed, 21 Oct 2015 07:28:00 GMT

部分请求响应

在处理范围请求时,有三种相关的状态:

  • 如果成功请求范围,206 Partial Content状态将从服务器返回。

  • 如果范围请求超出范围(范围值与资源的范围重叠),则服务器以416 Requested Range Not Satisfiable状态进行响应。

  • 如果不支持范围请求,则200 OK状态将从服务器发回。

比较分块 Transfer-Encoding

Transfer-Encoding报头允许分块编码,其有用的,当大量数据被发送到客户端和响应是未知的,直到请求已被完全处理的总大小。服务器立即向客户端发送数据,而不会缓冲响应或确定确切的长度,从而延长了延迟时间。范围请求和分块是兼容的,可以使用或不使用对方。

上一篇: 下一篇: