Skip to content
Snippets Groups Projects
Unverified Commit 0379c252 authored by Nedyalko Andreev's avatar Nedyalko Andreev
Browse files

Combine the cors and optionsAndHead middlewares

parent 35442ca9
No related branches found
No related tags found
No related merge requests found
...@@ -174,9 +174,8 @@ func (h *HTTPBin) Handler() http.Handler { ...@@ -174,9 +174,8 @@ func (h *HTTPBin) Handler() http.Handler {
var handler http.Handler var handler http.Handler
handler = mux handler = mux
handler = limitRequestSize(h.options.MaxMemory, handler) handler = limitRequestSize(h.options.MaxMemory, handler)
handler = optionsAndHead(handler) handler = metaRequests(handler)
handler = logger(handler) handler = logger(handler)
handler = cors(handler)
return handler return handler
} }
......
...@@ -8,8 +8,16 @@ import ( ...@@ -8,8 +8,16 @@ import (
"time" "time"
) )
func optionsAndHead(h http.Handler) http.Handler { func metaRequests(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" {
origin = "*"
}
respHeader := w.Header()
respHeader.Set("Access-Control-Allow-Origin", origin)
respHeader.Set("Access-Control-Allow-Credentials", "true")
switch r.Method { switch r.Method {
case "OPTIONS": case "OPTIONS":
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, HEAD, PUT, DELETE, PATCH, OPTIONS")
...@@ -31,20 +39,6 @@ func optionsAndHead(h http.Handler) http.Handler { ...@@ -31,20 +39,6 @@ func optionsAndHead(h http.Handler) http.Handler {
}) })
} }
func cors(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" {
origin = "*"
}
respHeader := w.Header()
respHeader.Set("Access-Control-Allow-Origin", origin)
respHeader.Set("Access-Control-Allow-Credentials", "true")
h.ServeHTTP(w, r)
})
}
func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc { func methods(h http.HandlerFunc, methods ...string) http.HandlerFunc {
methodMap := make(map[string]struct{}, len(methods)) methodMap := make(map[string]struct{}, len(methods))
for _, m := range methods { for _, m := range methods {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment