Skip to content

Commit 2a7dc67

Browse files
committed
websocketproxy: fix and add test for subprotocols
1 parent 2d67618 commit 2a7dc67

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

websocketproxy.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
8080
// the final destinations.
8181
h := http.Header{}
8282
h.Add("Origin", req.Header.Get("Origin"))
83-
protocols := req.Header["Sec-WebSocket-Protocol"]
83+
protocols := req.Header[http.CanonicalHeaderKey("Sec-WebSocket-Protocol")]
8484
for _, prot := range protocols {
8585
h.Add("Sec-WebSocket-Protocol", prot)
8686
}
87-
cookies := req.Header["Cookie"]
87+
cookies := req.Header[http.CanonicalHeaderKey("Cookie")]
8888
for _, cookie := range cookies {
8989
h.Add("Cookie", cookie)
9090
}
@@ -114,7 +114,8 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
114114
// Connect to the backend URL, also pass the headers we prepared above.
115115
// TODO: support multiplexing on the same backend connection instead of
116116
// opening a new TCP connection time for each request. This should be
117-
// optional.
117+
// optional:
118+
// http://tools.ietf.org/html/draft-ietf-hybi-websocket-multiplexing-01
118119
connBackend, resp, err := dialer.Dial(backendURL.String(), h)
119120
if err != nil {
120121
log.Printf("websocketproxy: couldn't dial to remote backend url %s\n", err)
@@ -129,8 +130,10 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
129130

130131
// Only pass those headers to the upgrader.
131132
upgradeHeader := http.Header{}
132-
upgradeHeader.Add("Sec-WebSocket-Protocol", resp.Header.Get("Sec-WebSocket-Protocol"))
133-
upgradeHeader.Add("Set-Cookie", resp.Header.Get("Set-Cookie"))
133+
upgradeHeader.Set("Sec-WebSocket-Protocol",
134+
resp.Header.Get(http.CanonicalHeaderKey("Sec-WebSocket-Protocol")))
135+
upgradeHeader.Set("Set-Cookie",
136+
resp.Header.Get(http.CanonicalHeaderKey("Set-Cookie")))
134137

135138
// Now upgrade the existing incoming request to a WebSocket connection.
136139
// Also pass the header that we gathered from the Dial handshake.

websocketproxy_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ var (
1717

1818
func TestProxy(t *testing.T) {
1919
// websocket proxy
20+
supportedSubProtocols := []string{"test-protocol"}
2021
upgrader := &websocket.Upgrader{
2122
ReadBufferSize: 4096,
2223
WriteBufferSize: 4096,
2324
CheckOrigin: func(r *http.Request) bool {
2425
return true
2526
},
27+
Subprotocols: supportedSubProtocols,
2628
}
2729

2830
u, _ := url.Parse(backendURL)
@@ -67,13 +69,41 @@ func TestProxy(t *testing.T) {
6769

6870
time.Sleep(time.Millisecond * 100)
6971

72+
// let's us define two subprotocols, only one is supported by the server
73+
clientSubProtocols := []string{"test-protocol", "test-notsupported"}
74+
h := http.Header{}
75+
for _, subprot := range clientSubProtocols {
76+
h.Add("Sec-WebSocket-Protocol", subprot)
77+
}
78+
7079
// frontend server, dial now our proxy, which will reverse proxy our
7180
// message to the backend websocket server.
72-
conn, _, err := websocket.DefaultDialer.Dial(serverURL+"/proxy", nil)
81+
conn, resp, err := websocket.DefaultDialer.Dial(serverURL+"/proxy", h)
7382
if err != nil {
7483
t.Fatal(err)
7584
}
7685

86+
// check if the server really accepted only the first one
87+
in := func(desired string) bool {
88+
for _, prot := range resp.Header[http.CanonicalHeaderKey("Sec-WebSocket-Protocol")] {
89+
if desired == prot {
90+
return true
91+
}
92+
}
93+
94+
return false
95+
}
96+
97+
if !in("test-protocol") {
98+
t.Error("test-protocol should be available")
99+
}
100+
101+
if in("test-notsupported") {
102+
t.Error("test-notsupported should be not recevied from the server.")
103+
}
104+
105+
// now write a message and send it to the backend server (which goes trough
106+
// proxy..)
77107
msg := "hello kite"
78108
err = conn.WriteMessage(websocket.TextMessage, []byte(msg))
79109
if err != nil {

0 commit comments

Comments
 (0)