sendGetAttachment requests the full attachment from the peer.
(sender *blip.Sender, docID string, name string, digest string, meta map[string]interface{})
| 1525 | |
| 1526 | // sendGetAttachment requests the full attachment from the peer. |
| 1527 | func (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 |
no test coverage detected