其他分享
首页 > 其他分享> > Http协议规范

Http协议规范

作者:互联网

HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容请参考RFC2616

HTTP协议采用了请求/响应模型。客户端向服务器发送一个请求,请求头包含请求的方法、URI、协议版本、以及包含请求修饰符、客户信息和内容的类似于MIME的消息结构

服务器以一个状态行作为响应,相应的内容包括消息协议的版本,成功或者错误编码加上包含服务器信息、实体元信息以及可能的实体内容

通用头域

通用头域包含请求和响应消息都支持的头域,通用头域包含Cache-Control、Connection、Date、Pragma、Transfer- Encoding、Upgrade、Via

对通用头域的扩展要求通讯双方都支持此扩展,如果存在不支持的通用头域,一般将会作为实体头域处理。下面简单介绍几个在UPnP消息中使用的通用头域

Cache-Control头域

Cache-Control指定请求和响应遵循的缓存机制

各个消息中的指令含义如下

Public指示响应可被任何缓存区缓存。

Date头域

Date头域表示消息发送的时间,时间的描述格式由rfc822定义

例如,Date:Mon,31Dec200104:25:57GMT

Date描述的时间表示世界标准时,换算成本地时间,需要知道用户所在的时区

Pragma头域

Pragma头域用来包含实现特定的指令,最常用的是Pragma:no-cache。在HTTP/1.1协议中,它的含义和Cache-Control:no-cache相同

请求消息

请求消息的第一行为下面的格式

MethodSPRequest- URISPHTTP-VersionCRLFMethod

SP表示空格

Request-URI遵循URI格式,在此字段为星号(*)时,说明请求并不用于某个特定的资源地址,而是用于服务器本身

HTTP- Version表示支持的HTTP版本,例如为HTTP/1.1

CRLF表示换行回车符

Method Request-URI HTTP-Version
Method

请求头域允许客户端向服务器传递关于请求或者关于客户机的附加信息

请求头域可能包含下列字段

对请求头域的扩展要求通讯双方都支持,如果存在不支持的请求头域,一般将会作为实体头域处理

典型的请求消息

