HTTP协议学习

# 前言

官方文档简略翻译。9 不是代表第九篇,而是在 RFC2616 中是第九篇。重要加粗,龟速翻译。

# Method

9.3 GET

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process.

GET 方法含义是根据请求 URI 检索信息。如果请求 URI 是一个数据处理过程,它产生的数据在响应中应该以一个条目返回,而不是未经处理的文本,除非文本恰好就是处理的输出。

The semantics of the GET method change to a "conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client.

 

GET 的语义变成“条件性的 GET”,当 header 域的请求消息包括了 If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, 或者 If-Range 之一。一个条件性的 GET 方法,要求条目传输必须包括之前描述的域之一。条件性的 GET 方法意图减少不必要的网络使用,通过缓存条目的技术,多条请求可以不用网页刷新或者再次传送数据,只需要利用已有的数据。

注:意思就是,你比如有 If-Modified-Since 属性,这个属性的值有个时间,一般是网页最后修改的时间。提交 GET 请求的时候,服务器根据这个 If-Modified-Since 判断,如果服务器有该网页修改的版本,则发送给一个新网页;否则就告诉简单的通知用户,用原来的网页,所以不需要再次发送网页。这样就减少了网络流量。

The semantics of the GET method change to a "partial GET" if the request message includes a Range header field. A partial GET requests that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client.

GET 的语义变成“部分的 GET”,当 header 域的请求消息包括了 Range。一个部分 GET 请求要求了部分数据被传输,正如区 14.35 描述的那样。一个部分 GET 请求意图减少不必要的网络使用,通过传输部分检索条目而不是客户端拥有的全部条目。

注:意思很明白,检索1000条记录和检索10000条记录时间肯定是不一样的。

The response to a GET request is cacheable if and only if it meets the requirements for HTTP caching described in section 13.

(略)

See section 15.1.3 for security considerations when used for forms.

 

(略)

9.5 POST

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions:

POST 方法用于请求最初的服务器接受了隐藏在请求中的条目,并作为一个新的下级资源,这个资源在 Request-Line(这是什么?) 中可以被 Request-URI 识别。POST 被设计成一个一致的方法,涵盖了以下功能:

      - Annotation of existing resources;
      - Posting a message to a bulletin board, newsgroup, mailing list,
        or similar group of articles;
      - Providing a block of data, such as the result of submitting a
        form, to a data-handling process;

- Extending a database through an append operation.


- 已经存在的资源的标记

- 发表一条信息到电子公告牌,新闻组,邮件列表,或者相似文章组

- 提供一块数据,例如提交一个表格的结果,到一个数据处理的流程

- 扩展一个数据库,通过一个附加的操作

注:附加的操作可能指的是数据库的 Insert 操作

The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI. The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, a news article is subordinate to a newsgroup to which it is posted, or a record is subordinate to a database.

The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30).

Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields. However, the 303 (See Other) response can be used to direct the user agent to retrieve a cacheable resource.

POST requests MUST obey the message transmission requirements set out in section 8.2.

See section 15.1.3 for security considerations.

9.6 PUT

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem. The recipient of the entity MUST NOT ignore any Content-* (e.g. Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases.

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI, it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.

A single resource MAY be identified by many different URIs. For example, an article might have a URI for identifying "the current version" which is separate from the URI identifying each particular version. In this case, a PUT request on a general URI might result in several other URIs being defined by the origin server.

HTTP/1.1 does not define how a PUT method affects the state of an origin server.

PUT requests MUST obey the message transmission requirements set out in section 8.2.

Unless otherwise specified for a particular entity-header, the entity-headers in the PUT request SHOULD be applied to the resource created or modified by the PUT.

9.7 DELETE

The DELETE method requests that the origin server delete the resource identified by the Request-URI. This method MAY be overridden by human intervention (or other means) on the origin server. The client cannot be guaranteed that the operation has been carried out, even if the status code returned from the origin server indicates that the action has been completed successfully. However, the server SHOULD NOT indicate success unless, at the time the response is given, it intends to delete the resource or move it to an inaccessible location.

A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.

# 译者的话

特定为学习 REST 查看了这篇规范。

从这篇文章可以看出,在 REST 中,经常把 HTTP 方法的 POST GET PUT DELETE 对应成 CRUD 是不完全合适的。GET 、PUT 和 DELETE 对应成 RUD 倒是无可厚非;POST 对应成 Create 似乎不太合适,因为POST 方法原意并不是创建什么东西,POST 方法用途很广。

# Reference

https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

原文地址:https://www.cnblogs.com/Piers/p/6591916.html