请求报文格式
1)请求行,第一行是请求行:请求方法 URL路径 HTTP版本号 , 如:GET http://news.baidu.com/ HTTP/1.1
2)请求头,如:
Host: news.baidu.com
Connection: keep-alive
Accept: text/html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN
3)请求体,get方法不适用,post方法提交数据时,用于存放请求的参数
响应报文格式
1)响应行, 包括:HTTP协议版本号 状态码 状态码的文本描述信息
如:HTTP/1.0 200 OK
2)响应头, 如:
Content-Encoding: gzip
Content-Type: text/html
Date: Sun, 10 Apr 2016 11:41:40 GMT
Server: Apache
Connection: close
3)响应体,, 响应头后面跟着一个回车换行,后面的内容就是响应体部分
常用的HTTP头部
1.Accept
告诉WEB服务器自己接受什么介质类型,/ 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type。
对应的响应头:Content-Type
2.Accept-Encoding
申明自己接收的编码方法,(gzip,deflate)
对应的响应头:Content-Encoding
3.Accept-Language
申明自己接收的语言
对应的响应头:Content-Language
4.Connection
请求:close(告诉WEB服务器,在完成本次请求的响应后,断开连接,不要等待本次连接的后续请求了)。
keepalive(告诉WEB服务器,在完成本次请求的响应后,保持连接,等待本次连接的后续请求)。
响应:close(连接已经关闭)。
keepalive(连接保持着,在等待本次连接的后续请求)。
5.Cache-Control
(HTTP 1.1版本时引入,HTTP 1.0版本使用Pragma)
Cache-Control 可以出现在请求头和响应头中,
若出现在请求头中,该头部可能的值:
1)no-cache 表示本次请求不能使用缓存,也就是即便有缓存也必须向源服务器请求结果。
2) no-store 不缓存响应
3)max-age=xxx (xxx is numeric)
如: Cache-Control: max-age=31536000
表明客户端愿接受这样一个响应,此响应的年龄小于等于 max-age 指定的时间
4)max-stale
表明客户端愿接受已经过期的响应,超过的时间不超过在 max-stale 里指定秒数,即:
current_age <= freshness_lifetime + max-stale 就认为是新鲜的
5)min-fresh
表明客户端愿接受一个这样的响应,其保鲜寿命不小于响应当前年龄加上min-fresh 指定的时间之和,即:
current_age + min-fresh <= freshness_lifetime 才认为是新鲜的
6)only-if-cached 只用已经缓存的响应,并且不发网路请求去验证缓存
若出现在响应头中,该头部可能的值:
1)public 指明响应可被任何缓存保存。
2)private 私有缓存可以缓存此响应
3)no-cache 必须要与原服务器再验证成功之后才能作为响应返回给客户端
4)no-store
5)must-revalidate 缓存必须与原始服务器再验证之后才能给客户端
6)max-age=xxx
6.Expires
Expires头是一个绝对时间,表示过期时间。
如:Expires: Tue, 04 Apr 2017 16:01:25 GMT
Expires在http1.0中,代表该资源的过期时间。使用的是绝对的时间来标记资源的过期时间,但是由于服务器与客户端之间的时间可能没有同步,此时会导致缓存的有效期计算不准确。
Expires和Cache-Control: max-age=xxx均是用来控制缓存的过期时间的,它们的区别?
1).Cache-Control: max-age=xxx是一个相对时间,Expires是一个绝对时间。
2).Cache-Control 优先级高于Expires,即如果同时存在,Cache-Control会覆盖Expires。
3).Cache-Control 在HTTP 1.1版本引入,Expires在http1.0版本引入。
7.ETag/If-None-Match
ETag 实体标签,如果资源被修改了,那么它的Etag值就会被修改
If-None-Match
如果服务器上资源的实体标签与指定的不匹配,则返回200,并把新的Etag值和资源返回,否则返回304
8.Last-Modified/If-Modified-Since
Last-Modified:WEB 服务器上资源的最后修改时间
如:Last-Modified:Tue, 06 May 2008 02:42:43 GMT
If-Modified-Since
如果自指定的时间之后,文档发生了变化,就返回新的修改时间和文档内容,客户端用新的数据覆盖本地缓存。
如果文档没有变化,返回304 Not Modified,只返回一些需要更新的Header信息。
如: If-Modified-Since:Thu, 10 Apr 2008 09:14:42 GMT
9.Date
响应在原始服务器生成时的时间,是绝对时间
如: Date: Tue, 15 Nov 1994 08:12:31 GMT
10.Age
响应从原始服务器产生到现在经过的时间 ,单位 :秒
响应的状态码
状态码(3位数字)分类:
-
1xx:通知——仅在与HTTP服务器沟通时使用
100 Continue -
2xx:成功——成功收到、理解和接受动作
200 OK -
3xx:重定向——为完成请求,必须进一步采取措施
301 Moved Permanently、
302 Move temporarily、
304 Not Modified -
4xx:客户端错误——请求包含错误的语法或不能完成
403 Forbidden、
404 Not Found -
5xx:服务器端错误——服务器不能完成明显合理的请求
500 Internal Server Error 、
503 Service Unavailable
http请求整个流程
http请求-响应机制
http新鲜度检测算法
步骤:
1)计算缓存的当前年龄值(current_age)
2)计算缓存的保鲜寿命(freshness_lifetime)
3)如何判断缓存是否新鲜?
response_is_fresh = (current_age < freshness_lifetime )
1)计算当前年龄值
/*
* age_value
* is the value of Age: header received by the cache with this response.
* date_value
* is the value of the origin server's Date: header
* request_time
* is the (local) time when the cache made the request
* that resulted in this cached response
* response_time
* is the (local) time when the cache received the response
* now
* is the current (local) time
*/
apparent_age = max(0, response_time - date_value); //缓存收到响应时响应的年龄
corrected_received_age = max(apparent_age, age_value);
response_delay = response_time - request_time;
corrected_initial_age = corrected_received_age + response_delay;
resident_time = now - response_time; //即收到响应到现在的时间间隔
current_age = corrected_initial_age + resident_time;
2)计算保鲜寿命
保鲜寿命根据 Expires 头域和 Cache-Control 头域里“max-age”控制指令的值来计算,步骤:
1)Cache-Control:max-age 指令优于 Expires 头域执行,所以如果 Cache-Control:max-age 出现在响应里,那么: freshness_lifetime = max_age_value ,计算结束,否则到步骤2)
2)若Expires 头域出现在响应里,则:
freshness_lifetime = expires_value - date_value
3)判断缓存是否新鲜
response_is_fresh = (current_age < freshness_lifetime )
即如果满足: current_age < freshness_lifetime, 则缓存是新鲜的。