Skip to content

Commit a751a4a

Browse files
committed
feat(serverHandler/cors): simplify CORS response headers
Always response `*` for all CORS headers.
1 parent b41dc4d commit a751a4a

File tree

2 files changed

+4
-33
lines changed

2 files changed

+4
-33
lines changed

src/serverHandler/cors.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
package serverHandler
22

33
import (
4-
"../util"
54
"net/http"
6-
"strings"
75
)
86

9-
func (h *handler) cors(w http.ResponseWriter, r *http.Request) {
7+
func (h *handler) cors(w http.ResponseWriter) {
108
header := w.Header()
119

1210
header.Set("Access-Control-Allow-Origin", "*")
13-
14-
if r.Method != http.MethodOptions {
15-
return
16-
}
17-
18-
// Access-Control-Allow-Methods
19-
acAllowMethods := []string{
20-
http.MethodGet,
21-
http.MethodHead,
22-
http.MethodPost,
23-
http.MethodPut,
24-
http.MethodDelete,
25-
http.MethodOptions,
26-
http.MethodTrace,
27-
}
28-
acReqMethods := r.Header["Access-Control-Request-Method"]
29-
if len(acReqMethods) > 0 {
30-
acReqMethod := acReqMethods[0]
31-
if !util.Contains(acAllowMethods, acReqMethod) {
32-
acAllowMethods = append(acAllowMethods, acReqMethod)
33-
}
34-
}
35-
header.Set("Access-Control-Allow-Methods", strings.Join(acAllowMethods, ", "))
36-
37-
// Access-Control-Allow-Headers
38-
acAllowHeaders := r.Header["Access-Control-Request-Headers"]
39-
if len(acAllowHeaders) > 0 {
40-
header.Set("Access-Control-Allow-Headers", strings.Join(acAllowHeaders, ", "))
41-
}
11+
header.Set("Access-Control-Allow-Methods", "*")
12+
header.Set("Access-Control-Allow-Headers", "*")
4213
}

src/serverHandler/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
104104
h.header(w)
105105

106106
if data.CanCors {
107-
h.cors(w, r)
107+
h.cors(w)
108108
}
109109

110110
if data.IsMutate {

0 commit comments

Comments
 (0)