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

Method sendGetAttachment

db/blip_handler.go:1527–1583  ·  view source on GitHub ↗

sendGetAttachment requests the full attachment from the peer.

(sender *blip.Sender, docID string, name string, digest string, meta map[string]interface{})

Source from the content-addressed store, hash-verified

1525
1526// sendGetAttachment requests the full attachment from the peer.
1527func (bh *blipHandler) sendGetAttachment(sender *blip.Sender, docID string, name string, digest string, meta map[string]interface{}) ([]byte, error) {
1528 base.DebugfCtx(bh.loggingCtx, base.KeySync, " Asking for attachment %q for doc %s (digest %s)", base.UD(name), base.UD(docID), digest)
1529 outrq := blip.NewRequest()
1530 outrq.SetProfile(MessageGetAttachment)
1531 outrq.Properties[GetAttachmentDigest] = digest
1532 if bh.collectionIdx != nil {
1533 outrq.Properties[BlipCollection] = strconv.Itoa(*bh.collectionIdx)
1534 }
1535 if isCompressible(name, meta) {
1536 outrq.Properties[BlipCompress] = trueProperty
1537 }
1538
1539 if bh.activeCBMobileSubprotocol >= CBMobileReplicationV3 {
1540 outrq.Properties[GetAttachmentID] = docID
1541 }
1542
1543 if !bh.sendBLIPMessage(sender, outrq) {
1544 return nil, ErrClosedBLIPSender
1545 }
1546
1547 resp := outrq.Response()
1548
1549 respBody, err := resp.Body()
1550 if err != nil {
1551 return nil, err
1552 }
1553
1554 if resp.Properties[BlipErrorCode] != "" {
1555 return nil, fmt.Errorf("error %s from getAttachment: %s", resp.Properties[BlipErrorCode], respBody)
1556 }
1557 lNum, metaLengthOK := meta["length"]
1558 if !metaLengthOK {
1559 return nil, fmt.Errorf("no attachment length provided in meta")
1560 }
1561
1562 metaLength, ok := base.ToInt64(lNum)
1563 if !ok {
1564 return nil, fmt.Errorf("invalid attachment length %q found in meta", lNum)
1565 }
1566
1567 // Verify that the attachment we received matches the metadata stored in the document
1568 expectedLength := int(metaLength)
1569 actualLength := len(respBody)
1570 if actualLength != expectedLength {
1571 return nil, base.HTTPErrorf(http.StatusBadRequest, "Incorrect data sent for attachment with digest: %s (length mismatch - expected %d got %d)", digest, expectedLength, actualLength)
1572 }
1573
1574 actualDigest := Sha1DigestKey(respBody)
1575 if actualDigest != digest {
1576 return nil, base.HTTPErrorf(http.StatusBadRequest, "Incorrect data sent for attachment with digest: %s (digest mismatch - got %s)", digest, actualDigest)
1577 }
1578
1579 bh.replicationStats.GetAttachment.Add(1)
1580 bh.replicationStats.GetAttachmentBytes.Add(metaLength)
1581
1582 return respBody, nil
1583}
1584

Callers 1

Calls 11

DebugfCtxFunction · 0.92
UDFunction · 0.92
ToInt64Function · 0.92
HTTPErrorfFunction · 0.92
isCompressibleFunction · 0.85
Sha1DigestKeyFunction · 0.85
sendBLIPMessageMethod · 0.80
ErrorfMethod · 0.80
ResponseMethod · 0.45
BodyMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected