Received a "getAttachment" request
(rq *blip.Message)
| 1471 | |
| 1472 | // Received a "getAttachment" request |
| 1473 | func (bh *blipHandler) handleGetAttachment(rq *blip.Message) error { |
| 1474 | |
| 1475 | getAttachmentParams := newGetAttachmentParams(rq) |
| 1476 | bh.logEndpointEntry(rq.Profile(), getAttachmentParams.String()) |
| 1477 | |
| 1478 | digest := getAttachmentParams.digest() |
| 1479 | if digest == "" { |
| 1480 | return base.HTTPErrorf(http.StatusBadRequest, "Missing 'digest'") |
| 1481 | } |
| 1482 | |
| 1483 | docID := "" |
| 1484 | attachmentAllowedKey := digest |
| 1485 | if bh.activeCBMobileSubprotocol >= CBMobileReplicationV3 { |
| 1486 | docID = getAttachmentParams.docID() |
| 1487 | if docID == "" { |
| 1488 | return base.HTTPErrorf(http.StatusBadRequest, "Missing 'docID'") |
| 1489 | } |
| 1490 | attachmentAllowedKey = docID + digest |
| 1491 | } |
| 1492 | |
| 1493 | // attachmentName should only be used for logging, it is one possible name of an attachment for a given document if multiple attachments share the same digest. |
| 1494 | allowedAttachment := bh.allowedAttachment(attachmentAllowedKey) |
| 1495 | if allowedAttachment.counter <= 0 { |
| 1496 | return base.HTTPErrorf(http.StatusForbidden, "Attachment's doc not being synced") |
| 1497 | } |
| 1498 | |
| 1499 | if bh.activeCBMobileSubprotocol <= CBMobileReplicationV2 { |
| 1500 | docID = allowedAttachment.docID |
| 1501 | } |
| 1502 | |
| 1503 | attachmentKey := MakeAttachmentKey(allowedAttachment.version, docID, digest) |
| 1504 | attachment, err := bh.collection.GetAttachment(attachmentKey) |
| 1505 | if err != nil { |
| 1506 | return err |
| 1507 | |
| 1508 | } |
| 1509 | base.DebugfCtx(bh.loggingCtx, base.KeySync, "Sending attachment with digest=%q (%.2f KB)", digest, float64(len(attachment))/float64(1024)) |
| 1510 | response := rq.Response() |
| 1511 | response.SetBody(attachment) |
| 1512 | response.SetCompressed(rq.Properties[BlipCompress] == trueProperty) |
| 1513 | bh.replicationStats.HandleGetAttachment.Add(1) |
| 1514 | bh.replicationStats.HandleGetAttachmentBytes.Add(int64(len(attachment))) |
| 1515 | base.Audit(bh.loggingCtx, base.AuditIDAttachmentRead, base.AuditFields{ |
| 1516 | base.AuditFieldDocID: docID, |
| 1517 | base.AuditFieldDocVersion: allowedAttachment.docVersion, |
| 1518 | base.AuditFieldAttachmentID: allowedAttachment.name, |
| 1519 | }) |
| 1520 | |
| 1521 | return nil |
| 1522 | } |
| 1523 | |
| 1524 | var errNoBlipHandler = fmt.Errorf("404 - No handler for BLIP request") |
| 1525 |
nothing calls this directly
no test coverage detected