@@ -56,38 +56,55 @@ func NewProxy(target *url.URL) *WebsocketProxy {
5656
5757// ServeHTTP implements the http.Handler that proxies WebSocket connections.
5858func (w * WebsocketProxy ) ServeHTTP (rw http.ResponseWriter , req * http.Request ) {
59- upgrader := w .Upgrader
60- if w .Upgrader == nil {
61- upgrader = DefaultUpgrader
62- }
63-
64- connPub , err := upgrader .Upgrade (rw , req , nil )
65- if err != nil {
66- log .Printf ("websocketproxy: couldn't upgrade %s\n " , err )
67- return
68- }
69- defer connPub .Close ()
70-
7159 backendURL := w .Backend (req )
7260
7361 dialer := w .Dialer
7462 if w .Dialer == nil {
7563 dialer = DefaultDialer
7664 }
7765
78- connBackend , _ , err := dialer .Dial (backendURL .String (), nil )
66+ // Pass headers from the incoming request to the dialer to forward them to
67+ // the final destinations.
68+ h := http.Header {}
69+ h .Add ("Origin" , req .Header .Get ("Origin" ))
70+ protocols := req .Header ["Sec-WebSocket-Protocol" ]
71+ for _ , prot := range protocols {
72+ h .Add ("Sec-WebSocket-Protocol" , prot )
73+ }
74+ cookies := req .Header ["Cookie" ]
75+ for _ , cookie := range cookies {
76+ h .Add ("Cookie" , cookie )
77+ }
78+
79+ // Connect to the backend url, also pass the headers we prepared above.
80+ connBackend , resp , err := dialer .Dial (backendURL .String (), h )
7981 if err != nil {
8082 log .Printf ("websocketproxy: couldn't dial to remote backend url %s\n " , err )
8183 return
8284 }
8385 defer connBackend .Close ()
8486
87+ upgrader := w .Upgrader
88+ if w .Upgrader == nil {
89+ upgrader = DefaultUpgrader
90+ }
91+
92+ // Now upgrade the existing incoming request to a WebSocket connection.
93+ // Also pass the responseHeader that we gathered from the Dial handshake.
94+ connPub , err := upgrader .Upgrade (rw , req , resp .Header )
95+ if err != nil {
96+ log .Printf ("websocketproxy: couldn't upgrade %s\n " , err )
97+ return
98+ }
99+ defer connPub .Close ()
100+
85101 errc := make (chan error , 2 )
86102 cp := func (dst io.Writer , src io.Reader ) {
87103 _ , err := io .Copy (dst , src )
88104 errc <- err
89105 }
90106
107+ // Start our proxy now, after we setup everything.
91108 go cp (connBackend .UnderlyingConn (), connPub .UnderlyingConn ())
92109 go cp (connPub .UnderlyingConn (), connBackend .UnderlyingConn ())
93110 <- errc
0 commit comments