MCPcopy
hub / github.com/coder/websocket / cloneWithDefaults

Method cloneWithDefaults

dial.go:68–105  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

66}
67
68func (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//

Callers 1

dialFunction · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected