git.iko.soy/typeable/oauth2-proxy
on master help sign in
Replace standard serve mux with gorilla mux
d2d62bbJoel Speed
go.mod+1−1
@@ -16,7 +16,7 @@ require (
16 github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
16 github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
17 github.com/go-redis/redis/v8 v8.2.3
17 github.com/go-redis/redis/v8 v8.2.3
18 github.com/google/uuid v1.2.0
18 github.com/google/uuid v1.2.0
19− github.com/gorilla/mux v1.8.0 // indirect
19+ github.com/gorilla/mux v1.8.0
20 github.com/justinas/alice v1.2.0
20 github.com/justinas/alice v1.2.0
21 github.com/mbland/hmacauth v0.0.0-20170912233209-44256dfd4bfa
21 github.com/mbland/hmacauth v0.0.0-20170912233209-44256dfd4bfa
22 github.com/mitchellh/mapstructure v1.1.2
22 github.com/mitchellh/mapstructure v1.1.2
pkg/upstream/proxy.go+17−5
@@ -4,7 +4,9 @@ import (
4 "fmt"
4 "fmt"
5 "net/http"
5 "net/http"
6 "net/url"
6 "net/url"
7+ "strings"
7
8
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(),
22 }
24 }
23
25
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
50}
52}
51
53
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))
61}
63}
62
64
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))
67}
69}
68
70
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))
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+ }
73}
85}