HTTP规范中关于消息的header头部
HTTP中的header头部
包括通用头部(general-header)、请求头部(request-header)、响应头部(response-header)、实体头部(entity-header)。
这些header头部都采用通用的格式定义。
通用头部(general-header):
general-header = Cache-Control ; Section 14.9
| Connection ; Section 14.10
| Date ; Section 14.18
| Pragma ; Section 14.32
| Trailer ; Section 14.40
| Transfer-Encoding ; Section 14.41
| Upgrade ; Section 14.42
| Via ; Section 14.45
| Warning ; Section 14.46
Transfer-Encoding通用头部:
Transfer-Encoding头部都有哪些可选值?
Transfer-Encoding头部可指定:"chunked" (section 3.6.1), "identity" (section 3.6.2), "gzip" (section 3.5), "compress" (section 3.5), and "deflate" (section 3.5).
Transfer-Encoding头部的这些可指定的值是从哪里来的?
transfer-coding value tokens. Initially, the registry contains the
following tokens: "chunked" (section 3.6.1), "identity" (section
3.6.2), "gzip" (section 3.5), "compress" (section 3.5), and "deflate"
(section 3.5).
New transfer-coding value tokens SHOULD be registered in the same way
as new content-coding value tokens (section 3.5).
以下例子如果不采用Transfer-Encoding: chunked的话,一般是这样请求的:采用Content-Type: application/x-www-form-urlencoded的Content-Type。如下:
Content-Type: application/x-www-form-urlencoded的例子:
POST /test HTTP/1.1\r\n
Host: localhost\r\n
Content-Length: 12\r\n
Content-Type: application/x-www-form-urlencoded\r\n
\r\n
data=abcdefg
响应:
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 7
Date: Fri, 22 Mar 2019 17:54:40 GMT
abcdefg
如果采用Transfer-Encoding: chunked的话:
这里采用Transfer-Encoding: chunked后,将请求参数data=abcdefg硬生生的分成4段(chunk)进行传输(实际上是3段,第4段的chunk为0表示所有的chunk都传输过去了,即结束标识),当然这里只是为了演示Transfer-Encoding: chunked的用法。
POST /test HTTP/1.1\r\n
Host: localhost\r\n
Transfer-Encoding: chunked\r\n
Content-Type: application/x-www-form-urlencoded\r\n
\r\n
2\r\n
da\r\n
5\r\n
ta=ab\r\n
5\r\n
cdefg\r\n
0\r\n
\r\n
响应:
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 7
Date: Fri, 22 Mar 2019 18:38:42 GMT
abcdefg
请求头部(request-header):
request-header = Accept ; Section 14.1
| Accept-Charset ; Section 14.2
| Accept-Encoding ; Section 14.3
| Accept-Language ; Section 14.4
| Authorization ; Section 14.8
| Expect ; Section 14.20
| From ; Section 14.22
| Host ; Section 14.23
| If-Match ; Section 14.24
| If-Modified-Since ; Section 14.25
| If-None-Match ; Section 14.26
| If-Range ; Section 14.27
| If-Unmodified-Since ; Section 14.28
| Max-Forwards ; Section 14.31
| Proxy-Authorization ; Section 14.34
| Range ; Section 14.35
| Referer ; Section 14.36
| TE ; Section 14.39
| User-Agent ; Section 14.43
Range请求头部:
例子:
一次请求一个分段
请求服务端资源的前3个字节:
GET / HTTP/1.1\r\n
Host: localhost\r\n
Range:bytes=0-2\r\n
\r\n
响应:
HTTP/1.1 206 Partial Content
Server: nginx/0.8.18
Date: Fri, 22 Mar 2019 16:55:13 GMT
Content-Type: text/html
Content-Length: 3
Last-Modified: Wed, 30 Aug 2006 06:39:18 GMT
Connection: keep-alive
Content-Range: bytes 0-2/151
<ht
一次请求多个分段
分2个分段请求服务端资源,第一个分段请求前3个字节,第二个分段请求从第3个字节到最后一个字节:
GET / HTTP/1.1\r\n
Host: localhost\r\n
Range:bytes=0-2,3-\r\n
\r\n
响应:
HTTP/1.1 206 Partial Content
Server: nginx/0.8.18
Date: Fri, 22 Mar 2019 17:02:17 GMT
Content-Type: multipart/byteranges; boundary=00000000001
Content-Length: 320
Last-Modified: Wed, 30 Aug 2006 06:39:18 GMT
Connection: keep-alive
--00000000001
Content-Type: text/html
Content-Range: bytes 0-2/151
<ht
--00000000001
Content-Type: text/html
Content-Range: bytes 3-150/151
ml>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
--00000000001--
完整的响应应该是:
HTTP/1.1 200 OK
Server: nginx/0.8.18
Date: Fri, 22 Mar 2019 16:58:36 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Wed, 30 Aug 2006 06:39:18 GMT
Connection: keep-alive
Accept-Ranges: bytes
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
响应头部(response-header):
response-header = Accept-Ranges ; Section 14.5
| Age ; Section 14.6
| ETag ; Section 14.19
| Location ; Section 14.30
| Proxy-Authenticate ; Section 14.33
| Retry-After ; Section 14.37
| Server ; Section 14.38
| Vary ; Section 14.44
| WWW-Authenticate ; Section 14.47
实体头部(entity-header):
entity-header = Allow ; Section 14.7
| Content-Encoding ; Section 14.11
| Content-Language ; Section 14.12
| Content-Length ; Section 14.13
| Content-Location ; Section 14.14
| Content-MD5 ; Section 14.15
| Content-Range ; Section 14.16
| Content-Type ; Section 14.17
| Expires ; Section 14.21
| Last-Modified ; Section 14.29
| extension-header
extension-header = message-header
Content-Type实体头部:
Content-Type: application/x-www-form-urlencoded的例子:
POST /test HTTP/1.1\r\n
Host: localhost\r\n
Content-Length: 12\r\n
Content-Type: application/x-www-form-urlencoded\r\n
\r\n
data=abcdefg
响应:
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 7
Date: Fri, 22 Mar 2019 17:54:40 GMT
abcdefg
Content-Type: multipart/form-data; boundary=m1w592WOaxybpIOCyoAg-IXoMBaAb1R9kv92的例子:
POST /test HTTP/1.1\r\n
Host: localhost\r\n
Content-Length: 205\r\n
Content-Type: multipart/form-data; boundary=m1w592WOaxybpIOCyoAg-IXoMBaAb1R9kv92\r\n
\r\n
--m1w592WOaxybpIOCyoAg-IXoMBaAb1R9kv92\r\n
Content-Disposition: form-data; name="data"\r\n
Content-Type: text/plain; charset=UTF-8\r\n
Content-Transfer-Encoding: 8bit\r\n
\r\n
aaaa\r\n
--m1w592WOaxybpIOCyoAg-IXoMBaAb1R9kv92
响应:
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 4
Date: Fri, 22 Mar 2019 17:57:19 GMT
aaaa