Skip to content

Commit 482a047

Browse files
committed
refactor(serverHandler): use constants defined in http package instead of literals
1 parent 7f4efff commit 482a047

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/serverHandler/cors.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ func (h *handler) cors(w http.ResponseWriter, r *http.Request) {
1212
header.Set("Access-Control-Allow-Origin", "*")
1313
header.Set("Access-Control-Allow-Credentials", "true")
1414

15-
if r.Method != "OPTIONS" {
15+
if r.Method != http.MethodOptions {
1616
return
1717
}
1818

1919
// Access-Control-Allow-Methods
20-
acAllowMethods := []string{"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE"}
20+
acAllowMethods := []string{
21+
http.MethodGet,
22+
http.MethodHead,
23+
http.MethodPost,
24+
http.MethodPut,
25+
http.MethodDelete,
26+
http.MethodOptions,
27+
http.MethodTrace,
28+
}
2129
acReqMethods := r.Header["Access-Control-Request-Method"]
2230
if len(acReqMethods) > 0 {
2331
acReqMethod := acReqMethods[0]

src/serverHandler/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8181
h.cors(w, r)
8282
}
8383

84-
if data.CanUpload && r.Method == "POST" {
84+
if data.CanUpload && r.Method == http.MethodPost {
8585
h.saveUploadFiles(data.handlerReqPath, r)
8686
http.Redirect(w, r, r.RequestURI, http.StatusFound)
8787
return

src/serverHandler/util.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package serverHandler
22

3+
import "net/http"
4+
35
func needResponseBody(method string) bool {
4-
return method != "HEAD" && method != "OPTIONS" && method != "CONNECT" && method != "TRACE"
6+
return method != http.MethodHead &&
7+
method != http.MethodOptions &&
8+
method != http.MethodConnect &&
9+
method != http.MethodTrace
510
}

0 commit comments

Comments
 (0)