HTTP之Last-Modified、Etage、If-Modified-Since理论与实践(C++ Qt实现)
目錄
?
?
基本理論
博主例子
?
基本理論
首先要理解緩存是如何被創(chuàng)建的:
瀏覽器首先先本地緩存發(fā)起創(chuàng)建請(qǐng)求,如果命中則返回?cái)?shù)據(jù)給瀏覽器;
如果沒(méi)有命中,就向代理服務(wù)器發(fā)起緩存,如果命中代理服務(wù)器會(huì)發(fā)給瀏覽器以及本地緩存;
如果沒(méi)命中,則向資源服務(wù)器發(fā)起創(chuàng)建請(qǐng)求,然后資源服務(wù)器再給代理緩存,本地緩存,瀏覽器數(shù)據(jù);
?
如果要實(shí)現(xiàn)這樣的功能:
1. 瀏覽器本地有緩存,但每次還要向服務(wù)器發(fā)起請(qǐng)求,看看資源是不是更新了;
2. 如果更新了服務(wù)器發(fā)送新資源給瀏覽器,如果沒(méi)更新就叫瀏覽器讀取緩存;
?
要使用HTTP實(shí)現(xiàn)上面的功能,要具備以下的知識(shí)點(diǎn):
1.? 服務(wù)器響應(yīng)頭中的no-cahce為每次發(fā)起請(qǐng)求都要在服務(wù)器那邊驗(yàn)證下,如果服務(wù)器能讓瀏覽器使用本地緩存,才能使用。no-store為本地和代理服務(wù)器都不能存儲(chǔ)緩存;每次都要拿新的。
2. 服務(wù)器響應(yīng)頭中有兩根驗(yàn)證頭,一個(gè)是Last-Modified和一個(gè)Etag。
3. Last-Modified為上次修改時(shí)間,配合請(qǐng)求頭中的If-Modified或If-Unmodified-Since使用,對(duì)比上傳修改時(shí)間以驗(yàn)證資源是否需要更新。
4. Etag為數(shù)字簽名,配合If-Match或If-Non-Match使用,對(duì)比資源的簽名判斷是否使用緩存。
?
博主例子
服務(wù)器運(yùn)行截圖如下:
這個(gè)是回正常的200的HTTP數(shù)據(jù)包;
這樣是回304讀取本地緩存的包,
先把他調(diào)到正常會(huì)HTTP包;
瀏覽器數(shù)據(jù)如下:
這里看看其大小:
當(dāng)變?yōu)榛?04時(shí):
發(fā)現(xiàn)大小改變了:
但文本內(nèi)容還是沒(méi)變:
看看數(shù)據(jù)包詳細(xì)信息:‘
程序結(jié)構(gòu)如下:
源碼如下:
widget.h
#ifndef WIDGET_H #define WIDGET_H#include <QWidget>QT_BEGIN_NAMESPACE class QTcpServer; class QTcpSocket; QT_END_NAMESPACEnamespace Ui { class Widget; }class Widget : public QWidget {Q_OBJECTpublic:Widget(QWidget *parent = 0);~Widget();protected slots:void newConnectionSlot80();void errorStringSlot80();void sendMsg80();void btnClicked();private:Ui::Widget *ui;QTcpServer *m_tcpServer80;QTcpSocket *m_tcpSocket80;};#endif // WIDGET_Hmain.cpp
#include "widget.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); }widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QTcpServer> #include <QDebug> #include <QTcpSocket>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget) {ui->setupUi(this);m_tcpServer80 = new QTcpServer(this);m_tcpServer80->listen(QHostAddress::Any, 80);connect(m_tcpServer80, SIGNAL(newConnection()), this, SLOT(newConnectionSlot80()));connect(m_tcpServer80, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(errorStringSlot80()));connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(btnClicked())); }Widget::~Widget() {delete ui;m_tcpServer80->close(); }void Widget::newConnectionSlot80() {qDebug() << "newConnectionSlot80() called";m_tcpSocket80 = m_tcpServer80->nextPendingConnection();sendMsg80();//connect(m_tcpSocket80, SIGNAL(readyRead()), this, SLOT(sendMsg80()));//connect(m_tcpSocket80, SIGNAL(connected()), this, SLOT(sendMsg80())); }void Widget::errorStringSlot80() {qDebug() << m_tcpServer80->errorString(); }void Widget::sendMsg80() {QString contentStr;QString str;if(ui->pushButton->text() == "正常回HTTP包"){contentStr = "<html>""<head>""<title>""Socket 80""</title>""</head>""<body>""<h1>Socket 80</h1>""</body>""</html>";//send msgstr = "HTTP/1.1 200 OK\r\n";str.append("Server:nginx\r\n");str.append("Content-Type:text/html\r\n");str.append("Access-Control-Allow-Origin: *\r\n");str.append("Connection:keep-alive\r\n");str.append("Cache-Control: max-age=200000\r\n");str.append("Last-Modified: 777777\r\n");str.append("Etage: 888888\r\n");str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));str.append(contentStr);//qDebug() << str;}else{str = "HTTP/1.1 304 OK\r\n";str.append("Server:nginx\r\n");str.append("Content-Type:text/html\r\n");str.append("Access-Control-Allow-Origin: *\r\n");str.append("Connection:keep-alive\r\n");str.append("Cache-Control: max-age=200000, no-cache\r\n");str.append("Last-Modified: 777777\r\n");str.append("Etage: 888888\r\n");str.append(QString("Content-Length:%1\r\n\r\n").arg(contentStr.size()));}m_tcpSocket80->write(str.toStdString().c_str()); }void Widget::btnClicked() {if(ui->pushButton->text() == "正?;豀TTP包"){ui->pushButton->setText("回304包");}else{ui->pushButton->setText("正?;豀TTP包");} }?
源碼打包下載地址:
https://github.com/fengfanchen/Qt/tree/master/HTTPLast-Modified
總結(jié)
以上是生活随笔為你收集整理的HTTP之Last-Modified、Etage、If-Modified-Since理论与实践(C++ Qt实现)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Git笔记-Connection res
- 下一篇: Spring Boot笔记-目前对Web