Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
feat: support http.Header for check fresh
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Dec 25, 2018
1 parent 9d7cefa commit 24960d8
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 173 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

.PHONY: default test test-cover dev


# for test
test:
go test -race -cover ./...

test-cover:
go test -race -coverprofile=test.out ./... && go tool cover --html=test.out

bench:
go test -bench=. ./
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ HTTP response freshness testing,it is copied from [fresh](https://github.com/j
- `ResponseHeader`

```go
reqHeader = &RequestHeader{
IfNoneMatch: []byte("\"foo\""),
IfModifiedSince: []byte("Sat, 01 Jan 2000 00:00:00 GMT"),
}
resHeader = &ResponseHeader{
ETag: []byte("\"foo\""),
LastModified: []byte("Sat, 01 Jan 2000 00:00:00 GMT"),
}
req := httptest.NewRequest("GET", "/users/me", nil)
resp := httptest.NewRecorder()
// true
Fresh(reqHeader, resHeader)
Fresh(req.Header, resp.Header)
```

### Check
Expand Down
36 changes: 22 additions & 14 deletions fresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package fresh

import (
"bytes"
"net/http"
"regexp"
"time"
)

// RequestHeader 请求头
type RequestHeader struct {
IfModifiedSince []byte
IfNoneMatch []byte
CacheControl []byte
}

// ResponseHeader 响应头
type ResponseHeader struct {
ETag []byte
LastModified []byte
}
const (
// HeaderIfModifiedSince if modified since
HeaderIfModifiedSince = "If-Modified-Since"
// HeaderIfNoneMatch if none match
HeaderIfNoneMatch = "If-None-Match"
// HeaderCacheControl Cache-Control
HeaderCacheControl = "Cache-Control"
// HeaderETag ETag
HeaderETag = "ETag"
// HeaderLastModified last modified
HeaderLastModified = "Last-Modified"
)

var noCacheReg = regexp.MustCompile(`(?:^|,)\s*?no-cache\s*?(?:,|$)`)

Expand Down Expand Up @@ -108,6 +109,13 @@ func Check(modifiedSince, noneMatch, cacheControl, lastModified, etag []byte) bo
}

// Fresh 判断该请求是否 fresh
func Fresh(reqHeader *RequestHeader, resHeader *ResponseHeader) bool {
return Check(reqHeader.IfModifiedSince, reqHeader.IfNoneMatch, reqHeader.CacheControl, resHeader.LastModified, resHeader.ETag)
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 Check(modifiedSince, noneMatch, cacheControl, lastModified, etag)
}
Loading

0 comments on commit 24960d8

Please sign in to comment.