🧹 Refactor etag stale checking

This commit is contained in:
Larry Lv 2020-07-26 17:48:24 -07:00
parent 415a6026c1
commit fc85699a95
No known key found for this signature in database
GPG Key ID: 8500335BD174D408
2 changed files with 17 additions and 18 deletions

10
ctx.go
View File

@ -449,15 +449,7 @@ func (ctx *Ctx) Fresh() bool {
if etag == "" {
return false
}
var etagStale = true
var matches = parseTokenList(getBytes(noneMatch))
for _, match := range matches {
if match == etag || match == "W/"+etag || "W/"+match == etag {
etagStale = false
break
}
}
if etagStale {
if isEtagStale(etag, getBytes(noneMatch)) {
return false
}

View File

@ -163,14 +163,14 @@ func getOffer(header string, offers ...string) string {
return ""
}
// Adapted from:
// https://github.com/jshttp/fresh/blob/10e0471669dbbfbfd8de65bc6efac2ddd0bfa057/index.js#L110
func parseTokenList(noneMatchBytes []byte) []string {
func isEtagStale(etag string, noneMatchBytes []byte) bool {
var (
start int
end int
list []string
start int
end int
matches []string
)
// Adapted from:
// https://github.com/jshttp/fresh/blob/10e0471669dbbfbfd8de65bc6efac2ddd0bfa057/index.js#L110
for i := range noneMatchBytes {
switch noneMatchBytes[i] {
case 0x20:
@ -179,7 +179,7 @@ func parseTokenList(noneMatchBytes []byte) []string {
end = i + 1
}
case 0x2c:
list = append(list, getString(noneMatchBytes[start:end]))
matches = append(matches, getString(noneMatchBytes[start:end]))
start = i + 1
end = i + 1
default:
@ -187,8 +187,15 @@ func parseTokenList(noneMatchBytes []byte) []string {
}
}
list = append(list, getString(noneMatchBytes[start:end]))
return list
matches = append(matches, getString(noneMatchBytes[start:end]))
for _, match := range matches {
if match == etag || match == "W/"+etag || "W/"+match == etag {
return false
}
}
return true
}
func isIPv6(address string) bool {