mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
🧹 Refactor etag stale checking
This commit is contained in:
parent
415a6026c1
commit
fc85699a95
10
ctx.go
10
ctx.go
@ -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
|
||||
}
|
||||
|
||||
|
25
utils.go
25
utils.go
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user