Skip to content

Commit

Permalink
0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidCai1111 committed Nov 19, 2016
1 parent 84cfa29 commit 29d7c02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ resHeader.Set(headers.LastModified, "Mon, 14 Nov 2016 22:05:47 GMT")
fresh.IsFresh(reqHeader, resHeader)
// -> true
```

```go
reqHeader, resHeader := make(http.Header), make(http.Header)

resHeader.Set(headers.IfUnmodifiedSince, "Mon, 14 Nov 2016 22:05:47 GMT")
reqHeader.Set(headers.LastModified, "Mon, 14 Nov 2016 22:05:49 GMT")

fresh.IsFresh(reqHeader, resHeader)
// -> true
```
24 changes: 15 additions & 9 deletions fresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Version is this package's verison
const Version = "0.3.0"
const Version = "0.3.1"

// IsFresh check whether cache can be used in this HTTP request
func IsFresh(reqHeader http.Header, resHeader http.Header) bool {
Expand Down Expand Up @@ -67,21 +67,27 @@ func checkEtagMatch(etagsToMatch []string, etag string) bool {
}

func checkModifedMatch(lastModified, ifModifiedSince string) bool {
if lm, err := time.Parse(http.TimeFormat, lastModified); err == nil {
if ims, err := time.Parse(http.TimeFormat, ifModifiedSince); err == nil {
return lm.Before(ims)
}
if lm, ims, ok := parseTimePairs(lastModified, ifModifiedSince); ok == true {
return lm.Before(ims)
}

return false
}

func checkUnmodifedMatch(lastModified, ifUnmodifiedSince string) bool {
if lm, err := time.Parse(http.TimeFormat, lastModified); err == nil {
if ius, err := time.Parse(http.TimeFormat, ifUnmodifiedSince); err == nil {
return lm.After(ius)
}
if lm, ius, ok := parseTimePairs(lastModified, ifUnmodifiedSince); ok == true {
return lm.After(ius)
}

return false
}

func parseTimePairs(s1, s2 string) (t1 time.Time, t2 time.Time, ok bool) {
if t1, err := time.Parse(http.TimeFormat, s1); err == nil {
if t2, err := time.Parse(http.TimeFormat, s2); err == nil {
return t1, t2, true
}
}

return t1, t2, false
}

0 comments on commit 29d7c02

Please sign in to comment.