Skip to content

Commit ca7a538

Browse files
committed
websocket: better backend function
1 parent a280c97 commit ca7a538

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

websocketproxy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ var (
2626
// connection and proxies it to another server.
2727
type WebsocketProxy struct {
2828
// Backend returns the backend URL which the proxy uses to reverse proxy
29-
// the incoming websocket connection.
30-
Backend func() *url.URL
29+
// the incoming websocket connection. Request is the initial incoming and
30+
// unmodified request.
31+
Backend func(*http.Request) *url.URL
3132

3233
// Upgrader specifies the paramaters for upgrading an HTTP connection to a
3334
// WebSocket connection. If nil, DefaultUpgrader is used.
@@ -49,7 +50,7 @@ func ProxyHandler(target *url.URL) http.Handler {
4950
// NewProxy returns a new Websocket reverse proxy that rewrites the
5051
// URL's to the scheme, host and base path provider in target.
5152
func NewProxy(target *url.URL) *WebsocketProxy {
52-
backend := func() *url.URL { return target }
53+
backend := func(r *http.Request) *url.URL { return target }
5354
return &WebsocketProxy{Backend: backend}
5455
}
5556

@@ -67,7 +68,7 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
6768
}
6869
defer connPub.Close()
6970

70-
backendURL := w.Backend()
71+
backendURL := w.Backend(req)
7172

7273
dialer := w.Dialer
7374
if w.Dialer == nil {

0 commit comments

Comments
 (0)