Skip to content

Commit 387bf45

Browse files
committed
feat(serverHandler): use unique Vary header for protocol versions
1 parent 53ecdf1 commit 387bf45

File tree

4 files changed

+18
-38
lines changed

4 files changed

+18
-38
lines changed

src/serverHandler/aliasHandler.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ type aliasParam struct {
4040
headersUrls []pathHeaders
4141
headersDirs []pathHeaders
4242

43-
pageVaryV1 string
44-
pageVary string
45-
contentVaryV1 string
46-
contentVary string
43+
pageVary string
44+
contentVary string
4745
}
4846

4947
type aliasHandler struct {
@@ -104,10 +102,8 @@ type aliasHandler struct {
104102
corsUrls []string
105103
corsDirs []string
106104

107-
pageVaryV1 string
108-
pageVary string
109-
contentVaryV1 string
110-
contentVary string
105+
pageVary string
106+
contentVary string
111107

112108
postMiddlewares []middleware.Middleware
113109

@@ -284,10 +280,8 @@ func newAliasHandler(
284280

285281
fileServer: fileServer,
286282

287-
pageVaryV1: ap.pageVaryV1,
288-
pageVary: ap.pageVary,
289-
contentVaryV1: ap.contentVaryV1,
290-
contentVary: ap.contentVary,
283+
pageVary: ap.pageVary,
284+
contentVary: ap.contentVary,
291285

292286
postMiddlewares: p.PostMiddlewares,
293287
}

src/serverHandler/content.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@ var serveContent = func(h *aliasHandler, w http.ResponseWriter, r *http.Request,
1616
func (h *aliasHandler) content(w http.ResponseWriter, r *http.Request, data *responseData) {
1717
header := w.Header()
1818
header.Set("X-Content-Type-Options", "nosniff")
19-
if r.ProtoMajor <= 1 {
20-
if len(h.contentVaryV1) > 0 {
21-
header.Set("Vary", h.contentVaryV1)
22-
}
23-
} else {
24-
if len(h.contentVary) > 0 {
25-
header.Set("Vary", h.contentVary)
26-
}
19+
if len(h.contentVary) > 0 {
20+
header.Set("Vary", h.contentVary)
2721
}
2822
if data.IsDownload {
2923
header.Set("Content-Disposition", "attachment; filename*=UTF-8''"+url.PathEscape(data.ItemName))

src/serverHandler/page.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ func (h *aliasHandler) page(w http.ResponseWriter, r *http.Request, data *respon
6969
if lacksHeader(header, "Cache-Control") {
7070
header.Set("Cache-Control", "public, max-age=0")
7171
}
72-
73-
if r.ProtoMajor <= 1 {
74-
header.Set("Vary", h.pageVaryV1)
75-
} else {
72+
if len(h.pageVary) > 0 {
7673
header.Set("Vary", h.pageVary)
7774
}
7875

src/serverHandler/vhostHandler.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,15 @@ func NewVhostHandler(
6060
restrictAccess := hasRestrictAccess(p.GlobalRestrictAccess, restrictAccessUrls, restrictAccessDirs)
6161

6262
// `Vary` header
63-
pageVarys := make([]string, 0, 4)
64-
contentVarys := make([]string, 0, 3)
65-
pageVarys = append(pageVarys, "Accept-Encoding")
63+
pageVarys := make([]string, 0, 3)
64+
contentVarys := make([]string, 0, 2)
65+
pageVarys = append(pageVarys, "accept-encoding")
6666
if restrictAccess {
67-
pageVarys = append(pageVarys, "Referer", "Origin")
68-
contentVarys = append(contentVarys, "Referer", "Origin")
67+
pageVarys = append(pageVarys, "referer", "origin")
68+
contentVarys = append(contentVarys, "referer", "origin")
6969
}
70-
71-
pageVaryV1 := strings.Join(pageVarys, ", ")
72-
contentVaryV1 := strings.Join(contentVarys, ", ")
73-
pageVary := strings.ToLower(pageVaryV1)
74-
contentVary := strings.ToLower(contentVaryV1)
70+
pageVary := strings.Join(pageVarys, ", ")
71+
contentVary := strings.Join(contentVarys, ", ")
7572

7673
// alias param
7774
ap := &aliasParam{
@@ -93,10 +90,8 @@ func NewVhostHandler(
9390
headersUrls: newPathHeaders(p.HeadersUrls),
9491
headersDirs: newPathHeaders(p.HeadersDirs),
9592

96-
pageVaryV1: pageVaryV1,
97-
pageVary: pageVary,
98-
contentVaryV1: contentVaryV1,
99-
contentVary: contentVary,
93+
pageVary: pageVary,
94+
contentVary: contentVary,
10095
}
10196

10297
muxHandler := newMultiplexHandler(p, ap)

0 commit comments

Comments
 (0)