关于Etag/If-None-Match的用法

if (method_exists($controller, $action)) {
         ob_start();
            $controller -> {$action}();
            $content = ob_get_contents();
            ob_end_clean();
                        
            $etag = md5($content); //计算输出内容的md5
            fheader('Etag: ' . $etag); //服务器端输出Etag header
   //判断浏览器发送过来的If-None-Match头与服务器端的etag值是否相等
            if ($_SERVER['HTTP_IF_NONE_MATCH'] == $etag) {
             write_log(sprintf("%s Client HTTP_IF_NONE_MATCH:|%s| and Server Etag:|%s| is same, send 304 header!", __METHOD__, $_SERVER['HTTP_IF_NONE_MATCH'], $etag));
             fheader('HTTP/1.1 304 Not Modified!'); //相等,直接向浏览器发送304状态码,浏览器则直接取本地的缓存数据,节省带宽,提高页面响应速度
            } else {
             write_log(sprintf("%s file content already modified, output content!", __METHOD__));
             echo $content;
            }
        } else {
            $error = get_class($controller) . "::$action not exists!";
            die($error);
            write_log($error);
        }

 

注意服务器端(PHP)接收浏览器发送过来的If-Modified-Since及If-None-Match时服务器端存在变量
$_SERVER['HTTP_IF_MODIFIED_SINCE']及$_SERVER['HTTP_IF_NONE_MATCH']

---------------------------------------------------------
http://hi.baidu.com/kakaying1985/blog/item/286a13ca90774882c817685d.html
Last-Modified和Etags如何帮助提高性能?
         聪明的开发者会把Last-Modified 和ETags请求的http报头一起使用,这样可利用客户端(例如浏览器)的缓存。因为服务器首先产生 Last-Modified/Etag标记,服务器可在稍后使用它来判断页面是否已经被修改。本质上,客户端通过将该记号传回服务器要求服务器验证其(客户端)缓存。 
         过程如下:
                 1. 客户端请求一个页面(A)。 
                 2. 服务器返回页面A,并在给A加上一个Last-Modified/ETag。 
                 3. 客户端展现该页面,并将页面连同Last-Modified/ETag一起缓存。 
                 4. 客户再次请求页面A,并将上次请求时服务器返回的Last-Modified/ETag一起传递给服务器。 
                 5. 服务器检查该Last-Modified或ETag,并判断出该页面自上次客户端请求之后还未被修改,直接返回响应304和一个空的响应体。
---------------------------------------------------------

-------------------------------------------
下面是用firebug监控到的

Request Headers
Host localhost
User-Agent Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16
Accept text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language zh-cn,zh;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Cookie 61m_auth=97S0CrUoaHtE8qlkgA0hMJjzgFQpXZl1r6HBCbj8OoreWZwoeysL%2FLedoA; 61m_cookietime=2592000; 61m_sid=6jgxcA
If-Modified-Since Tue, 19 Aug 2008 06:49:35GMT
If-None-Match 7936caeeaf6aee6ff8834b381618b513
Cache-Control max-age=0

Response Headers
Date Tue, 19 Aug 2008 06:50:19 GMT
Server Apache/2.0.47 (Win32) PHP/5.2.3
X-Powered-By PHP/5.2.3
Expires Tue, 19 Aug 2008 07:00:19 GMT
Last-Modified Tue, 19 Aug 2008 06:49:35GMT
Etag 7936caeeaf6aee6ff8834b381618b513
Content-Encoding gzip
Vary Accept-Encoding
Content-Length 421
Content-Type text/html; charset=utf-8
-------------------------------------------

-------------------------------------------
下面是用Http look监控到的内容,说明304头已经生效了
Request
GET /i33edu_v2/web/test/display_group HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.16) Gecko/20080702 Firefox/2.0.0.16
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: 61m_auth=97S0CrUoaHtE8qlkgA0hMJjzgFQpXZl1r6HBCbj8OoreWZwoeysL%2FLedoA; 61m_cookietime=2592000; 61m_sid=6jgxcA
If-Modified-Since: Tue, 19 Aug 2008 06:49:35GMT
If-None-Match: 7936caeeaf6aee6ff8834b381618b513
Cache-Control: max-age=0

Response
HTTP/1.1 304 Not Modified!
Date: Tue, 19 Aug 2008 06:50:19 GMT
Server: Apache/2.0.47 (Win32) PHP/5.2.3
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
Etag: 7936caeeaf6aee6ff8834b381618b513
Expires: Tue, 19 Aug 2008 07:00:19 GMT
Vary: Accept-Encoding

