@@ -4,7 +4,9 @@ import (
9+ "github.com/gorilla/mux"
8 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app/pagewriter"
11 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/app/pagewriter"
10 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
@@ -18,7 +20,7 @@ type ProxyErrorHandler func(http.ResponseWriter, *http.Request, error)
18// multiple upstreams.
20// multiple upstreams.
19func NewProxy(upstreams options.Upstreams, sigData *options.SignatureData, writer pagewriter.Writer) (http.Handler, error) {
21func NewProxy(upstreams options.Upstreams, sigData *options.SignatureData, writer pagewriter.Writer) (http.Handler, error) {
20 m := &multiUpstreamProxy{
22 m := &multiUpstreamProxy{
21− serveMux: http.NewServeMux(),
23+ serveMux: mux.NewRouter(),
24 for _, upstream := range upstreams {
26 for _, upstream := range upstreams {
@@ -46,7 +48,7 @@ func NewProxy(upstreams options.Upstreams, sigData *options.SignatureData, write
46// multiUpstreamProxy will serve requests directed to multiple upstream servers
48// multiUpstreamProxy will serve requests directed to multiple upstream servers
47// registered in the serverMux.
49// registered in the serverMux.
48type multiUpstreamProxy struct {
50type multiUpstreamProxy struct {
49− serveMux *http.ServeMux
51+ serveMux *mux.Router
52// ServerHTTP handles HTTP requests.
54// ServerHTTP handles HTTP requests.
@@ -57,17 +59,27 @@ func (m *multiUpstreamProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request
57// registerStaticResponseHandler registers a static response handler with at the given path.
59// registerStaticResponseHandler registers a static response handler with at the given path.
58func (m *multiUpstreamProxy) registerStaticResponseHandler(upstream options.Upstream) {
60func (m *multiUpstreamProxy) registerStaticResponseHandler(upstream options.Upstream) {
59 logger.Printf("mapping path %q => static response %d", upstream.Path, derefStaticCode(upstream.StaticCode))
61 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))
63// registerFileServer registers a new fileServer based on the configuration given.
65// registerFileServer registers a new fileServer based on the configuration given.
64func (m *multiUpstreamProxy) registerFileServer(upstream options.Upstream, u *url.URL) {
66func (m *multiUpstreamProxy) registerFileServer(upstream options.Upstream, u *url.URL) {
65 logger.Printf("mapping path %q => file system %q", upstream.Path, u.Path)
67 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))
69// registerHTTPUpstreamProxy registers a new httpUpstreamProxy based on the configuration given.
71// registerHTTPUpstreamProxy registers a new httpUpstreamProxy based on the configuration given.
70func (m *multiUpstreamProxy) registerHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *options.SignatureData, writer pagewriter.Writer) {
72func (m *multiUpstreamProxy) registerHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *options.SignatureData, writer pagewriter.Writer) {
71 logger.Printf("mapping path %q => upstream %q", upstream.Path, upstream.URI)
73 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))
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)
83+ m.serveMux.Path(path).Handler(handler)