MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / blipSync

Function blipSync

db/active_replicator.go:274–327  ·  view source on GitHub ↗

blipSync opens a connection to the target, and returns a blip.Sender to send messages over.

(target url.URL, blipContext *blip.Context, insecureSkipVerify bool)

Source from the content-addressed store, hash-verified

272
273// blipSync opens a connection to the target, and returns a blip.Sender to send messages over.
274func 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.

Callers 2

connectFunction · 0.85

Calls 7

base64UserInfoFunction · 0.85
ErrorfMethod · 0.80
StringMethod · 0.65
CloseMethod · 0.65
UserMethod · 0.45
AddMethod · 0.45

Tested by 1