000000  1F 8B 08 00 00 00 00 00  00 03 D2 D5 25 19 D8 24  .?.......艺%.?
000010  15 29 E8 DB 71 A9 94 25  16 29 64 16 2B 24 2A 14  .)?q%.)d.+$*.
000020  97 14 65 E6 A5 EB 28 14  00 A9 12 85 C4 62 A8 00  ?e?.(..?吥b?
000030  54 5D 51 6A 62 8A 42 5A  51 7E AE 42 72 62 72 46  T]Qjb夿ZQ~瓸rbrF
000040  AA 95 0D 50 36 3F 2F DD  AE A6 C6 46 1F CA 54 80  獣.P6?/莓ζF.蔜?BR>000050  2A 25 DD 2D 30 C7 00 00  00 00 FF FF 03 00 4F BC  *%?0?...?..000060  32 45 C7 00 00 00                                 2E?..
O?
-------------------------------------------

-------------------------------------------
下面是用ie7的http watch监控的

GET /i33edu_v2/web/test/display_group HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
If-Modified-Since: Tue, 19 Aug 2008 07:09:35 GMT
If-None-Match: 7936caeeaf6aee6ff8834b381618b513
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; POTU(RR:27120522:0:5231843); Potu-WebBrowser www.potu.com; CIBA)
Host: localhost
Connection: Keep-Alive
Cookie: 61m_sid=nQbrC8

HTTP/1.1 304 Not Modified!
Date: Tue, 19 Aug 2008 07:10:47 GMT
Server: Apache/2.0.47 (Win32) PHP/5.2.3
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
Etag: 7936caeeaf6aee6ff8834b381618b513
Expires: Tue, 19 Aug 2008 07:20:47 GMT
Vary: Accept-Encoding

\u001F?
-------------------------------------------


相关文章

http://http://www.educity.cn/wenda/123218.html

, , ,

Related posts

git系统间换行符的问题CRLF/LF/CR

CR回车 LF换行Windows/Dos CRLF \r\n
Linux/Unix LF \n
MacOS CR \r

一、AutoCRLF
#提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

#提交时转换为LF,检出时不转换
git config --global...

数据采集触发防火墙

通过dns分析,目标网站使用阿里云ECS云盾.网站流量先经过aliyunddos1001.com服务器

经过反复尝试将之前Curl请求改成基于HTTP_Request2的Socket请求,顺利绕过防火墙.
通过tcpdump抓包比较

# socket
23:37:37.498916 IP localhost.58718 > localhost.http: Flags , seq 258486799, win 65495, options [mss...

ubuntu下利用QSS、WPS破解wpa/wpa2

安装airodump-ng、reaver

sudo apt-get install airodump-ng
sudo apt-get install reaver

开启设置无线网卡的监听接口

sudo airodump-ng start mon0

sudo airodump-ng mon0

进行破解

sudo reaver -i mon0 -b...

Latest posts

CloudflareCDN+nginx配置特定IP访问限制

Cloudflare公开中国节点IP段
https://www.cloudflare.com/zh-cn/ips/

# 获取CF代理IP
# 配置加入nginx Server前
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from...

git系统间换行符的问题CRLF/LF/CR

CR回车 LF换行Windows/Dos CRLF \r\n
Linux/Unix LF \n
MacOS CR \r

一、AutoCRLF
#提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

#提交时转换为LF,检出时不转换
git config --global...

数据采集触发防火墙

通过dns分析,目标网站使用阿里云ECS云盾.网站流量先经过aliyunddos1001.com服务器

经过反复尝试将之前Curl请求改成基于HTTP_Request2的Socket请求,顺利绕过防火墙.
通过tcpdump抓包比较

# socket
23:37:37.498916 IP localhost.58718 > localhost.http: Flags , seq 258486799, win 65495, options [mss...

wsl2使用genie守护进程

安装 wsl-transdebian

sudo apt install apt-transport-https

wget -O /etc/apt/trusted.gpg.d/wsl-transdebian.gpg https://arkane-systems.github.io/wsl-transdebian/apt/wsl-transdebian.gpg

chmod a+r /etc/apt/trusted.gpg.d/wsl-transdebian.gpg

cat << EOF > /etc/apt/sources.list.d/wsl-transdebian.list
deb...

CloudFlare的SSL证书浏览器提示不安全原因

Cloudflare Origin CA 证书安装说明

1、先创建CA证书

2、将证书文件下载

3、获取CloudFlare的根证书

4、将根证书合并的xxxxx.pem的后面

再将证书部署到服务器,浏览器刷新显示“连接安全”。

Leave a Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注