-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: support frech check function
- Loading branch information
Showing
5 changed files
with
325 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2021 Tree Xie | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
package elton | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"regexp" | ||
"time" | ||
) | ||
|
||
var noCacheReg = regexp.MustCompile(`(?:^|,)\s*?no-cache\s*?(?:,|$)`) | ||
|
||
var weekTagPrefix = []byte("W/") | ||
|
||
func parseTokenList(buf []byte) [][]byte { | ||
end := 0 | ||
start := 0 | ||
count := len(buf) | ||
list := make([][]byte, 0) | ||
for index := 0; index < count; index++ { | ||
switch int(buf[index]) { | ||
// 空格 | ||
case 0x20: | ||
if start == end { | ||
end = index + 1 | ||
start = end | ||
} | ||
// , 号 | ||
case 0x2c: | ||
list = append(list, buf[start:end]) | ||
end = index + 1 | ||
start = end | ||
default: | ||
end = index + 1 | ||
} | ||
} | ||
list = append(list, buf[start:end]) | ||
return list | ||
} | ||
|
||
func parseHTTPDate(date string) int64 { | ||
t, err := time.Parse(time.RFC1123, date) | ||
if err != nil { | ||
return 0 | ||
} | ||
return t.Unix() | ||
} | ||
|
||
// isFresh returns true if the data is fresh | ||
func isFresh(modifiedSince, noneMatch, cacheControl, lastModified, etag []byte) bool { | ||
if len(modifiedSince) == 0 && len(noneMatch) == 0 { | ||
return false | ||
} | ||
if len(cacheControl) != 0 && noCacheReg.Match(cacheControl) { | ||
return false | ||
} | ||
// if none match | ||
if len(noneMatch) != 0 && (len(noneMatch) != 1 || noneMatch[0] != byte('*')) { | ||
if len(etag) == 0 { | ||
return false | ||
} | ||
matches := parseTokenList(noneMatch) | ||
etagStale := true | ||
for _, match := range matches { | ||
if bytes.Equal(match, etag) { | ||
etagStale = false | ||
break | ||
} | ||
if bytes.HasPrefix(match, weekTagPrefix) && bytes.Equal(match[2:], etag) { | ||
etagStale = false | ||
break | ||
} | ||
if bytes.HasPrefix(etag, weekTagPrefix) && bytes.Equal(etag[2:], match) { | ||
etagStale = false | ||
break | ||
} | ||
} | ||
if etagStale { | ||
return false | ||
} | ||
} | ||
// if modified since | ||
if len(modifiedSince) != 0 { | ||
if len(lastModified) == 0 { | ||
return false | ||
} | ||
lastModifiedUnix := parseHTTPDate(string(lastModified)) | ||
modifiedSinceUnix := parseHTTPDate(string(modifiedSince)) | ||
if lastModifiedUnix == 0 || modifiedSinceUnix == 0 { | ||
return false | ||
} | ||
if modifiedSinceUnix < lastModifiedUnix { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Fresh returns fresh status by judget request header and response header | ||
func Fresh(reqHeader http.Header, resHeader http.Header) bool { | ||
modifiedSince := []byte(reqHeader.Get(HeaderIfModifiedSince)) | ||
noneMatch := []byte(reqHeader.Get(HeaderIfNoneMatch)) | ||
cacheControl := []byte(reqHeader.Get(HeaderCacheControl)) | ||
|
||
lastModified := []byte(resHeader.Get(HeaderLastModified)) | ||
etag := []byte(resHeader.Get(HeaderETag)) | ||
|
||
return isFresh(modifiedSince, noneMatch, cacheControl, lastModified, etag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// MIT License | ||
|
||
// Copyright (c) 2021 Tree Xie | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
package elton | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func createRequestHeader(modifiedSince, noneMatch, cacheControl string) http.Header { | ||
req := httptest.NewRequest("GET", "/users/me", nil) | ||
header := req.Header | ||
if modifiedSince != "" { | ||
header.Set(HeaderIfModifiedSince, modifiedSince) | ||
} | ||
|
||
if noneMatch != "" { | ||
header.Set(HeaderIfNoneMatch, noneMatch) | ||
} | ||
|
||
if cacheControl != "" { | ||
header.Set(HeaderCacheControl, cacheControl) | ||
} | ||
return header | ||
} | ||
|
||
func createResponseHeader(lastModified, eTag string) http.Header { | ||
resp := httptest.NewRecorder() | ||
header := resp.Header() | ||
if lastModified != "" { | ||
header.Set(HeaderLastModified, lastModified) | ||
} | ||
|
||
if eTag != "" { | ||
header.Set(HeaderETag, eTag) | ||
} | ||
return header | ||
} | ||
func TestFresh(t *testing.T) { | ||
assert := assert.New(t) | ||
// when a non-conditional GET is performed | ||
reqHeader := createRequestHeader("", "", "") | ||
resHeader := createResponseHeader("", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when ETags match | ||
reqHeader = createRequestHeader("", "\"foo\"", "") | ||
|
||
resHeader = createResponseHeader("", "\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
reqHeader = createRequestHeader("", "W/\"foo\"", "") | ||
resHeader = createResponseHeader("", "\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
reqHeader = createRequestHeader("", "\"foo\"", "") | ||
resHeader = createResponseHeader("", "W/\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
// when ETags mismatch | ||
reqHeader = createRequestHeader("", "\"foo\"", "") | ||
resHeader = createResponseHeader("", "\"bar\"") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when at least one matches | ||
reqHeader = createRequestHeader("", " \"bar\" , \"foo\"", "") | ||
resHeader = createResponseHeader("", "\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
// when eTag is missing | ||
reqHeader = createRequestHeader("", "\"foo\"", "") | ||
resHeader = createResponseHeader("", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when ETag is weak | ||
reqHeader = createRequestHeader("", "W/\"foo\"", "") | ||
resHeader = createResponseHeader("", "W/\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
resHeader = createResponseHeader("", "\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
// when ETag is strong | ||
reqHeader = createRequestHeader("", "\"foo\"", "") | ||
resHeader = createResponseHeader("", "\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
// weak eTag | ||
resHeader = createResponseHeader("", "W/\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
// when * is given | ||
reqHeader = createRequestHeader("", "*", "") | ||
resHeader = createResponseHeader("", "\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
reqHeader = createRequestHeader("", "*, \"bar\"", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when modified since the date | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "", "") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 01:00:00 GMT", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when unmodified since the date | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 01:00:00 GMT", "", "") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 00:00:00 GMT", "") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
// when Last-Modified is missing | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 01:00:00 GMT", "", "") | ||
resHeader = createResponseHeader("", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// with invalid If-Modified-Since date | ||
reqHeader = createRequestHeader("foo", "", "") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 00:00:00 GMT", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// with invalid Last-Modified date | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "", "") | ||
resHeader = createResponseHeader("foo", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when requested with If-Modified-Since and If-None-Match | ||
|
||
// both match | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"", "") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"") | ||
assert.True(Fresh(reqHeader, resHeader)) | ||
|
||
// when only ETag matches | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"", "") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 01:00:00 GMT", "\"foo\"") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when only Last-Modified matches | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"", "") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"bar\"") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when none match | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"", "") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 01:00:00 GMT", "\"bar\"") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when requested with Cache-Control: no-cache | ||
reqHeader = createRequestHeader("", "", "no-cache") | ||
resHeader = createResponseHeader("", "") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when ETags match | ||
reqHeader = createRequestHeader("", "\"foo\"", "no-cache") | ||
resHeader = createResponseHeader("", "\"foo\"") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
|
||
// when unmodified since the date | ||
reqHeader = createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "", "no-cache") | ||
resHeader = createResponseHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"") | ||
assert.False(Fresh(reqHeader, resHeader)) | ||
} | ||
|
||
func BenchmarkFresh(b *testing.B) { | ||
b.ResetTimer() | ||
reqHeader := createRequestHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"", "") | ||
resHeader := createResponseHeader("Sat, 01 Jan 2000 00:00:00 GMT", "\"foo\"") | ||
for i := 0; i < b.N; i++ { | ||
Fresh(reqHeader, resHeader) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters