Skip to content

Commit 7238db2

Browse files
committed
feat(serverHandler/json): set optional header if missing
1 parent 757ea6c commit 7238db2

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

src/serverHandler/content.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func (h *aliasHandler) content(w http.ResponseWriter, r *http.Request, data *res
3838
}
3939

4040
header.Set("Accept-Ranges", "bytes")
41-
if len(header.Values("Content-Type")) == 0 {
41+
if lacksHeader(header, "Content-Type") {
4242
ctype, err := util.GetContentType(item.Name(), file)
4343
if err == nil {
4444
header.Set("Content-Type", ctype)

src/serverHandler/json.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ func getJsonData(data *responseData) *jsonResponseData {
7878
func (h *aliasHandler) json(w http.ResponseWriter, r *http.Request, data *responseData) {
7979
header := w.Header()
8080
header.Set("Content-Type", "application/json; charset=utf-8")
81-
header.Set("Cache-Control", "public, max-age=0")
81+
if lacksHeader(header, "Cache-Control") {
82+
header.Set("Cache-Control", "public, max-age=0")
83+
}
8284

8385
if !NeedResponseBody(r.Method) {
8486
w.WriteHeader(data.Status)

src/serverHandler/page.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (h *aliasHandler) page(w http.ResponseWriter, r *http.Request, data *respon
6666
header := w.Header()
6767
header.Set("X-Content-Type-Options", "nosniff")
6868
header.Set("Content-Type", "text/html; charset=utf-8")
69-
if len(header.Values("Cache-Control")) == 0 {
69+
if lacksHeader(header, "Cache-Control") {
7070
header.Set("Cache-Control", "public, max-age=0")
7171
}
7272

src/serverHandler/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func NeedResponseBody(method string) bool {
4545
method != http.MethodTrace
4646
}
4747

48+
func lacksHeader(header http.Header, key string) bool {
49+
return len(header.Values(key)) == 0
50+
}
51+
4852
func getCleanFilePath(requestPath string) (filePath string, ok bool) {
4953
filePath = path.Clean(requestPath)
5054
ok = filePath == path.Base(filePath)

0 commit comments

Comments
 (0)