ifnot proxied, then call WriteHeader to ensure CORS

This commit is contained in:
bel
2022-05-26 19:34:12 -06:00
parent 38f19408c2
commit 0eea3e787c
2 changed files with 52 additions and 9 deletions

View File

@@ -269,7 +269,7 @@ func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
w.WriteHeader(http.StatusTooManyRequests)
return
}
w, did := s.doCORS(w, r)
w, did := doCORS(w, r)
if did {
return
}
@@ -299,19 +299,24 @@ func (cb corsResponseWriter) WriteHeader(code int) {
cb.ResponseWriter.WriteHeader(code)
}
func (s *Server) doCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, bool) {
func doCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, bool) {
key := mapKey(r.Host)
if !config.GetCORS(key) {
return w, false
}
w = corsResponseWriter{ResponseWriter: w}
if r.Method != "OPTIONS" {
return w, false
return _doCORS(w, r)
}
func _doCORS(w http.ResponseWriter, r *http.Request) (http.ResponseWriter, bool) {
w2 := corsResponseWriter{ResponseWriter: w}
if r.Method != http.MethodOptions {
return w2, false
}
w.Header().Set("Content-Length", "0")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, TRACE, PATCH, HEAD, DELETE")
return w, true
w2.Header().Set("Content-Length", "0")
w2.Header().Set("Content-Type", "text/plain")
w2.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS, TRACE, PATCH, HEAD, DELETE")
w2.WriteHeader(http.StatusOK)
return w2, true
}
func getProxyAuth(r *http.Request) (string, string) {