简体   繁体   English

HTTP请求\\响应头语法

[英]HTTP Request\Response Header Grammar

In the header of an HTTP request or response will the header keys be constant in terms of capitalization, between servers. 在HTTP请求或响应的标头中,标头键在服务器之间在大小写方面是恒定的。

I ask so I can expect in my code: (Using Fake Function names) 我问,所以我可以在我的代码中期望:(使用伪函数名称)

Safe Precise Python Code 安全精确的Python代码

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. 澄清将有助于我的代码整洁。

HTTP header names are case-insensitive , per the HTTP specification. 根据HTTP规范,HTTP标头名称不区分大小写

RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1 RFC 2616-超文本传输​​协议-HTTP / 1.1
Section 4.2 - Message Headers 第4.2节 -邮件标题

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]. HTTP标头字段包括通用标头(第4.5节),请求标头(第5.3节),响应标头(第6.2节)和实体标头(第7.1节)字段,它们遵循与本节相同的通用格式RFC 822 [3.1]中的3.1。 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. 字段值可以在任何数量的LWS之前,尽管首选单个SP。 Header fields can be extended over multiple lines by preceding each extra line with at least one SP or HT. 通过在每条额外的行之前添加至少一个SP或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. 在生成HTTP构造时,应用程序应遵循“通用形式”,即已知形式或表示形式,因为某些实现可能无法接受通用形式以外的任何形式。

RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing RFC 7230-超文本传输​​协议(HTTP / 1.1):消息语法和路由
Section 3.2 - Header Fields: 第3.2节 -标头字段:

Each header field consists of a case-insensitive field name followed by a colon (":"), optional leading whitespace, the field value, and optional trailing whitespace. 每个标题字段均由不区分大小写的字段名,后跟冒号(“:”),可选的前导空格,字段值和可选的尾随空格组成。

请记住,即使大多数主要服务器都具有一致的大写字母,任何Joe PHP Developer都可以在其代码中手动设置响应头-并且没有办法监管该人用作大写字母的标准。

HTTP header names are case insensitive. HTTP标头名称不区分大小写。

It looks like you're using python. 看来您使用的是python。 Check out the requests library. 签出requests库。 It'll make your life much easier: http://docs.python-requests.org/en/latest/ 它将使您的生活更加轻松: http : //docs.python-requests.org/en/latest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM