| 66 | } |
| 67 | |
| 68 | func (opts *DialOptions) cloneWithDefaults(ctx context.Context) (context.Context, context.CancelFunc, *DialOptions) { |
| 69 | var cancel context.CancelFunc |
| 70 | |
| 71 | var o DialOptions |
| 72 | if opts != nil { |
| 73 | o = *opts |
| 74 | } |
| 75 | if o.HTTPClient == nil { |
| 76 | o.HTTPClient = http.DefaultClient |
| 77 | } |
| 78 | if o.HTTPClient.Timeout > 0 { |
| 79 | ctx, cancel = context.WithTimeout(ctx, o.HTTPClient.Timeout) |
| 80 | |
| 81 | newClient := *o.HTTPClient |
| 82 | newClient.Timeout = 0 |
| 83 | o.HTTPClient = &newClient |
| 84 | } |
| 85 | if o.HTTPHeader == nil { |
| 86 | o.HTTPHeader = http.Header{} |
| 87 | } |
| 88 | newClient := *o.HTTPClient |
| 89 | oldCheckRedirect := o.HTTPClient.CheckRedirect |
| 90 | newClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { |
| 91 | switch req.URL.Scheme { |
| 92 | case "ws": |
| 93 | req.URL.Scheme = "http" |
| 94 | case "wss": |
| 95 | req.URL.Scheme = "https" |
| 96 | } |
| 97 | if oldCheckRedirect != nil { |
| 98 | return oldCheckRedirect(req, via) |
| 99 | } |
| 100 | return nil |
| 101 | } |
| 102 | o.HTTPClient = &newClient |
| 103 | |
| 104 | return ctx, cancel, &o |
| 105 | } |
| 106 | |
| 107 | // Dial performs a WebSocket handshake on url. |
| 108 | // |