Skip to content

Commit

Permalink
修复 304 处理问题
Browse files Browse the repository at this point in the history
  • Loading branch information
movsb committed Mar 27, 2024
1 parent b5cd598 commit 516b113
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
6 changes: 6 additions & 0 deletions modules/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ var (
GitCommit string
)

func init() {
if GitCommit == `` {
GitCommit = `HEAD`
}
}

// AddCommands ...
func AddCommands(rootCmd *cobra.Command) {
versionCmd := &cobra.Command{
Expand Down
4 changes: 2 additions & 2 deletions theme/blog/statics/scripts/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ TaoBlog.fn.fadeIn = function(elem, callback) {
elem.style.display = 'block';
if (typeof callback == 'function') {
elem.addEventListener('animationend', function(event) {
console.log('fade-in animationend');
// console.log('fade-in animationend');
callback();
}, { once: true});
}
Expand All @@ -113,7 +113,7 @@ TaoBlog.fn.fadeIn = function(elem, callback) {
TaoBlog.fn.fadeOut = function(elem, callback) {
elem.classList.remove('fade-in');
elem.addEventListener('animationend', function(event) {
console.log('fade-out animationend');
// console.log('fade-out animationend');
elem.style.display = 'none';
if (typeof callback == 'function') {
callback();
Expand Down
56 changes: 29 additions & 27 deletions theme/modules/handle304/handle304.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package handle304

import (
"fmt"
"net/http"
"time"

"github.com/movsb/taoblog/modules/version"
)

const httpgmtFormat = "Mon, 02 Jan 2006 15:04:05 GMT"
const httpGmtFormat = "Mon, 02 Jan 2006 15:04:05 GMT"

// Requester ...
type Requester interface {
Expand All @@ -19,11 +20,15 @@ type Responder interface {
Response(w http.ResponseWriter)
}

// ArticleRequest ...
// ArticleRequest 判断是否符合 304 请求。
// 如果符合,返回 true;否则,返回 false。
func ArticleRequest(w http.ResponseWriter, req *http.Request, modified time.Time) bool {
handlers := []Requester{
NotModified{modified},
CommitMatch{},
EntityTag{
modified: modified,
version: version.GitCommit,
},
}
for _, handler := range handlers {
if !handler.Request(req) {
Expand All @@ -38,7 +43,10 @@ func ArticleRequest(w http.ResponseWriter, req *http.Request, modified time.Time
func ArticleResponse(w http.ResponseWriter, modified time.Time) {
handlers := []Responder{
NotModified{modified},
CommitMatch{},
EntityTag{
modified: modified,
version: version.GitCommit,
},
}
for _, handler := range handlers {
handler.Response(w)
Expand All @@ -53,40 +61,34 @@ type NotModified struct {
// Request ...
func (nm NotModified) Request(req *http.Request) bool {
h := req.Header.Get(`If-Modified-Since`)
t, _ := time.ParseInLocation(httpgmtFormat, h, time.UTC)
t, _ := time.ParseInLocation(httpGmtFormat, h, time.UTC)
return t.Equal(nm.Modified)
}

// Response ...
func (nm NotModified) Response(w http.ResponseWriter) {
w.Header().Add(`Last-Modified`, nm.Modified.UTC().Format(httpgmtFormat))
w.Header().Add(`Last-Modified`, nm.Modified.UTC().Format(httpGmtFormat))
}

// CommitMatch ...
type CommitMatch struct {
// 格式:文章的修改日期-博客版本号
// Typically, the ETag value is a hash of the content, a hash of the last modification timestamp, or just a revision number.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
type EntityTag struct {
modified time.Time
version string
}

func (cm EntityTag) format() string {
return fmt.Sprintf(`"%d-%s"`, cm.modified.Unix(), cm.version)
}

// Request ...
func (cm CommitMatch) Request(req *http.Request) bool {
commitCookie, err := req.Cookie(`commit`)
if err != nil {
return false
}
return commitCookie.Value == version.GitCommit
func (cm EntityTag) Request(req *http.Request) bool {
haystack := req.Header.Get(`If-None-Match`)
return haystack == cm.format()
}

// Response ...
func (cm CommitMatch) Response(w http.ResponseWriter) {
v := version.GitCommit
if v == `` {
v = `HEAD`
}
http.SetCookie(w, &http.Cookie{
Name: `commit`,
Value: v,
Path: `/`,
MaxAge: 0,
Secure: false,
HttpOnly: true,
})
func (cm EntityTag) Response(w http.ResponseWriter) {
w.Header().Add(`ETag`, cm.format())
}

0 comments on commit 516b113

Please sign in to comment.