@@ -4,7 +4,9 @@ import (
44 "fmt"
55 "net/http"
66 "net/url"
7+ "strings"
78
9+ "github.com/gorilla/mux"
810 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
911 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app/pagewriter"
1012 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
@@ -18,7 +20,7 @@ type ProxyErrorHandler func(http.ResponseWriter, *http.Request, error)
1820// multiple upstreams.
1921func NewProxy(upstreams options.Upstreams, sigData *options.SignatureData, writer pagewriter.Writer) (http.Handler, error) {
2022 m := &multiUpstreamProxy{
21− serveMux: http.NewServeMux(),
23+ serveMux: mux.NewRouter(),
2224 }
2325
2426 for _, upstream := range upstreams {
@@ -46,7 +48,7 @@ func NewProxy(upstreams options.Upstreams, sigData *options.SignatureData, write
4648// multiUpstreamProxy will serve requests directed to multiple upstream servers
4749// registered in the serverMux.
4850type multiUpstreamProxy struct {
49− serveMux *http.ServeMux
51+ serveMux *mux.Router
5052}
5153
5254// ServerHTTP handles HTTP requests.
@@ -57,17 +59,27 @@ func (m *multiUpstreamProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request
5759// registerStaticResponseHandler registers a static response handler with at the given path.
5860func (m *multiUpstreamProxy) registerStaticResponseHandler(upstream options.Upstream) {
5961 logger.Printf("mapping path %q => static response %d", upstream.Path, derefStaticCode(upstream.StaticCode))
60− m.serveMux.Handle(upstream.Path, newStaticResponseHandler(upstream.ID, upstream.StaticCode))
62+ m.registerSimpleHandler(upstream.Path, newStaticResponseHandler(upstream.ID, upstream.StaticCode))
6163}
6264
6365// registerFileServer registers a new fileServer based on the configuration given.
6466func (m *multiUpstreamProxy) registerFileServer(upstream options.Upstream, u *url.URL) {
6567 logger.Printf("mapping path %q => file system %q", upstream.Path, u.Path)
66− m.serveMux.Handle(upstream.Path, newFileServer(upstream.ID, upstream.Path, u.Path))
68+ m.registerSimpleHandler(upstream.Path, newFileServer(upstream.ID, upstream.Path, u.Path))
6769}
6870
6971// registerHTTPUpstreamProxy registers a new httpUpstreamProxy based on the configuration given.
7072func (m *multiUpstreamProxy) registerHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *options.SignatureData, writer pagewriter.Writer) {
7173 logger.Printf("mapping path %q => upstream %q", upstream.Path, upstream.URI)
72− m.serveMux.Handle(upstream.Path, newHTTPUpstreamProxy(upstream, u, sigData, writer.ProxyErrorHandler))
74+ m.registerSimpleHandler(upstream.Path, newHTTPUpstreamProxy(upstream, u, sigData, writer.ProxyErrorHandler))
75+}
76+
77+// registerSimpleHandler maintains the behaviour of the go standard serveMux
78+// by ensuring any path with a trailing `/` matches all paths under that prefix.
79+func (m *multiUpstreamProxy) registerSimpleHandler(path string, handler http.Handler) {
80+ if strings.HasSuffix(path, "/") {
81+ m.serveMux.PathPrefix(path).Handler(handler)
82+ } else {
83+ m.serveMux.Path(path).Handler(handler)
84+ }
7385}