HTTP请求\响应头语法 [英] HTTP Request\Response Header Grammar

查看:207
本文介绍了HTTP请求\响应头语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTTP请求或响应的标头中,在服务器之间,标头键在大小写方面是恒定的.

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)

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.

我要使用的代码

 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.

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

不好但可能的反应?

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规范,HTTP标头名称不区分大小写.

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

RFC 2616 -超文本传输​​协议-HTTP/1.1
4.2节-邮件标题

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

HTTP标头字段,其中包括通用标头(第4.5节), 请求标头(第5.3节),响应标头(第6.2节)和 实体标题(第7.1节)字段,遵循与以下相同的通用格式 RFC 822 [9]第3.1节中给出的内容.每个标题字段包含 的名称,后跟冒号(:")和字段值. 字段名称 不区分大小写.字段值可以前面加上任意数量 LWS,尽管首选单个SP.标头字段可以是 通过在每条额外的行之前加上at来扩展多行 至少一个SP或HT.应用程序应遵循通用格式",其中 在生成HTTP构造时,已知或已指出一种,因为 可能存在一些无法接受任何内容的实现 除了常见的形式.

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 -超文本传输​​协议(HTTP/1.1):消息语法和路由
3.2节-标头字段:

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.

这篇关于HTTP请求\响应头语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