diff options
Diffstat (limited to 'srv/src/http/apiutil')
-rw-r--r-- | srv/src/http/apiutil/apiutil.go | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/srv/src/http/apiutil/apiutil.go b/srv/src/http/apiutil/apiutil.go index aa62299..fed6fb5 100644 --- a/srv/src/http/apiutil/apiutil.go +++ b/srv/src/http/apiutil/apiutil.go @@ -117,6 +117,9 @@ func RandStr(numBytes int) string { // // If no Handler is defined for a method then a 405 Method Not Allowed error is // returned. +// +// If the method "*" is defined then all methods not defined will be directed to +// that handler, and 405 Method Not Allowed is never returned. func MethodMux(handlers map[string]http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { @@ -128,13 +131,16 @@ func MethodMux(handlers map[string]http.Handler) http.Handler { method = formMethod } - handler, ok := handlers[method] + if handler, ok := handlers[method]; ok { + handler.ServeHTTP(rw, r) + return + } - if !ok { - http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed) + if handler, ok := handlers["*"]; ok { + handler.ServeHTTP(rw, r) return } - handler.ServeHTTP(rw, r) + http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed) }) } |