Skip to content

Commit 953d01b

Browse files
committed
websocketproxy: do not create intermediate values
1 parent 2a7dc67 commit 953d01b

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

websocketproxy.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
7878

7979
// Pass headers from the incoming request to the dialer to forward them to
8080
// the final destinations.
81-
h := http.Header{}
82-
h.Add("Origin", req.Header.Get("Origin"))
83-
protocols := req.Header[http.CanonicalHeaderKey("Sec-WebSocket-Protocol")]
84-
for _, prot := range protocols {
85-
h.Add("Sec-WebSocket-Protocol", prot)
81+
requestHeader := http.Header{}
82+
requestHeader.Add("Origin", req.Header.Get("Origin"))
83+
for _, prot := range req.Header[http.CanonicalHeaderKey("Sec-WebSocket-Protocol")] {
84+
requestHeader.Add("Sec-WebSocket-Protocol", prot)
8685
}
87-
cookies := req.Header[http.CanonicalHeaderKey("Cookie")]
88-
for _, cookie := range cookies {
89-
h.Add("Cookie", cookie)
86+
for _, cookie := range req.Header[http.CanonicalHeaderKey("Cookie")] {
87+
requestHeader.Add("Cookie", cookie)
9088
}
9189

9290
// Pass X-Forwarded-For headers too, code below is a part of
@@ -100,23 +98,24 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
10098
if prior, ok := req.Header["X-Forwarded-For"]; ok {
10199
clientIP = strings.Join(prior, ", ") + ", " + clientIP
102100
}
103-
h.Set("X-Forwarded-For", clientIP)
101+
requestHeader.Set("X-Forwarded-For", clientIP)
104102
}
105103

106104
// Set the originating protocol of the incoming HTTP request. The SSL might
107105
// be terminated on our site and because we doing proxy adding this would
108106
// be helpful for applications on the backend.
109-
h.Set("X-Forwarded-Proto", "http")
107+
requestHeader.Set("X-Forwarded-Proto", "http")
110108
if req.TLS != nil {
111-
h.Set("X-Forwarded-Proto", "https")
109+
requestHeader.Set("X-Forwarded-Proto", "https")
112110
}
113111

114-
// Connect to the backend URL, also pass the headers we prepared above.
112+
// Connect to the backend URL, also pass the headers we get from the requst
113+
// together with the Forwarded headers we prepared above.
115114
// TODO: support multiplexing on the same backend connection instead of
116115
// opening a new TCP connection time for each request. This should be
117116
// optional:
118117
// http://tools.ietf.org/html/draft-ietf-hybi-websocket-multiplexing-01
119-
connBackend, resp, err := dialer.Dial(backendURL.String(), h)
118+
connBackend, resp, err := dialer.Dial(backendURL.String(), requestHeader)
120119
if err != nil {
121120
log.Printf("websocketproxy: couldn't dial to remote backend url %s\n", err)
122121
return

0 commit comments

Comments
 (0)