Skip to content

Commit 4b60c32

Browse files
committed
feat(serverHandler): use unique Vary header for page and content
As content is also use gzip compress now, also add Vary header for serving content and json.
1 parent 387bf45 commit 4b60c32

File tree

5 files changed

+9
-22
lines changed

5 files changed

+9
-22
lines changed

src/serverHandler/aliasHandler.go

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

43-
pageVary string
44-
contentVary string
43+
vary string
4544
}
4645

4746
type aliasHandler struct {
@@ -102,8 +101,7 @@ type aliasHandler struct {
102101
corsUrls []string
103102
corsDirs []string
104103

105-
pageVary string
106-
contentVary string
104+
vary string
107105

108106
postMiddlewares []middleware.Middleware
109107

@@ -280,8 +278,7 @@ func newAliasHandler(
280278

281279
fileServer: fileServer,
282280

283-
pageVary: ap.pageVary,
284-
contentVary: ap.contentVary,
281+
vary: ap.vary,
285282

286283
postMiddlewares: p.PostMiddlewares,
287284
}

src/serverHandler/content.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ var serveContent = func(h *aliasHandler, w http.ResponseWriter, r *http.Request,
1515

1616
func (h *aliasHandler) content(w http.ResponseWriter, r *http.Request, data *responseData) {
1717
header := w.Header()
18+
header.Set("Vary", h.vary)
1819
header.Set("X-Content-Type-Options", "nosniff")
19-
if len(h.contentVary) > 0 {
20-
header.Set("Vary", h.contentVary)
21-
}
2220
if data.IsDownload {
2321
header.Set("Content-Disposition", "attachment; filename*=UTF-8''"+url.PathEscape(data.ItemName))
2422
}

src/serverHandler/json.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func getJsonData(data *responseData) *jsonResponseData {
7777

7878
func (h *aliasHandler) json(w http.ResponseWriter, r *http.Request, data *responseData) {
7979
header := w.Header()
80+
header.Set("Vary", h.vary)
8081
header.Set("Content-Type", "application/json; charset=utf-8")
8182
if lacksHeader(header, "Cache-Control") {
8283
header.Set("Cache-Control", "public, max-age=0")

src/serverHandler/page.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,12 @@ func updateTranslation(r *http.Request, data *responseData) {
6464

6565
func (h *aliasHandler) page(w http.ResponseWriter, r *http.Request, data *responseData) {
6666
header := w.Header()
67+
header.Set("Vary", h.vary)
6768
header.Set("X-Content-Type-Options", "nosniff")
6869
header.Set("Content-Type", "text/html; charset=utf-8")
6970
if lacksHeader(header, "Cache-Control") {
7071
header.Set("Cache-Control", "public, max-age=0")
7172
}
72-
if len(h.pageVary) > 0 {
73-
header.Set("Vary", h.pageVary)
74-
}
7573

7674
updateTranslation(r, data)
7775

src/serverHandler/vhostHandler.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"mjpclab.dev/ghfs/src/tpl/theme"
88
"mjpclab.dev/ghfs/src/user"
99
"net/http"
10-
"strings"
1110
)
1211

1312
func NewVhostHandler(
@@ -60,15 +59,10 @@ func NewVhostHandler(
6059
restrictAccess := hasRestrictAccess(p.GlobalRestrictAccess, restrictAccessUrls, restrictAccessDirs)
6160

6261
// `Vary` header
63-
pageVarys := make([]string, 0, 3)
64-
contentVarys := make([]string, 0, 2)
65-
pageVarys = append(pageVarys, "accept-encoding")
62+
vary := "accept-encoding"
6663
if restrictAccess {
67-
pageVarys = append(pageVarys, "referer", "origin")
68-
contentVarys = append(contentVarys, "referer", "origin")
64+
vary += ", referer, origin"
6965
}
70-
pageVary := strings.Join(pageVarys, ", ")
71-
contentVary := strings.Join(contentVarys, ", ")
7266

7367
// alias param
7468
ap := &aliasParam{
@@ -90,8 +84,7 @@ func NewVhostHandler(
9084
headersUrls: newPathHeaders(p.HeadersUrls),
9185
headersDirs: newPathHeaders(p.HeadersDirs),
9286

93-
pageVary: pageVary,
94-
contentVary: contentVary,
87+
vary: vary,
9588
}
9689

9790
muxHandler := newMultiplexHandler(p, ap)

0 commit comments

Comments
 (0)