Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy HTTP client #261

Merged
merged 3 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -74,7 +73,19 @@ func dial(ctx context.Context, urls string, opts *DialOptions, rand io.Reader) (
opts = &*opts
if opts.HTTPClient == nil {
opts.HTTPClient = http.DefaultClient
} else if opts.HTTPClient.Timeout > 0 {
var cancel context.CancelFunc

ctx, cancel = context.WithTimeout(ctx, opts.HTTPClient.Timeout)
defer cancel()

opts.HTTPClient = &http.Client{
Transport: opts.HTTPClient.Transport,
CheckRedirect: opts.HTTPClient.CheckRedirect,
Jar: opts.HTTPClient.Jar,
}
}

if opts.HTTPHeader == nil {
opts.HTTPHeader = http.Header{}
}
Expand Down Expand Up @@ -133,10 +144,6 @@ func dial(ctx context.Context, urls string, opts *DialOptions, rand io.Reader) (
}

func handshakeRequest(ctx context.Context, urls string, opts *DialOptions, copts *compressionOptions, secWebSocketKey string) (*http.Response, error) {
if opts.HTTPClient.Timeout > 0 {
return nil, errors.New("use context for cancellation instead of http.Client.Timeout; see https://github.com/nhooyr/websocket/issues/67")
}

u, err := url.Parse(urls)
if err != nil {
return nil, fmt.Errorf("failed to parse url: %w", err)
Expand Down
9 changes: 0 additions & 9 deletions dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,6 @@ func TestBadDials(t *testing.T) {
name: "badURLScheme",
url: "ftp://nhooyr.io",
},
{
name: "badHTTPClient",
url: "ws://nhooyr.io",
opts: &DialOptions{
HTTPClient: &http.Client{
Timeout: time.Minute,
},
},
},
{
name: "badTLS",
url: "wss://totallyfake.nhooyr.io",
Expand Down