diff --git a/README.md b/README.md index 2fc353b..142e693 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/fresh.go b/fresh.go index 16edf0e..fa4158e 100644 --- a/fresh.go +++ b/fresh.go @@ -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 { @@ -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 +}