Skip to content

Commit a8e475c

Browse files
authored
Merge pull request #277 from nhooyr/merge-master
Merge master into dev
2 parents fdc4079 + e4fee52 commit a8e475c

File tree

7 files changed

+35
-32
lines changed

7 files changed

+35
-32
lines changed

‎accept.go‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ func verifyClientRequest(w http.ResponseWriter, r *http.Request) (errCode int, _
163163
returnhttp.StatusUpgradeRequired, fmt.Errorf("WebSocket protocol violation: handshake request must be at least HTTP/1.1: %q", r.Proto)
164164
}
165165

166-
if!headerContainsToken(r.Header, "Connection", "Upgrade"){
166+
if!headerContainsTokenIgnoreCase(r.Header, "Connection", "Upgrade"){
167167
w.Header().Set("Connection", "Upgrade")
168168
w.Header().Set("Upgrade", "websocket")
169169
returnhttp.StatusUpgradeRequired, fmt.Errorf("WebSocket protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
170170
}
171171

172-
if!headerContainsToken(r.Header, "Upgrade", "websocket"){
172+
if!headerContainsTokenIgnoreCase(r.Header, "Upgrade", "websocket"){
173173
w.Header().Set("Connection", "Upgrade")
174174
w.Header().Set("Upgrade", "websocket")
175175
returnhttp.StatusUpgradeRequired, fmt.Errorf("WebSocket protocol violation: Upgrade header %q does not contain websocket", r.Header.Get("Upgrade"))
@@ -313,11 +313,9 @@ func acceptWebkitDeflate(w http.ResponseWriter, ext websocketExtension, mode Com
313313
returncopts, nil
314314
}
315315

316-
funcheaderContainsToken(h http.Header, key, tokenstring) bool{
317-
token=strings.ToLower(token)
318-
316+
funcheaderContainsTokenIgnoreCase(h http.Header, key, tokenstring) bool{
319317
for_, t:=rangeheaderTokens(h, key){
320-
ift==token{
318+
ifstrings.EqualFold(t, token){
321319
returntrue
322320
}
323321
}
@@ -358,7 +356,6 @@ func headerTokens(h http.Header, key string) []string{
358356
for_, v:=rangeh[key]{
359357
v=strings.TrimSpace(v)
360358
for_, t:=rangestrings.Split(v, ","){
361-
t=strings.ToLower(t)
362359
t=strings.TrimSpace(t)
363360
tokens=append(tokens, t)
364361
}

‎accept_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ func Test_selectSubprotocol(t *testing.T){
226226
serverProtocols: []string{"echo2", "echo3"},
227227
negotiated: "echo3",
228228
},
229+
{
230+
name: "clientCasePresered",
231+
clientProtocols: []string{"Echo1"},
232+
serverProtocols: []string{"echo1"},
233+
negotiated: "Echo1",
234+
},
229235
}
230236

231237
for_, tc:=rangetestCases{

‎ci/test.sh‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ main(){
55
cd"$(dirname "$0")/.."
66

77
go test -timeout=30m -covermode=atomic -coverprofile=ci/out/coverage.prof -coverpkg=./... "$@" ./...
8-
sed -i '/stringer\.go/d' ci/out/coverage.prof
9-
sed -i '/nhooyr.io\/websocket\/internal\/test/d' ci/out/coverage.prof
10-
sed -i '/examples/d' ci/out/coverage.prof
8+
sed -i.bak'/stringer\.go/d' ci/out/coverage.prof
9+
sed -i.bak'/nhooyr.io\/websocket\/internal\/test/d' ci/out/coverage.prof
10+
sed -i.bak'/examples/d' ci/out/coverage.prof
1111

1212
# Last line is the total coverage.
1313
go tool cover -func ci/out/coverage.prof | tail -n1

‎dial.go‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"crypto/rand"
1010
"encoding/base64"
11-
"errors"
1211
"fmt"
1312
"io"
1413
"io/ioutil"
@@ -47,18 +46,27 @@ type DialOptions struct{
4746
CompressionThresholdint
4847
}
4948

50-
func (opts*DialOptions) cloneWithDefaults() *DialOptions{
49+
func (opts*DialOptions) cloneWithDefaults(ctx context.Context) (context.Context, context.CancelFunc, *DialOptions){
50+
varcancel context.CancelFunc
51+
5152
varoDialOptions
5253
ifopts!=nil{
5354
o=*opts
5455
}
5556
ifo.HTTPClient==nil{
5657
o.HTTPClient=http.DefaultClient
58+
} elseifopts.HTTPClient.Timeout>0{
59+
ctx, cancel=context.WithTimeout(ctx, opts.HTTPClient.Timeout)
60+
61+
newClient:=*opts.HTTPClient
62+
newClient.Timeout=0
63+
opts.HTTPClient=&newClient
5764
}
5865
ifo.HTTPHeader==nil{
5966
o.HTTPHeader= http.Header{}
6067
}
61-
return&o
68+
69+
returnctx, cancel, &o
6270
}
6371

6472
// Dial performs a WebSocket handshake on url.
@@ -81,7 +89,11 @@ func Dial(ctx context.Context, u string, opts *DialOptions) (*Conn, *http.Respon
8189
funcdial(ctx context.Context, urlsstring, opts*DialOptions, rand io.Reader) (_*Conn, _*http.Response, errerror){
8290
defererrd.Wrap(&err, "failed to WebSocket dial")
8391

84-
opts=opts.cloneWithDefaults()
92+
varcancel context.CancelFunc
93+
ctx, cancel, opts=opts.cloneWithDefaults(ctx)
94+
ifcancel!=nil{
95+
defercancel()
96+
}
8597

8698
secWebSocketKey, err:=secWebSocketKey(rand)
8799
iferr!=nil{
@@ -137,10 +149,6 @@ func dial(ctx context.Context, urls string, opts *DialOptions, rand io.Reader) (
137149
}
138150

139151
funchandshakeRequest(ctx context.Context, urlsstring, opts*DialOptions, copts*compressionOptions, secWebSocketKeystring) (*http.Response, error){
140-
ifopts.HTTPClient.Timeout>0{
141-
returnnil, errors.New("use context for cancellation instead of http.Client.Timeout; see https://github.com/nhooyr/websocket/issues/67")
142-
}
143-
144152
u, err:=url.Parse(urls)
145153
iferr!=nil{
146154
returnnil, fmt.Errorf("failed to parse url: %w", err)
@@ -193,11 +201,11 @@ func verifyServerResponse(opts *DialOptions, copts *compressionOptions, secWebSo
193201
returnnil, fmt.Errorf("expected handshake response status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode)
194202
}
195203

196-
if!headerContainsToken(resp.Header, "Connection", "Upgrade"){
204+
if!headerContainsTokenIgnoreCase(resp.Header, "Connection", "Upgrade"){
197205
returnnil, fmt.Errorf("WebSocket protocol violation: Connection header %q does not contain Upgrade", resp.Header.Get("Connection"))
198206
}
199207

200-
if!headerContainsToken(resp.Header, "Upgrade", "WebSocket"){
208+
if!headerContainsTokenIgnoreCase(resp.Header, "Upgrade", "WebSocket"){
201209
returnnil, fmt.Errorf("WebSocket protocol violation: Upgrade header %q does not contain websocket", resp.Header.Get("Upgrade"))
202210
}
203211

@@ -242,7 +250,8 @@ func verifyServerExtensions(copts *compressionOptions, h http.Header) (*compress
242250
returnnil, fmt.Errorf("WebSocket protcol violation: unsupported extensions from server: %+v", exts[1:])
243251
}
244252

245-
copts=&*copts
253+
_copts:=*copts
254+
copts=&_copts
246255

247256
for_, p:=rangeext.params{
248257
switchp{

‎dial_test.go‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ func TestBadDials(t *testing.T){
3636
name: "badURLScheme",
3737
url: "ftp://nhooyr.io",
3838
},
39-
{
40-
name: "badHTTPClient",
41-
url: "ws://nhooyr.io",
42-
opts: &DialOptions{
43-
HTTPClient: &http.Client{
44-
Timeout: time.Minute,
45-
},
46-
},
47-
},
4839
{
4940
name: "badTLS",
5041
url: "wss://totallyfake.nhooyr.io",

‎examples/echo/server.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
// It ensures the client speaks the echo subprotocol and
1717
// only allows one message every 100ms with a 10 message burst.
1818
typeechoServerstruct{
19-
2019
// logf controls where logs are sent.
2120
logffunc(fstring, v...interface{})
2221
}

‎internal/test/wstest/pipe.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func Pipe(dialOpts *websocket.DialOptions, acceptOpts *websocket.AcceptOptions)
2424
ifdialOpts==nil{
2525
dialOpts=&websocket.DialOptions{}
2626
}
27-
dialOpts=&*dialOpts
27+
_dialOpts:=*dialOpts
28+
dialOpts=&_dialOpts
2829
dialOpts.HTTPClient=&http.Client{
2930
Transport: tt,
3031
}

0 commit comments

Comments
(0)