Skip to content

Commit da07764

Browse files
committed
feat(serverHandler): output response body only if necessary
For methdos like HEAD, OPTIONS, CONNECT and TRACE, prevent output response body.
1 parent 4f53f04 commit da07764

File tree

7 files changed

+38
-10
lines changed

7 files changed

+38
-10
lines changed

src/serverHandler/archiveTar.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func (h *handler) tar(w http.ResponseWriter, r *http.Request, pageData *response
5555
filename := pageData.ItemName + ".tar"
5656
writeArchiveHeader(w, "application/octet-stream", filename)
5757

58+
if !needResponseBody(r.Method) {
59+
return
60+
}
61+
5862
h.visitFs(
5963
h.root+pageData.handlerReqPath,
6064
pageData.rawReqPath,
@@ -88,6 +92,10 @@ func (h *handler) tgz(w http.ResponseWriter, r *http.Request, pageData *response
8892
filename := pageData.ItemName + ".tar.gz"
8993
writeArchiveHeader(w, "application/octet-stream", filename)
9094

95+
if !needResponseBody(r.Method) {
96+
return
97+
}
98+
9199
h.visitFs(
92100
h.root+pageData.handlerReqPath,
93101
pageData.rawReqPath,

src/serverHandler/archiveZip.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func (h *handler) zip(w http.ResponseWriter, r *http.Request, pageData *response
5454
filename := pageData.ItemName + ".zip"
5555
writeArchiveHeader(w, "application/zip", filename)
5656

57+
if !needResponseBody(r.Method) {
58+
return
59+
}
60+
5761
h.visitFs(
5862
h.root+pageData.handlerReqPath,
5963
pageData.rawReqPath,

src/serverHandler/assert.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ func (h *handler) assert(w http.ResponseWriter, r *http.Request, assertPath stri
1313

1414
header := w.Header()
1515
header.Set("Content-Type", content.ContentType)
16-
http.ServeContent(w, r, assertPath, initTime, content.ReadSeeker)
16+
if needResponseBody(r.Method) {
17+
http.ServeContent(w, r, assertPath, initTime, content.ReadSeeker)
18+
}
1719
}

src/serverHandler/content.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package serverHandler
2+
3+
import "net/http"
4+
5+
func (h *handler) content(w http.ResponseWriter, r *http.Request, data *responseData) {
6+
if needResponseBody(r.Method) {
7+
item := data.Item
8+
file := data.File
9+
http.ServeContent(w, r, item.Name(), item.ModTime(), file)
10+
}
11+
}

src/serverHandler/main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7474

7575
if data.CanCors {
7676
h.cors(w, r)
77-
if r.Method == "OPTIONS" {
78-
return
79-
}
8077
}
8178

8279
if data.CanArchive && len(r.URL.RawQuery) > 0 {
@@ -95,11 +92,10 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9592

9693
item := data.Item
9794
if file != nil && item != nil && !item.IsDir() {
98-
http.ServeContent(w, r, item.Name(), item.ModTime(), file)
99-
return
95+
h.content(w, r, data)
96+
} else {
97+
h.page(w, r, data)
10098
}
101-
102-
h.page(w, r, data)
10399
}
104100

105101
func NewHandler(

src/serverHandler/page.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func (h *handler) page(w http.ResponseWriter, r *http.Request, data *responseDat
1515
w.WriteHeader(http.StatusOK)
1616
}
1717

18-
err := h.template.Execute(w, data)
19-
h.errHandler.LogError(err)
18+
if needResponseBody(r.Method) {
19+
err := h.template.Execute(w, data)
20+
h.errHandler.LogError(err)
21+
}
2022
}

src/serverHandler/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package serverHandler
2+
3+
func needResponseBody(method string) bool {
4+
return method != "HEAD" && method != "OPTIONS" && method != "CONNECT" && method != "TRACE"
5+
}

0 commit comments

Comments
 (0)