Skip to content

Commit

Permalink
Fix consistent cache control headers for response
Browse files Browse the repository at this point in the history
  • Loading branch information
xorkevin committed May 18, 2023
1 parent 53f5748 commit c4a4585
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions service/cachecontrol/cachecontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type (
// Directive is a cache control directive
Directive string

//Directives are a list of directives
// Directives are a list of directives
Directives []Directive
)

Expand Down Expand Up @@ -96,11 +96,6 @@ func ControlCtx(public bool, directives Directives, maxage int64, etagfunc func(
etag = etagToValue(tag)
}

if val := c.Header(headerIfNoneMatch); etag != "" && val == etag {
c.WriteStatus(http.StatusNotModified)
return
}

finalDirectives := make([]string, 0, 2+len(directives))
if public {
finalDirectives = append(finalDirectives, string(DirPublic))
Expand All @@ -112,9 +107,20 @@ func ControlCtx(public bool, directives Directives, maxage int64, etagfunc func(
finalDirectives = append(finalDirectives, string(i))
}

ccHeader := strings.Join(finalDirectives, ", ")

if val := c.Header(headerIfNoneMatch); etag != "" && val == etag {
c.SetHeader(headerCacheControl, ccHeader)
if etag != "" {
c.SetHeader(headerETag, etag)
}
c.WriteStatus(http.StatusNotModified)
return
}

w2 := &cacheControlWriter{
ResponseWriter: c.Res(),
valCC: strings.Join(finalDirectives, ", "),
valCC: ccHeader,
valETag: etag,
wroteHeader: false,
}
Expand Down

0 comments on commit c4a4585

Please sign in to comment.