Http协议head和cache control使用分类小结
1 Request:
method: GET, HEAD, POST.(...)
- Get --- Retrive Data.
- Head --- The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. (只有head,没有body)
- Post --- 上交数据。
head:
小组1:
- Accept --- certain media types which are acceptable for the response.
- Accept-Charset --- indicate what character sets are acceptable for the response.
- Accept-Encoding --- restricts the content-codings that are acceptable in the response
- Accept-Language --- restricts the set of natural languages that are preferred as a response to the request.
小组1中的accept这类代表:user agent 希望接收的类型。
小组1例:
- Accept: */* ---- 可以接收任何类型。
- Accept-Language: zh-cn,zh;q=0.5 ---- 最希望接收中国的中文,如果没有接收其他国家的zh也可以,但对于我来说质量就只能为q=0.5了。
- Accept-Encoding: gzip,deflate ---- 可以接收的传输内容压缩方式为gzip deflate
- Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7 ----- 最希望接收的编码为GB2312,如果没有GB2312的版本的话,可以传送utf-8的,再没有就传送其他的也可以。不过GB2312默认q=1,utf-8对我来说就只剩q=0.7了。
小组2:
- Host --- the Internet host and port number of the resource being requested
- Referer --- allows the client to specify, for the server's benefit, the address (URI) of the resource from which the Request-URI was obtained
- User-Agent --- contains information about the user agent originating the request
小组2中代表的是一些主机信息,和User-Agent的一些信息。
小组2例,假设我在我的windows上的火狐浏览器中使用hao123上的链接去访问google的话,request里面上述几个head field的信息是这样的:
- Host: www.google.com.hk
- User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
- Referer: http://www.hao123.com/
小组3:
- If-*, 是一些跟cache control的validate有关的head,放到下面和response里有关cache control有关的head一起说。
小组4:
一些不常用到的request head:
- Authorization
- From
- Max-Forwards
- Proxy-Authorization
- Range
- TE
- Expect --- 比如加上 100-continue,The purpose of the 100 (Continue) status (see section 10.1.1) is to allow a client that is sending a request messagewith a request body to determine if the origin server is willing to accept the request (based on the request headers)before the client sends the request body.(只有当Server传回一个100-continue的reponse之后,再继续传输body,否则就不在传输body了)
2 Response
Status Code:
There are 5 values for the first digit:
- 1xx: Informational - Request received, continuing process
- 2xx: Success - The action was successfully received, understood, and accepted
- 3xx: Redirection - Further action must be taken in order to complete the request
- 4xx: Client Error - The request contains bad syntax or cannot be fulfilled
- 5xx: Server Error - The server failed to fulfill an apparently valid request
head:
小组1:
- Accept-Ranges
- ETag
- Location
- Proxy-Authenticate
- Retry-After
- Server --- contains information about the software used by the origin server to handle the request
- WWW-Authenticate
小组1中其他的,说实话,不是特别常用。
小组2:
- Age
- Vary
也用于Cache Control,后面一起讲。
3 Entity
head:
小组1:
- Content-Encoding --- used as a modifier to the compressed enconding type
- Content-Language --- describes the natural language(s) of the intended audience for the enclosed entity.
- Content-Length --- indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient
- Content-Location --- defines the base URI for the entity
- Content-MD5 --- an MD5 digest of the entity-body for the purpose of providing an end-to-end message integrity check (MIC) of the entity-body
- Content-Range --- 不常用,可先不看
- Content-Type --- indicates the media type of the entity-body sent to the recipient
小组1提供的是发送的entity的属性,当从client使用POST method发送数据给server时候,需要包含content属性。从server给client response的时候,也需要包含content属性。下面为一例:
- Content-Type text/html; charset=UTF-8
- Content-Encoding gzip
- Content-Length 7176
小组2:
- Expires
- Last-Modified
这两项也是用于Cache Control,会在下面Cache中一起说。
4 Cache Control
为了提高性能,才多出来这么多的head field用于cache control。
Goal:
The goal of caching in HTTP/1.1 is to eliminate the need to send requests in many cases, and to eliminate the need to send full responses in many other cases. The former reduces the number of network round-trips required for many operations; we use an “expiration”mechanism for this purpose (see section 13.2). The latter reduces network bandwidth requirements; we use a
“validation” mechanism for this purpose (see section 13.3).
expiration:
HTTP caching works best when caches can entirely avoid making requests to the origin server. The primary
mechanism for avoiding requests is for an origin server to provide an explicit expiration time in the future, indicating that a response MAY be used to satisfy subsequent requests. In other words, a cache can return a fresh response without first contacting the server.
这里跟expire关联到的head field如下:
小组1:
- Date --- the date and time at which the message was originated, having the same semantics as orig-date
- Age --- the sender's/cache's estimate of the amount of time since the response (or its revalidation) was generated at the origin server.
小组1(均在response中出现)的用于计算当前的response的实际的age,因为只有real-age不超过response的fresh-lifetime,这个response才有效。
实际的计算公式是:corrected_received_age = max(now - date_value, age_value).
小组2:
- Expires --- The Expires entity-header field gives the date/time after which the response is considered stale.
- Cache-Control -- specify directives that MUST be obeyed by all caching mechanisms along the request/response chain.These directives typically override the default caching algorithms.
小组2中的Expires和Cache-Control的max-age directive用于计算fresh-lifetime.
- The max-age directive takes priority over Expires, so if max-age is present in a response, the calculation is simply: freshness_lifetime = max_age_value
- Otherwise, if Expires is present in the response, the calculation is:
freshness_lifetime = expires_value - date_value
例子:
- Expires: Fri, 05 Oct 2012 00:00:00 GMT
- Date: Fri, 07 Oct 2011 22:38:05 GMT
- Cache-Control: public, max-age=31536000
- Age: 190190
计算出来后,如果age < freshness_lifetime,那么这个response is fresh, or it should be validated!
Validation
When a cache has a stale entry that it would like to use as a response to a client’s request, it first has to check with the origin server to see if its cached entry is still usable. We call this “validating” the cache entry. Since we do not want to have to pay the overhead of retransmitting the full response if the cached entry is good, and we do not want to pay the overhead of an extra round trip if the cached entry is invalid, the HTTP/1.1 protocol supports the use of conditional methods.
小组1:
When an origin server generates a full response, it attaches some sort of validator to it, which is kept with the cache entry.
在response中的一些validator:
- Last-Modified --- 最后修改的时间。 In simple terms, a cache entry is considered to be valid if the entity has not been modified since the Last-Modified value.
- ETag -- 不常用。虽然内容很多,不提了这里。
小组2:
A conditional request looks exactly the same as a normal request for the same resource, except that it
carries a special header (which includes the validator) that implicitly turns the method (usually, GET) into a
conditional。
在request中的一些validator:
- If-Modified-Since or If-Unmodified-Since -- 如果response过期的话,就non-fresh response进行validate, 根据response传过来的Last-Modified.
- If-Match, If-None-Match, or If-Range --- 使用ETag进行校验的手法,暂时用不到。