HTTP Request\Response Header Grammar

2019-07-27 02:27发布

In the header of an HTTP request or response will the header keys be constant in terms of capitalization, between servers.

I ask so I can expect in my code: (Using Fake Function names)

Safe Precise Python Code

for hdr in header.keys():
    if 'content-length' == hdr.lower():
        recv_more_data( header[hdr] )    # header[hdr] == Content-Length (5388) bytes
        break    # Exit for loop when if statement is met.

Code I Would Like To Use

 recv_more_data (header['Content-Length'])    
 # I know to expect 'Content-Length' not 'content-Length' or some other variation

Meaning will a server ever return a header with the keys like so.

Standard Request

GET / HTTP/1.1
Host: www.example-host.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: */*
Accept-Language: en-US
Accept-Encoding: gzip
Connection: closed
Content-Length: 0

A Bad But Possible Response?

HTTP/1.1 200 OK
Server: nginx/1.0.15
date: Thu, 23 Oct 2014 00:25:37 GMT
content-Type: text/html; charset=iso-8859-1
transfer-encoding: chunked
Connection: close
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip

Clarification will help my code neatness.

3条回答
看我几分像从前
2楼-- · 2019-07-27 02:35

Bear in mind that, even though most major servers will have consistent capitalization, any Joe PHP Developer can set the response headers manually in their code - and there is no way to police what that guy uses as a capitalization standard.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-27 02:47

HTTP header names are case-insensitive, per the HTTP specification.

RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1
Section 4.2 - Message Headers

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. The field value MAY be preceded by any amount of LWS, though a single SP is preferred. Header fields can be extended over multiple lines by preceding each extra line with at least one SP or HT. Applications ought to follow "common form", where one is known or indicated, when generating HTTP constructs, since there might exist some implementations that fail to accept anything beyond the common forms.

RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
Section 3.2 - Header Fields:

Each header field consists of a case-insensitive field name followed by a colon (":"), optional leading whitespace, the field value, and optional trailing whitespace.

查看更多
smile是对你的礼貌
4楼-- · 2019-07-27 02:56

HTTP header names are case insensitive.

It looks like you're using python. Check out the requests library. It'll make your life much easier: http://docs.python-requests.org/en/latest/

查看更多
登录 后发表回答