Skip to content

Commit

Permalink
dial: Redirect wss/ws correctly by modifying the http client
Browse files Browse the repository at this point in the history
Closes #333

Needs a test.
  • Loading branch information
nhooyr committed Oct 14, 2023
1 parent a94999f commit 9fddd2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ func (opts *DialOptions) cloneWithDefaults(ctx context.Context) (context.Context
if o.HTTPHeader == nil {
o.HTTPHeader = http.Header{}
}
newClient := *o.HTTPClient
oldCheckRedirect := o.HTTPClient.CheckRedirect
newClient.CheckRedirect = func(req *http.Request, via []*http.Request) error {
switch req.URL.Scheme {
case "ws":
req.URL.Scheme = "http"
case "wss":
req.URL.Scheme = "https"
}
if oldCheckRedirect != nil {
return oldCheckRedirect(req, via)
}
return nil
}
o.HTTPClient = &newClient

return ctx, cancel, &o
}
Expand Down
6 changes: 6 additions & 0 deletions dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,9 @@ type roundTripperFunc func(*http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}

func TestDialRedirect(t *testing.T) {
t.Parallel()

// TODO:
}

0 comments on commit 9fddd2c

Please sign in to comment.