Skip to content

Commit 1b61eb0

Browse files
committed
Copy http client
1 parent 02861b4 commit 1b61eb0

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

dial.go

Lines changed: 9 additions & 5 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"
@@ -26,6 +25,7 @@ type DialOptions struct {
2625
// HTTPClient is used for the connection.
2726
// Its Transport must return writable bodies for WebSocket handshakes.
2827
// http.Transport does beginning with Go 1.12.
28+
// Non-zero timeout will be ignored, see https://github.com/nhooyr/websocket/issues/67.
2929
HTTPClient *http.Client
3030

3131
// HTTPHeader specifies the HTTP headers included in the handshake request.
@@ -74,7 +74,15 @@ func dial(ctx context.Context, urls string, opts *DialOptions, rand io.Reader) (
7474
opts = &*opts
7575
if opts.HTTPClient == nil {
7676
opts.HTTPClient = http.DefaultClient
77+
} else if opts.HTTPClient.Timeout > 0 {
78+
// remove timeout
79+
opts.HTTPClient = &http.Client{
80+
Transport: opts.HTTPClient.Transport,
81+
CheckRedirect: opts.HTTPClient.CheckRedirect,
82+
Jar: opts.HTTPClient.Jar,
83+
}
7784
}
85+
7886
if opts.HTTPHeader == nil {
7987
opts.HTTPHeader = http.Header{}
8088
}
@@ -133,10 +141,6 @@ func dial(ctx context.Context, urls string, opts *DialOptions, rand io.Reader) (
133141
}
134142

135143
func handshakeRequest(ctx context.Context, urls string, opts *DialOptions, copts *compressionOptions, secWebSocketKey string) (*http.Response, error) {
136-
if opts.HTTPClient.Timeout > 0 {
137-
return nil, errors.New("use context for cancellation instead of http.Client.Timeout; see https://github.com/nhooyr/websocket/issues/67")
138-
}
139-
140144
u, err := url.Parse(urls)
141145
if err != nil {
142146
return nil, fmt.Errorf("failed to parse url: %w", err)

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",

0 commit comments

Comments
 (0)