blipSync opens a connection to the target, and returns a blip.Sender to send messages over.
(target url.URL, blipContext *blip.Context, insecureSkipVerify bool)
| 272 | |
| 273 | // blipSync opens a connection to the target, and returns a blip.Sender to send messages over. |
| 274 | func blipSync(target url.URL, blipContext *blip.Context, insecureSkipVerify bool) (*blip.Sender, error) { |
| 275 | // GET target database endpoint to see if reachable for exit-early/clearer error message |
| 276 | req, err := http.NewRequest(http.MethodGet, target.String(), nil) |
| 277 | if err != nil { |
| 278 | return nil, err |
| 279 | } |
| 280 | client := base.GetHttpClientForWebSocket(insecureSkipVerify) |
| 281 | resp, err := client.Do(req) |
| 282 | if err != nil { |
| 283 | return nil, err |
| 284 | } |
| 285 | |
| 286 | err = resp.Body.Close() |
| 287 | if err != nil { |
| 288 | return nil, err |
| 289 | } |
| 290 | |
| 291 | if resp.StatusCode != http.StatusOK { |
| 292 | return nil, fmt.Errorf("unexpected status code %d from target database", resp.StatusCode) |
| 293 | } |
| 294 | |
| 295 | // switch to websocket protocol scheme |
| 296 | if target.Scheme == "http" { |
| 297 | target.Scheme = "ws" |
| 298 | } else if target.Scheme == "https" { |
| 299 | target.Scheme = "wss" |
| 300 | } |
| 301 | |
| 302 | // Strip userinfo from the URL, don't need it because of the Basic auth header. |
| 303 | var basicAuthCreds *url.Userinfo |
| 304 | if target.User != nil { |
| 305 | // take a copy |
| 306 | if password, hasPassword := target.User.Password(); hasPassword { |
| 307 | basicAuthCreds = url.UserPassword(target.User.Username(), password) |
| 308 | } else { |
| 309 | basicAuthCreds = url.User(target.User.Username()) |
| 310 | } |
| 311 | target.User = nil |
| 312 | } |
| 313 | |
| 314 | config := blip.DialOptions{ |
| 315 | URL: target.String() + "/_blipsync?" + BLIPSyncClientTypeQueryParam + "=" + string(BLIPClientTypeSGR2), |
| 316 | HTTPClient: client, |
| 317 | HTTPHeader: http.Header{ |
| 318 | base.HTTPHeaderUserAgent: []string{ISGRUserAgent}, |
| 319 | }, |
| 320 | } |
| 321 | |
| 322 | if basicAuthCreds != nil { |
| 323 | config.HTTPHeader.Add("Authorization", "Basic "+base64UserInfo(basicAuthCreds)) |
| 324 | } |
| 325 | |
| 326 | return blipContext.DialConfig(&config) |
| 327 | } |
| 328 | |
| 329 | // base64UserInfo returns the base64 encoded version of the given UserInfo. |
| 330 | // Can't use i.String() here because that returns URL encoded versions of credentials. |