9 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
10 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
13// ProxyErrorHandler is a function that will be used to render error pages when
14// HTTP proxies fail to connect to upstream servers.
15type ProxyErrorHandler func(http.ResponseWriter, *http.Request, error)
17// NewProxy creates a new multiUpstreamProxy that can serve requests directed to
19func NewProxy(upstreams options.Upstreams, sigData *options.SignatureData, errorHandler ProxyErrorHandler) (http.Handler, error) {
20 m := &multiUpstreamProxy{
21 serveMux: http.NewServeMux(),
24 for _, upstream := range upstreams {
26 m.registerStaticResponseHandler(upstream)
30 u, err := url.Parse(upstream.URI)
32 return nil, fmt.Errorf("error parsing URI for upstream %q: %w", upstream.ID, err)
36 m.registerFileServer(upstream, u)
37 case httpScheme, httpsScheme:
38 m.registerHTTPUpstreamProxy(upstream, u, sigData, errorHandler)
40 return nil, fmt.Errorf("unknown scheme for upstream %q: %q", upstream.ID, u.Scheme)
46// multiUpstreamProxy will serve requests directed to multiple upstream servers
47// registered in the serverMux.
48type multiUpstreamProxy struct {
49 serveMux *http.ServeMux
52// ServerHTTP handles HTTP requests.
53func (m *multiUpstreamProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
54 m.serveMux.ServeHTTP(rw, req)
57// registerStaticResponseHandler registers a static response handler with at the given path.
58func (m *multiUpstreamProxy) registerStaticResponseHandler(upstream options.Upstream) {
59 m.serveMux.Handle(upstream.Path, newStaticResponseHandler(upstream.ID, upstream.StaticCode))
62// registerFileServer registers a new fileServer based on the configuration given.
63func (m *multiUpstreamProxy) registerFileServer(upstream options.Upstream, u *url.URL) {
64 logger.Printf("mapping path %q => file system %q", upstream.Path, u.Path)
65 m.serveMux.Handle(upstream.Path, newFileServer(upstream.ID, upstream.Path, u.Path))
68// registerHTTPUpstreamProxy registers a new httpUpstreamProxy based on the configuration given.
69func (m *multiUpstreamProxy) registerHTTPUpstreamProxy(upstream options.Upstream, u *url.URL, sigData *options.SignatureData, errorHandler ProxyErrorHandler) {
70 logger.Printf("mapping path %q => upstream %q", upstream.Path, upstream.URI)
71 m.serveMux.Handle(upstream.Path, newHTTPUpstreamProxy(upstream, u, sigData, errorHandler))
74// NewProxyErrorHandler creates a ProxyErrorHandler using the template given.
75func NewProxyErrorHandler(errorTemplate *template.Template, proxyPrefix string) ProxyErrorHandler {
76 return func(rw http.ResponseWriter, req *http.Request, proxyErr error) {
77 logger.Printf("Error proxying to upstream server: %v", proxyErr)
78 rw.WriteHeader(http.StatusBadGateway)
85 Message: "Error proxying to upstream server",
86 ProxyPrefix: proxyPrefix,
88 errorTemplate.Execute(rw, data)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list