GET http://download.microtool.de:80/somedata.exe
Host:download.microtool.de
Accept:*/*
Pragma:no-cache
Cache-Control:no-cache
Referer:http://download.microtool.de/
User-Agent:Mozilla/4.04[en](Win95;I;Nav)
Range:bytes=554554-

通用头部分

请求头部分

Host头域

Referer头域

Range头域

但是服务器可以忽略此请求头,如果无条件GET包含Range请求头,响应会以状态码206(PartialContent)返回而不是以200(OK)

User-Agent头域

User-Agent头域的内容包含发出请求的用户信息

响应消息

响应消息的第一行为下面的格式

HTTP-VersionSPStatus-CodeSPReason-PhraseCRLF

Status-Code的第一个数字定义响应的类别,后两个数字没有分类的作用

第一个数字可能取5个不同的值

响应头域允许服务器传递不能放在状态行的附加信息,这些域主要描述服务器的信息和Request-URI进一步的信息

响应头域包含Age、 Location、Proxy-Authenticate、Public、Retry-After、Server、Vary、Warning、WWW- Authenticate

对响应头域的扩展要求通讯双方都支持,如果存在不支持的响应头域,一般将会作为实体头域处理

典型的响应消息

HTTP/1.0 200 OK
Date:Mon,31Dec200104:25:57GMT
Server:Apache/1.3.14(Unix)
Content-type:text/html
Last-modified:Tue,17Apr200106:46:28GMT
Etag:"a030f020ac7c01:1e9f"
Content-length:39725426
Content-range:bytes554554-40279979/40279980

Location响应头

Location响应头用于重定向接收者到一个新URI地址

Server响应头

Server响应头包含处理请求的原始服务器的软件信息

此域能包含多个产品标识和注释,产品标识一般按照重要性排序

实体

请求消息和响应消息都可以包含实体信息,实体信息一般由实体头域和实体组成

实体头域包含关于实体的原信息

实体头包括

extension-header允许客户端定义新的实体头,但是这些域可能无法未接受方识别

实体可以是一个经过编码的字节流,它的编码方式由Content-Encoding或Content-Type定义,它的长度由Content-Length或 Content-Range定义

Content-Type实体头

一般格式

Content-Range:bytes-unitSPfirst-byte-pos-last-byte-pos/entity-legth

传送头500个字节次字段的形式:

HTTP协议(RFC2616)方法定义

OPTIONS

OPTIONS 方法表示在由 Request-URI 标识的请求/响应链上关于有效通迅选项信息的请求

尽管本规范没有定义该实体的用法,将来的HTTP 扩展可能使用 OPTIONS 消息体来更详细地查询服务器的信息

服务器不支持该扩展,可以丢弃该请求消息体
如果 Request-URI 是星号(“*”),OPTIONS 请求通常试图应用于服务器而不是特定的资源。

由于服务器的通迅选项一般由资源决定,所以“*”请求只作为“ping”或“no-op”类型的方法有用

它没有任何作用,除了允许客户端测试服务器的能力

例如,可用来测试HTTP/1.1 代理的一致性(或缺少因素)。
如果 Request-URI不是星号,OPTIONS请求只应用于与该资源通迅时的有效选项。

200 响应应该包括任何头部域来表示服务器实现和可应用到该资源的可选特性(如Allow),可能包括该规范没有定义的扩展

如果有响应消息体,则应该还包括通迅选项的信息

本规范没有定义该消息体的格式,但可能在将来扩展 HTTP时定义

内容协商可用于选择适当的响应格式,如果不包括响应消息体,则响应必须包括域值为“0”的 Content-Length 域

Max-Forwards 请求头部域可能用于请求链中定位特定代理

GET

HEAD

除了服务器不能在响应中返回消息体,HEAD 方法与 GET 相同

POST

POST 方法用来请求原始服务器接受请求中封装的实体作为从属于请求行中的Request-URI标识的副属。POST设计允许完成下列功能的统一方法:

POST 方法执行的实际功能由服务器决定,且通常取决于 Request-URI

上传的实体从属于该URI,通过文件从属于包含它的目录,新的论文从属于它上传的新闻组,或记录从属于数据库的方式

POST方法执行的行为可能不导致通过 URI 能够标识的某个资源

在这种情况下,200(OK)或 204(No Content)都是适合的响应状态

这取决于描述结果的响应是否包括实体,如果原始服务器创建了资源,响应应该是 201(Created),且包含描述请求状态的实体,和新资源的引用,和Location头部

该方法的响应不能缓存,除非响应包括适当的Cache-Control或 Expires头部域

303(See Other)响应能够用来引导用户代理获取可缓存的资源

PUT

PUT方法请求以提供的Request-URI存储封装的实体
如果 Request-URI引用已经存在的资源,该封装实体应该被认作原始服务器存储的修改版本

如果 Request-URI没有指向已存在的资源, 且该URI可以被请求的用户代理定义为新的资源

则原始服务器可以用该 URI创建资源

如果创建了新的资源,则原始服务器必须通过 201(Created)响应提示用户代理
如果修改了已存在的资源,则应该发送200(OK)或 204(No Content)响应代码来表示成功完成了请求

如果不能按 Request-URI创建或修改资源,则应该给出适当的错误响应以反映出问题的性质

实体的接受方不能乎略任何不理解或没有实现的 Content-*(如Content-Range)头部,在这种情况下必须返回 501(Not Implemented)响应。
如果请求通过缓冲服务器且Request-URI标识出一个或多个缓冲的实体,则应该认为这些实体过期了。该方法的响应不可缓存。

POST和PUT请求间的基本区别反映在 Request-URI 的不统一

POST请求中 URI 标识的资源将处理封装的实体

这时客户代理可以自己决定是否要重定向该请求,可以用许多不同的 URI 标识同个资源

例如,一篇文章可以有标识为“当前版本”的URI,它独立于标识每个特别版本的 URI

在这种情况下,使用通用 URI 的 PUT 请求可能造成原始服务器定义的一些不同URI的结果,HTTP/1.1 没有定义PUT方法如何影响原始服务器的状态

除了其它特殊实体头部的规定,PUT 请求中的实体头部应该应用到 PUT 创建或修改的资源上

DELETE

DELETE 方法请求原始服务器删除Request-URI 标识的资源

原始服务器可在人为干涉下(或其它意思)屏闭该方法

客户端不能确保该操作已经提交,即使原始服务器发出的状,态码表明动作已经成功完成也如此

然而,在给出响应的时候,服务器不应该表示成功,除非它试图删除该资源或将它移动到不可访问的位置

如果响应包含描述状态的实体,成功响应应该是200(OK)

如果动作没有实施,则是202(Accepted)

如果动作已经实施但响应不包含实体,则是 204(No Content)

如果请求通过缓冲服务器,且Request-URI标识一个或多个当前缓存的实体,则应该认为这些实体已经过期。该方法的响应不可缓存

TRACE

TRACE 方法用于引起远程的,该请求消息的应用层回射

请求的最终接收者应该反射200(OK)响应,并以该消息作为客户端回收消息的实体

最终接收者是原始服务器或第一个收到请求中的Max-Forwards值为0(0)的代理或网关

TRACE 请求不能包括实体

TRACE 允许客户端看见请求链上的另一端收到了什么,然后使用该数据作为测试或诊断信息

Via 头部域的值有特殊作用,将它作为请求链路径

使用Max-Forwards头部域允许客户端限制请求链的长度,这对于测试无限循环转发消息的代理链非常有用

如请求有效,则响应应该在实体中包含整个请求消息,设置 Content-Type 为“message/http” ,该方法的响应不能缓存

CONNECT

规范保留 CONNECT 方法名。该方法用于代理,使之能够动态切换隧道(例如 SSL隧道)

Requests

Field nameDescriptionExample
Accept Content-Types that are acceptable Accept: text/plain
Accept-Charset Character sets that are acceptable Accept-Charset: utf-8
Accept-Encoding Acceptable encodings Accept-Encoding: <compress | gzip | identity>
Accept-Language Acceptable languages for response Accept-Language: en-US
Accept-Ranges Allows the server to indicate its acceptance of range requests for a resource Accept-Ranges: bytes
Authorization Authentication credentials for HTTP authentication Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Cache-Control Used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain Cache-Control: no-cache
Connection What type of connection the user-agent would prefer Connection: close
Cookie an HTTP cookie previously sent by the server with Set-Cookie (below) Cookie: $Version=1; Skin=new;
Content-Length The length of the request body in octets (8-bit bytes) Content-Length: 348
Content-Type The mime type of the body of the request (used with POST and PUT requests) Content-Type: application/x-www-form-urlencoded
Date The date and time that the message was sent Date: Tue, 15 Nov 1994 08:12:31 GMT
Expect Indicates that particular server behaviors are required by the client Expect: 100-continue
From The email address of the user making the request From: user@email.com
Host The domain name of the server (for virtual hosting), mandatory since HTTP/1.1 Host: en.wikipedia.org
If-Match Only perform the action if the client supplied entity matches the same entity on the server. This is mainly for methods like PUT to only update a resource if it has not been modified since the user last updated it. If-Match: "737060cd8c284d8af7ad3082f209582d"
If-Modified-Since Allows a 304 Not Modified to be returned if content is unchanged If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
If-None-Match Allows a 304 Not Modified to be returned if content is unchanged, see HTTP ETag If-None-Match: "737060cd8c284d8af7ad3082f209582d"
If-Range If the entity is unchanged, send me the part(s) that I am missing; otherwise, send me the entire new entity If-Range: "737060cd8c284d8af7ad3082f209582d"
If-Unmodified-Since Only send the response if the entity has not been modified since a specific time. If-Unmodified-Since: Sat, 29 Oct 1994 19:43:31 GMT
Max-Forwards Limit the number of times the message can be forwarded through proxies or gateways. Max-Forwards: 10
Pragma Implementation-specific headers that may have various effects anywhere along the request-response chain. Pragma: no-cache
Proxy-Authorization Authorization credentials for connecting to a proxy. Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Range Request only part of an entity. Bytes are numbered from 0. Range: bytes=500-999
Referer This is the address of the previous web page from which a link to the currently requested page was followed. Referer: http://en.wikipedia.org/wiki/Main_Page
TE The transfer encodings the user agent is willing to accept: the same values as for the response header Transfer-Encoding can be used, plus the "trailers" value (related to the "chunked" transfer method) to notify the server it accepts to receive additional headers (the trailers) after the last, zero-sized, chunk. TE: trailers,deflate;q=0.5
Upgrade Ask the server to upgrade to another protocol. Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11
User-Agent The user agent string of the user agent User-Agent: Mozilla/5.0 (Linux; X11)
Via Informs the server of proxies through which the request was sent. Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning A general warning about possible problems with the entity body. Warning: 199 Miscellaneous warning

Responses

Field nameDescriptionExample
Accept-Ranges What partial content range types this server supports Accept-Ranges: bytes
Age The age the object has been in a proxy cache in seconds Age: 12
Allow Valid actions for a specified resource. To be used for a 405 Method not allowed Allow: GET, HEAD
Cache-Control Tells all caching mechanisms from server to client whether they may cache this object Cache-Control: no-cache
Content-Encoding The type of encoding used on the data Content-Encoding: gzip
Content-Language The language the content is in Content-Language: da
Content-Length The length of the response body in octets (8-bit bytes) Content-Length: 348
Content-Location An alternate location for the returned data Content-Location: /index.htm
Content-Disposition An opportunity to raise a "File Download" dialogue box for a known MIME type Content-Disposition: attachment; filename=fname.ext
Content-MD5 Base64-encoded binary MD5 sum of the content of the response Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ==
Content-Range Where in a full body message this partial message belongs Content-Range: bytes 21010-47021/47022
Content-Type The mime type of this content Content-Type: text/html; charset=utf-8
Date The date and time that the message was sent Date: Tue, 15 Nov 1994 08:12:31 GMT
ETag An identifier for a specific version of a resource, often a Message Digest, see ETag ETag: "737060cd8c284d8af7ad3082f209582d"
Expires Gives the date/time after which the response is considered stale Expires: Thu, 01 Dec 1994 16:00:00 GMT
Last-Modified The last modified date for the requested object, in RFC 2822 format Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT
Location Used in redirection, or when a new resource has been created. Location: http://www.w3.org/pub/WWW/People.html
Pragma Implementation-specific headers that may have various effects anywhere along the request-response chain. Pragma: no-cache
Proxy-Authenticate Request authentication to access the proxy. Proxy-Authenticate: Basic
Refresh Used in redirection, or when a new resource has been created. This refresh redirects after 5 seconds.
(This is a proprietary/non-standard header extension introduced by Netscape and supported by most web browsers.)
Refresh: 5; url=http://www.w3.org/pub/WWW/People.html
Retry-After If an entity is temporarily unavailable, this instructs the client to try again after a specified period of time. Retry-After: 120
Server A name for the server Server: Apache/1.3.27 (Unix) (Red-Hat/Linux)
Set-Cookie an HTTP cookie Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1
Trailer The Trailer general field value indicates that the given set of header fields is present in the trailer of a message encoded with chunked transfer-coding. Trailer: Max-Forwards
Transfer-Encoding The form of encoding used to safely transfer the entity to the user. Currently defined methodsare: chunked, compress, deflate, gzip, identity. Transfer-Encoding: chunked
Vary Tells downstream proxies how to match future request headers to decide whether the cached response can be used rather than requesting a fresh one from the origin server. Vary: *
Via Informs the client of proxies through which the response was sent. Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1)
Warning A general warning about possible problems with the entity body. Warning: 199 Miscellaneous warning
WWW-Authenticate Indicates the authentication scheme that should be used to access the requested entity. WWW-Authenticate: Basic

Common non-standard headers

Non-standard headers can be used, with the convention of prefixing the header name with X-

Field nameDescriptionExample
X-Frame-Options[2] Clickjacking protection. DENY - no rendering within a frame, SAMEORIGIN - no rendering if origin mismatch X-Frame-Options: DENY
X-XSS-Protection[citation needed] Cross-site scripting (XSS) filter X-XSS-Protection: 1; mode=block
X-Requested-With[3] mainly used to identify Ajax requests. Most JavaScript frameworks send this header with value of XMLHttpRequest X-Requested-With: XMLHttpRequest
X-Forwarded-For[4] a de facto standard for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or load balancer X-Forwarded-For: client1, proxy1, proxy2
X-Forwarded-Proto[citation needed] a de facto standard for identifying the originating protocol of an HTTP request, since a reverse proxy (load balancer) communicates with a web server using HTTP X-Forwarded-Proto: https

标签:协议,Http,请求,头域,实体,规范,URI,Content,响应
来源: https://www.cnblogs.com/YC-L/p/14472311.html