Skip to content

Commit fdc4079

Browse files
authored
1 parent 897a573 commit fdc4079

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

accept.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ type AcceptOptions struct {
6363
CompressionThreshold int
6464
}
6565

66+
func (opts *AcceptOptions) cloneWithDefaults() *AcceptOptions {
67+
var o AcceptOptions
68+
if opts != nil {
69+
o = *opts
70+
}
71+
return &o
72+
}
73+
6674
// Accept accepts a WebSocket handshake from a client and upgrades the
6775
// the connection to a WebSocket.
6876
//
@@ -77,17 +85,13 @@ func Accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (*Conn,
7785
func accept(w http.ResponseWriter, r *http.Request, opts *AcceptOptions) (_ *Conn, err error) {
7886
defer errd.Wrap(&err, "failed to accept WebSocket connection")
7987

80-
if opts == nil {
81-
opts = &AcceptOptions{}
82-
}
83-
opts = &*opts
84-
8588
errCode, err := verifyClientRequest(w, r)
8689
if err != nil {
8790
http.Error(w, err.Error(), errCode)
8891
return nil, err
8992
}
9093

94+
opts = opts.cloneWithDefaults()
9195
if !opts.InsecureSkipVerify {
9296
err = authenticateOrigin(r, opts.OriginPatterns)
9397
if err != nil {

dial.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ type DialOptions struct {
4747
CompressionThreshold int
4848
}
4949

50+
func (opts *DialOptions) cloneWithDefaults() *DialOptions {
51+
var o DialOptions
52+
if opts != nil {
53+
o = *opts
54+
}
55+
if o.HTTPClient == nil {
56+
o.HTTPClient = http.DefaultClient
57+
}
58+
if o.HTTPHeader == nil {
59+
o.HTTPHeader = http.Header{}
60+
}
61+
return &o
62+
}
63+
5064
// Dial performs a WebSocket handshake on url.
5165
//
5266
// The response is the WebSocket handshake response from the server.
@@ -67,17 +81,7 @@ func Dial(ctx context.Context, u string, opts *DialOptions) (*Conn, *http.Respon
6781
func dial(ctx context.Context, urls string, opts *DialOptions, rand io.Reader) (_ *Conn, _ *http.Response, err error) {
6882
defer errd.Wrap(&err, "failed to WebSocket dial")
6983

70-
if opts == nil {
71-
opts = &DialOptions{}
72-
}
73-
74-
opts = &*opts
75-
if opts.HTTPClient == nil {
76-
opts.HTTPClient = http.DefaultClient
77-
}
78-
if opts.HTTPHeader == nil {
79-
opts.HTTPHeader = http.Header{}
80-
}
84+
opts = opts.cloneWithDefaults()
8185

8286
secWebSocketKey, err := secWebSocketKey(rand)
8387
if err != nil {

0 commit comments

Comments
 (0)