Skip to content
Snippets Groups Projects
Unverified Commit e92c9b27 authored by Rohith Jayawardene's avatar Rohith Jayawardene Committed by GitHub
Browse files

Websocket Headers (#311)

- fixing up the headers for websockets
parent 1f5005e6
No related branches found
No related tags found
No related merge requests found
...@@ -30,7 +30,7 @@ func (r *oauthProxy) proxyMiddleware(next http.Handler) http.Handler { ...@@ -30,7 +30,7 @@ func (r *oauthProxy) proxyMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
next.ServeHTTP(w, req) next.ServeHTTP(w, req)
// step: retrieve the request scope // @step: retrieve the request scope
scope := req.Context().Value(contextScopeName) scope := req.Context().Value(contextScopeName)
if scope != nil { if scope != nil {
sc := scope.(*RequestScope) sc := scope.(*RequestScope)
...@@ -39,23 +39,12 @@ func (r *oauthProxy) proxyMiddleware(next http.Handler) http.Handler { ...@@ -39,23 +39,12 @@ func (r *oauthProxy) proxyMiddleware(next http.Handler) http.Handler {
} }
} }
if isUpgradedConnection(req) { // @step: add any custom headers to the request
r.log.Debug("upgrading the connnection", zap.String("client_ip", req.RemoteAddr))
if err := tryUpdateConnection(req, w, r.endpoint); err != nil {
r.log.Error("failed to upgrade connection", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
// add any custom headers to the request
for k, v := range r.config.Headers { for k, v := range r.config.Headers {
req.Header.Set(k, v) req.Header.Set(k, v)
} }
// By default goproxy only provides a forwarding proxy, thus all requests have to be absolute // @note: by default goproxy only provides a forwarding proxy, thus all requests have to be absolute and we must update the host headers
// and we must update the host headers
req.URL.Host = r.endpoint.Host req.URL.Host = r.endpoint.Host
req.URL.Scheme = r.endpoint.Scheme req.URL.Scheme = r.endpoint.Scheme
if v := req.Header.Get("Host"); v != "" { if v := req.Header.Get("Host"); v != "" {
...@@ -65,10 +54,21 @@ func (r *oauthProxy) proxyMiddleware(next http.Handler) http.Handler { ...@@ -65,10 +54,21 @@ func (r *oauthProxy) proxyMiddleware(next http.Handler) http.Handler {
req.Host = r.endpoint.Host req.Host = r.endpoint.Host
} }
// @step: add the proxy forwarding headers
req.Header.Add("X-Forwarded-For", realIP(req)) req.Header.Add("X-Forwarded-For", realIP(req))
req.Header.Set("X-Forwarded-Host", req.URL.Host) req.Header.Set("X-Forwarded-Host", req.URL.Host)
req.Header.Set("X-Forwarded-Proto", req.Header.Get("X-Forwarded-Proto")) req.Header.Set("X-Forwarded-Proto", req.Header.Get("X-Forwarded-Proto"))
if isUpgradedConnection(req) {
r.log.Debug("upgrading the connnection", zap.String("client_ip", req.RemoteAddr))
if err := tryUpdateConnection(req, w, r.endpoint); err != nil {
r.log.Error("failed to upgrade connection", zap.Error(err))
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
r.upstream.ServeHTTP(w, req) r.upstream.ServeHTTP(w, req)
}) })
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment