Skip to content

Commit fbd55c3

Browse files
authored
Merge branch 'master' into feature/context-takeover
2 parents b27407e + 21ab95f commit fbd55c3

File tree

7 files changed

+56
-20
lines changed

7 files changed

+56
-20
lines changed

client.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,13 @@ func hostPortNoPort(u *url.URL) (hostPort, hostNoPort string) {
110110

111111
// DefaultDialer is a dialer with all fields set to the default values.
112112
var DefaultDialer = &Dialer{
113-
Proxy: http.ProxyFromEnvironment,
113+
Proxy: http.ProxyFromEnvironment,
114+
HandshakeTimeout: 45 * time.Second,
114115
}
115116

117+
// nilDialer is dialer to use when receiver is nil.
118+
var nilDialer Dialer = *DefaultDialer
119+
116120
// Dial creates a new client connection. Use requestHeader to specify the
117121
// origin (Origin), subprotocols (Sec-WebSocket-Protocol) and cookies (Cookie).
118122
// Use the response.Header to get the selected subprotocol
@@ -125,9 +129,7 @@ var DefaultDialer = &Dialer{
125129
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
126130

127131
if d == nil {
128-
d = &Dialer{
129-
Proxy: http.ProxyFromEnvironment,
130-
}
132+
d = &nilDialer
131133
}
132134

133135
challengeKey, err := generateChallengeKey()
@@ -195,6 +197,8 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
195197
k == "Sec-Websocket-Extensions" ||
196198
(k == "Sec-Websocket-Protocol" && len(d.Subprotocols) > 0):
197199
return nil, nil, errors.New("websocket: duplicate header not allowed: " + k)
200+
case k == "Sec-Websocket-Protocol":
201+
req.Header["Sec-WebSocket-Protocol"] = vs
198202
default:
199203
req.Header[k] = vs
200204
}

conn.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ func (c *Conn) writeFatal(err error) error {
372372
return err
373373
}
374374

375-
func (c *Conn) write(frameType int, deadline time.Time, bufs ...[]byte) error {
375+
func (c *Conn) write(frameType int, deadline time.Time, buf0, buf1 []byte) error {
376376
<-c.mu
377377
defer func() { c.mu <- true }()
378378

@@ -384,15 +384,14 @@ func (c *Conn) write(frameType int, deadline time.Time, bufs ...[]byte) error {
384384
}
385385

386386
c.conn.SetWriteDeadline(deadline)
387-
for _, buf := range bufs {
388-
if len(buf) > 0 {
389-
_, err := c.conn.Write(buf)
390-
if err != nil {
391-
return c.writeFatal(err)
392-
}
393-
}
387+
if len(buf1) == 0 {
388+
_, err = c.conn.Write(buf0)
389+
} else {
390+
err = c.writeBufs(buf0, buf1)
391+
}
392+
if err != nil {
393+
return c.writeFatal(err)
394394
}
395-
396395
if frameType == CloseMessage {
397396
c.writeFatal(ErrCloseSent)
398397
}

conn_write.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build go1.8
6+
7+
package websocket
8+
9+
import "net"
10+
11+
func (c *Conn) writeBufs(bufs ...[]byte) error {
12+
b := net.Buffers(bufs)
13+
_, err := b.WriteTo(c.conn)
14+
return err
15+
}

conn_write_legacy.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !go1.8
6+
7+
package websocket
8+
9+
func (c *Conn) writeBufs(bufs ...[]byte) error {
10+
for _, buf := range bufs {
11+
if len(buf) > 0 {
12+
if _, err := c.conn.Write(buf); err != nil {
13+
return err
14+
}
15+
}
16+
}
17+
return nil
18+
}

examples/chat/hub.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package main
66

7-
// hub maintains the set of active clients and broadcasts messages to the
7+
// Hub maintains the set of active clients and broadcasts messages to the
88
// clients.
99
type Hub struct {
1010
// Registered clients.

proxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515
)
1616

17-
type netDialerFunc func(netowrk, addr string) (net.Conn, error)
17+
type netDialerFunc func(network, addr string) (net.Conn, error)
1818

1919
func (fn netDialerFunc) Dial(network, addr string) (net.Conn, error) {
2020
return fn(network, addr)

server.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (u *Upgrader) selectSubprotocol(r *http.Request, responseHeader http.Header
107107
//
108108
// The responseHeader is included in the response to the client's upgrade
109109
// request. Use the responseHeader to specify cookies (Set-Cookie) and the
110-
// application negotiated subprotocol (Sec-Websocket-Protocol).
110+
// application negotiated subprotocol (Sec-WebSocket-Protocol).
111111
//
112112
// If the upgrade fails, then Upgrade replies to the client with an HTTP error
113113
// response.
@@ -131,7 +131,7 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
131131
}
132132

133133
if _, ok := responseHeader["Sec-Websocket-Extensions"]; ok {
134-
return u.returnError(w, r, http.StatusInternalServerError, "websocket: application specific 'Sec-Websocket-Extensions' headers are unsupported")
134+
return u.returnError(w, r, http.StatusInternalServerError, "websocket: application specific 'Sec-WebSocket-Extensions' headers are unsupported")
135135
}
136136

137137
checkOrigin := u.CheckOrigin
@@ -144,7 +144,7 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
144144

145145
challengeKey := r.Header.Get("Sec-Websocket-Key")
146146
if challengeKey == "" {
147-
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: `Sec-Websocket-Key' header is missing or blank")
147+
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: `Sec-WebSocket-Key' header is missing or blank")
148148
}
149149

150150
subprotocol := u.selectSubprotocol(r, responseHeader)
@@ -212,7 +212,7 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
212212
p = append(p, computeAcceptKey(challengeKey)...)
213213
p = append(p, "\r\n"...)
214214
if c.subprotocol != "" {
215-
p = append(p, "Sec-Websocket-Protocol: "...)
215+
p = append(p, "Sec-WebSocket-Protocol: "...)
216216
p = append(p, c.subprotocol...)
217217
p = append(p, "\r\n"...)
218218
}
@@ -223,7 +223,7 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
223223
default:
224224
p = append(p, "Sec-Websocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover\r\n"...)
225225
}
226-
}
226+
}
227227
for k, vs := range responseHeader {
228228
if k == "Sec-Websocket-Protocol" {
229229
continue

0 commit comments

Comments
 (0)