协议限制

文档 RFC7504 明确指出Header的定义:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
8.1.2.  HTTP Header Fields

   HTTP header fields carry information as a series of key-value pairs.
   For a listing of registered HTTP headers, see the "Message Header
   Field" registry maintained at <https://www.iana.org/assignments/
   message-headers>.

   Just as in HTTP/1.x, header field names are strings of ASCII
   characters that are compared in a case-insensitive fashion.  However,
   header field names MUST be converted to lowercase prior to their
   encoding in HTTP/2.  A request or response containing uppercase
   header field names MUST be treated as malformed (Section 8.1.2.6).

从上文可知,HTTP/2 和 HTTP/1.x 同样使用 ASCII 字符集.

  • HTTP/1.x 的Header需要首字母大写
  • HTTP/2 的Header必须使用小写

大小写不敏感

HTTP协议的Header的key是大小写不敏感的。

标准如下:

1
2
3
4
5
6
HTTP header fields, which include general-header (section 4.5),
request-header (section 5.3), response-header (section 6.2), and
entity-header (section 7.1) fields, follow the same generic format as
that given in Section 3.1 of RFC 822 [9]. Each header field consists
of a name followed by a colon (":") and the field value. Field names
are case-insensitive.

协议中说了,header的name是大小写不区分的。

各语言的客户端需要保证http协议对header的读写是大小写不敏感的.