forked from projectdiscovery/simplehttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorslayer.go
More file actions
28 lines (23 loc) · 694 Bytes
/
corslayer.go
File metadata and controls
28 lines (23 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package httpserver
import (
"net/http"
"strings"
)
func (t *HTTPServer) corslayer(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers := w.Header()
headers.Set("Access-Control-Allow-Origin", "*")
if r.Method != http.MethodOptions {
handler.ServeHTTP(w, r)
return
}
headers.Add("Vary", "Origin")
headers.Add("Vary", "Access-Control-Request-Method")
headers.Add("Vary", "Access-Control-Request-Headers")
reqMethod := r.Header.Get("Access-Control-Request-Method")
if reqMethod != "" {
headers.Set("Access-Control-Allow-Methods", strings.ToUpper(reqMethod))
}
w.WriteHeader(http.StatusOK)
})
}