예제 #1
0
// HTTPCachingHandler middleware that provides HTTP level caching
// using the If-None-Match header. If the value of this header contains
// a matching CurrentSHA this handler will write a 304 status and return.
func HTTPCachingHandler(dcdr client.IFace) func(http.Handler) http.Handler {
	return func(h http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {
			if sha := dcdr.Info().CurrentSHA; sha != "" {
				w.Header().Set(EtagHeader, sha)
			}

			if date := dcdr.Info().LastModifiedDate; date != 0 {
				lmd := time.Unix(date, 0).Format(time.RFC1123)
				w.Header().Set(LastModifiedHeader, lmd)
			}

			SetCacheHeaders(w, r)

			if NotModified(dcdr.Info().CurrentSHA, r) {
				w.WriteHeader(http.StatusNotModified)
				return
			}

			h.ServeHTTP(w, r)
		}

		return http.HandlerFunc(fn)
	}
}
예제 #2
0
// ScopeMapFromRequest helper method for returning a FeatureMap scoped to
// the values found in DcdrScopesHeader.
func ScopeMapFromRequest(c client.IFace, r *http.Request) *models.FeatureMap {
	return c.WithScopes(GetScopes(r)...).ScopedMap()
}