-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathdoc_api.go
726 lines (628 loc) · 20.6 KB
/
doc_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// Copyright 2012-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package rest
import (
"bytes"
"context"
"fmt"
"math"
"mime/multipart"
"net/http"
"strconv"
"strings"
"time"
"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/base"
"github.com/couchbase/sync_gateway/db"
)
// HTTP handler for a GET of a document
func (h *handler) handleGetDoc() error {
docid := h.PathVar("docid")
revid := h.getQuery("rev")
openRevs := h.getQuery("open_revs")
showExp := h.getBoolQuery("show_exp")
if replicator2, _ := h.getOptBoolQuery("replicator2", false); replicator2 {
return h.handleGetDocReplicator2(docid, revid)
}
// Check whether the caller wants a revision history, or attachment bodies, or both:
var revsLimit = 0
var revsFrom, attachmentsSince []string
{
var err error
var attsSinceParam, revsFromParam []string
if revsFromParam, err = h.getJSONStringArrayQuery("revs_from"); err != nil {
return err
}
if attsSinceParam, err = h.getJSONStringArrayQuery("atts_since"); err != nil {
return err
}
if h.getBoolQuery("revs") {
revsLimit = int(h.getIntQuery("revs_limit", math.MaxInt32))
if revsFromParam != nil {
revsFrom = revsFromParam
} else {
revsFrom = attsSinceParam // revs_from defaults to same value as atts_since
}
}
if h.getBoolQuery("attachments") {
if attsSinceParam != nil {
attachmentsSince = attsSinceParam
} else {
attachmentsSince = []string{}
}
}
}
if openRevs == "" {
// Single-revision GET:
value, err := h.collection.Get1xRevBodyWithHistory(h.ctx(), docid, revid, revsLimit, revsFrom, attachmentsSince, showExp)
if err != nil {
if err == base.ErrImportCancelledPurged {
base.DebugfCtx(h.ctx(), base.KeyImport, fmt.Sprintf("Import cancelled as document %v is purged", base.UD(docid)))
return nil
}
if h.collection.ForceAPIForbiddenErrors() && base.IsDocNotFoundError(err) {
base.InfofCtx(h.ctx(), base.KeyCRUD, "Doc %q not found: %v", base.UD(docid), err)
return db.ErrForbidden
}
return err
}
if value == nil {
if h.collection.ForceAPIForbiddenErrors() {
base.InfofCtx(h.ctx(), base.KeyCRUD, "Doc %q missing", base.UD(docid))
return db.ErrForbidden
}
return kNotFoundError
}
foundRev := value[db.BodyRev].(string)
h.setEtag(foundRev)
h.db.DbStats.Database().NumDocReadsRest.Add(1)
hasBodies := attachmentsSince != nil && value[db.BodyAttachments] != nil
if h.requestAccepts("multipart/") && (hasBodies || !h.requestAccepts("application/json")) {
canCompress := strings.Contains(h.rq.Header.Get("X-Accept-Part-Encoding"), "gzip")
return h.writeMultipart("related", func(writer *multipart.Writer) error {
WriteMultipartDocument(h.ctx(), h.db.DatabaseContext.DbStats.CBLReplicationPull(), value, writer, canCompress)
return nil
})
} else {
h.writeJSON(value)
}
base.Audit(h.ctx(), base.AuditIDDocumentRead, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: foundRev,
})
} else {
var revids []string
attachmentsSince = []string{}
if openRevs == "all" {
// open_revs=all
doc, err := h.collection.GetDocument(h.ctx(), docid, db.DocUnmarshalSync)
if err != nil {
return err
}
if doc == nil {
return kNotFoundError
}
revids = doc.History.GetLeaves()
} else {
// open_revs=["id1", "id2", ...]
err := base.JSONUnmarshal([]byte(openRevs), &revids)
if err != nil {
return base.HTTPErrorf(http.StatusBadRequest, "bad open_revs")
}
}
if h.requestAccepts("multipart/") {
err := h.writeMultipart("mixed", func(writer *multipart.Writer) error {
for _, revid := range revids {
revBody, err := h.collection.Get1xRevBodyWithHistory(h.ctx(), docid, revid, revsLimit, revsFrom, attachmentsSince, showExp)
if err != nil {
revBody = db.Body{"missing": revid} // TODO: More specific error
}
_ = WriteRevisionAsPart(h.ctx(), h.db.DatabaseContext.DbStats.CBLReplicationPull(), revBody, err != nil, false, writer)
base.Audit(h.ctx(), base.AuditIDDocumentRead, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: revid,
})
h.db.DbStats.Database().NumDocReadsRest.Add(1)
}
return nil
})
return err
} else {
base.DebugfCtx(h.ctx(), base.KeyHTTP, "Fallback to non-multipart for open_revs")
h.setHeader("Content-Type", "application/json")
_, _ = h.response.Write([]byte(`[` + "\n"))
separator := []byte(``)
for _, revid := range revids {
revBody, err := h.collection.Get1xRevBodyWithHistory(h.ctx(), docid, revid, revsLimit, revsFrom, attachmentsSince, showExp)
if err != nil {
revBody = db.Body{"missing": revid} // TODO: More specific error
} else {
revBody = db.Body{"ok": revBody}
}
_, _ = h.response.Write(separator)
separator = []byte(",")
err = h.addJSON(revBody)
if err != nil {
return err
}
base.Audit(h.ctx(), base.AuditIDDocumentRead, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: revid,
})
}
_, _ = h.response.Write([]byte(`]`))
h.db.DbStats.Database().NumDocReadsRest.Add(1)
}
}
return nil
}
func (h *handler) handleGetDocReplicator2(docid, revid string) error {
if !base.IsEnterpriseEdition() {
return base.HTTPErrorf(http.StatusNotImplemented, "replicator2 endpoints are only supported in EE")
}
rev, err := h.collection.GetRev(h.ctx(), docid, revid, true, nil)
if err != nil {
return err
}
// Stamp _attachments into message to match BLIP sendRevision behaviour
bodyBytes := rev.BodyBytes
if len(rev.Attachments) > 0 {
bodyBytes, err = base.InjectJSONProperties(bodyBytes, base.KVPair{Key: db.BodyAttachments, Val: rev.Attachments})
if err != nil {
return err
}
}
h.setHeader("Content-Type", "application/json")
_, _ = h.response.Write(bodyBytes)
h.db.DbStats.Database().NumDocReadsRest.Add(1)
base.Audit(h.ctx(), base.AuditIDDocumentRead, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: rev.RevID,
})
return nil
}
// HTTP handler for a GET of a specific doc attachment
func (h *handler) handleGetAttachment() error {
docid := h.PathVar("docid")
attachmentName := h.PathVar("attach")
revid := h.getQuery("rev")
rev, err := h.collection.GetRev(h.ctx(), docid, revid, false, nil)
if err != nil {
return err
}
if rev.BodyBytes == nil {
return kNotFoundError
}
meta, ok := rev.Attachments[attachmentName].(map[string]interface{})
if !ok {
return base.HTTPErrorf(http.StatusNotFound, "missing attachment %s", attachmentName)
}
digest := meta["digest"].(string)
version, ok := db.GetAttachmentVersion(meta)
if !ok {
return db.ErrAttachmentVersion
}
attachmentKey := db.MakeAttachmentKey(version, docid, digest)
data, err := h.collection.GetAttachment(attachmentKey)
if err != nil {
return err
}
metaOption := h.getBoolQuery("meta")
if metaOption {
meta["key"] = attachmentKey
h.writeJSONStatus(http.StatusOK, meta)
return nil
}
status, start, end := h.handleRange(uint64(len(data)))
if status > 299 {
return base.HTTPErrorf(status, "")
} else if status == http.StatusPartialContent {
data = data[start:end]
}
h.setHeader("Content-Length", strconv.FormatUint(uint64(len(data)), 10))
// #720
setContentDisposition := h.privs == adminPrivs
h.setEtag(digest)
// Request will be returned with the same content type as is set on the attachment. The caveat to this is if the
// attachment has a content type which is vulnerable to a phishing attack. If this is the case we will return with
// the Content Disposition header so that browsers will download the attachment rather than attempt to render it
// unless overridden by config option. CBG-1004
contentType, contentTypeSet := meta["content_type"].(string)
if contentTypeSet {
h.setHeader("Content-Type", contentType)
}
if !h.db.ServeInsecureAttachmentTypes {
if contentTypeSet {
// This split is required as the content type field can have other elements after a ; such as charset,
// however, we only care about checking the first part. In the event that there is no ';' strings.Split just
// takes the full contentType string
contentTypeFirst := strings.Split(contentType, ";")[0]
switch contentTypeFirst {
case
"",
"text/html",
"application/xhtml+xml",
"image/svg+xml":
setContentDisposition = true
}
} else {
setContentDisposition = true
}
}
if encoding, ok := meta["encoding"].(string); ok {
if result, _ := h.getOptBoolQuery("content_encoding", true); result {
h.setHeader("Content-Encoding", encoding)
} else {
// Couchbase Lite wants to download the encoded form directly and store it that way,
// but some HTTP client libraries like NSURLConnection will automatically decompress
// the HTTP response if it has a Content-Encoding header. As a workaround, allow the
// client to add ?content_encoding=false to the request URL to disable setting this
// header.
h.setHeader("X-Content-Encoding", encoding)
h.setHeader("Content-Type", "application/gzip")
}
}
if setContentDisposition {
h.setHeader("Content-Disposition", "attachment")
}
h.db.DbStats.CBLReplicationPull().AttachmentPullCount.Add(1)
h.db.DbStats.CBLReplicationPull().AttachmentPullBytes.Add(int64(len(data)))
h.response.WriteHeader(status)
_, _ = h.response.Write(data)
base.Audit(h.ctx(), base.AuditIDAttachmentRead, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: revid,
base.AuditFieldAttachmentID: attachmentName,
})
return nil
}
// HTTP handler for a PUT of an attachment
func (h *handler) handlePutAttachment() error {
if h.isReadOnlyGuest() {
return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
}
docid := h.PathVar("docid")
attachmentName := h.PathVar("attach")
attachmentContentType := h.rq.Header.Get("Content-Type")
if attachmentContentType == "" {
attachmentContentType = "application/octet-stream"
}
revid := h.getQuery("rev")
if revid == "" {
var err error
revid, err = h.getEtag("If-Match")
if err != nil {
return err
}
}
attachmentData, err := h.readBody()
if err != nil {
return err
}
body, err := h.collection.Get1xRevBody(h.ctx(), docid, revid, false, nil)
if err != nil {
if base.IsDocNotFoundError(err) {
// couchdb creates empty body on attachment PUT
// for non-existent doc id
body = db.Body{db.BodyRev: revid}
} else if err != nil {
return err
}
} else if body != nil {
if revid == "" {
// If a revid is not specified and an active revision was found,
// return a conflict now, rather than letting db.Put do it further down...
return base.HTTPErrorf(http.StatusConflict, "Cannot modify attachments without a specific rev ID")
}
}
// find attachment (if it existed)
attachments := db.GetBodyAttachments(body)
if attachments == nil {
attachments = make(map[string]interface{})
}
// create new attachment
attachment := make(map[string]interface{})
attachment["data"] = attachmentData
attachment["content_type"] = attachmentContentType
// attach it
attachments[attachmentName] = attachment
body[db.BodyAttachments] = attachments
newRev, _, err := h.collection.Put(h.ctx(), docid, body)
if err != nil {
return err
}
h.setEtag(newRev)
h.writeRawJSONStatus(http.StatusCreated, []byte(`{"id":`+base.ConvertToJSONString(docid)+`,"ok":true,"rev":"`+newRev+`"}`))
return nil
}
func (h *handler) handleDeleteAttachment() error {
if h.isReadOnlyGuest() {
return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
}
docid := h.PathVar("docid")
attachmentName := h.PathVar("attach")
revid := h.getQuery("rev")
if revid == "" {
var err error
revid, err = h.getEtag("If-Match")
if err != nil {
return err
}
}
body, err := h.collection.Get1xRevBody(h.ctx(), docid, revid, false, nil)
if err != nil {
if base.IsDocNotFoundError(err) {
// Check here if error is relating to incorrect revid, if so return 409 code else return 404 code
if strings.Contains(err.Error(), "404 missing") {
return base.HTTPErrorf(http.StatusConflict, "Incorrect revision ID specified")
}
// Need to return an error if a document is not found
return base.HTTPErrorf(http.StatusNotFound, "Document specified is not found")
} else if err != nil {
return err
}
} else if body != nil {
if revid == "" {
// If a revid is not specified and an active revision was found,
// return a conflict now, rather than letting db.Put do it further down...
return base.HTTPErrorf(http.StatusConflict, "Cannot modify attachments without a specific rev ID")
}
}
// get document attachments and check if attachment exists
attachments := db.GetBodyAttachments(body)
if _, ok := attachments[attachmentName]; !ok {
return base.HTTPErrorf(http.StatusNotFound, "Attachment %s is not found", attachmentName)
}
// delete specified attachment from the map
delete(attachments, attachmentName)
body[db.BodyAttachments] = attachments
newRev, _, err := h.collection.Put(h.ctx(), docid, body)
if err != nil {
return err
}
h.setEtag(newRev)
h.writeRawJSONStatus(http.StatusOK, []byte(`{"id":`+base.ConvertToJSONString(docid)+`,"ok":true,"rev":"`+newRev+`"}`))
return nil
}
// HTTP handler for a PUT of a document
func (h *handler) handlePutDoc() error {
if h.isReadOnlyGuest() {
return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
}
startTime := time.Now()
defer func() {
h.db.DbStats.CBLReplicationPush().WriteProcessingTime.Add(time.Since(startTime).Nanoseconds())
}()
docid := h.PathVar("docid")
roundTrip := h.getBoolQuery("roundtrip")
if replicator2, _ := h.getOptBoolQuery("replicator2", false); replicator2 {
return h.handlePutDocReplicator2(docid, roundTrip)
}
body, err := h.readDocument()
if err != nil {
return err
}
if body == nil {
return base.ErrEmptyDocument
}
if bodyDocID, ok := body[db.BodyId].(string); ok && bodyDocID != docid {
return base.HTTPErrorf(http.StatusBadRequest, "The document ID provided in the body does not match the document ID in the path")
}
var newRev string
var doc *db.Document
if h.getQuery("new_edits") != "false" {
// Regular PUT:
bodyRev := body[db.BodyRev]
if oldRev := h.getQuery("rev"); oldRev != "" {
body[db.BodyRev] = oldRev
} else if ifMatch, _ := h.getEtag("If-Match"); ifMatch != "" {
body[db.BodyRev] = ifMatch
}
if bodyRev != nil && bodyRev != body[db.BodyRev] {
return base.HTTPErrorf(http.StatusBadRequest, "Revision IDs provided do not match")
}
newRev, doc, err = h.collection.Put(h.ctx(), docid, body)
if err != nil {
return err
}
h.setEtag(newRev)
} else {
// Replicator-style PUT with new_edits=false:
revisions := db.ParseRevisions(h.ctx(), body)
if revisions == nil {
return base.HTTPErrorf(http.StatusBadRequest, "Bad _revisions")
}
doc, newRev, err = h.collection.PutExistingRevWithBody(h.ctx(), docid, body, revisions, false)
if err != nil {
return err
}
}
if doc != nil && roundTrip {
if err := h.collection.WaitForSequenceNotSkipped(h.ctx(), doc.Sequence); err != nil {
return err
}
}
h.writeRawJSONStatus(http.StatusCreated, []byte(`{"id":`+base.ConvertToJSONString(docid)+`,"ok":true,"rev":"`+newRev+`"}`))
return nil
}
func (h *handler) handlePutDocReplicator2(docid string, roundTrip bool) (err error) {
if !base.IsEnterpriseEdition() {
return base.HTTPErrorf(http.StatusNotImplemented, "replicator2 endpoints are only supported in EE")
}
if h.isReadOnlyGuest() {
return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
}
bodyBytes, err := h.readBody()
if err != nil {
return err
}
if bodyBytes == nil || len(bodyBytes) == 0 {
return base.ErrEmptyDocument
}
newDoc := &db.Document{
ID: docid,
}
newDoc.UpdateBodyBytes(bodyBytes)
var parentRev string
if oldRev := h.getQuery("rev"); oldRev != "" {
parentRev = oldRev
} else if ifMatch, _ := h.getEtag("If-Match"); ifMatch != "" {
parentRev = ifMatch
}
generation, _ := db.ParseRevID(h.ctx(), parentRev)
generation++
deleted, _ := h.getOptBoolQuery("deleted", false)
newDoc.Deleted = deleted
newDoc.RevID = db.CreateRevIDWithBytes(generation, parentRev, bodyBytes)
history := []string{newDoc.RevID}
if parentRev != "" {
history = append(history, parentRev)
}
// Handle and pull out expiry
if bytes.Contains(bodyBytes, []byte(db.BodyExpiry)) {
body := newDoc.Body(h.ctx())
expiry, err := body.ExtractExpiry()
if err != nil {
return base.HTTPErrorf(http.StatusBadRequest, "Invalid expiry: %v", err)
}
newDoc.DocExpiry = expiry
newDoc.UpdateBody(body)
}
// Pull out attachments
if bytes.Contains(bodyBytes, []byte(db.BodyAttachments)) {
body := newDoc.Body(h.ctx())
newDoc.DocAttachments = db.GetBodyAttachments(body)
delete(body, db.BodyAttachments)
newDoc.UpdateBody(body)
}
doc, rev, err := h.collection.PutExistingRev(h.ctx(), newDoc, history, true, false, nil)
if err != nil {
return err
}
if doc != nil && roundTrip {
if err := h.collection.WaitForSequenceNotSkipped(h.ctx(), doc.Sequence); err != nil {
return err
}
}
h.writeRawJSONStatus(http.StatusCreated, []byte(`{"id":`+base.ConvertToJSONString(docid)+`,"ok":true,"rev":"`+rev+`"}`))
return nil
}
// HTTP handler for a POST to a database (creating a document)
func (h *handler) handlePostDoc() error {
if h.isReadOnlyGuest() {
return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
}
roundTrip := h.getBoolQuery("roundtrip")
body, err := h.readDocument()
if err != nil {
return err
}
docid, newRev, doc, err := h.collection.Post(h.ctx(), body)
if err != nil {
return err
}
if doc != nil && roundTrip {
err := h.collection.WaitForSequenceNotSkipped(h.ctx(), doc.Sequence)
if err != nil {
return err
}
}
h.setHeader("Location", docid)
h.setEtag(newRev)
h.writeRawJSON([]byte(`{"id":"` + docid + `","ok":true,"rev":"` + newRev + `"}`))
return nil
}
// HTTP handler for a DELETE of a document
func (h *handler) handleDeleteDoc() error {
docid := h.PathVar("docid")
revid := h.getQuery("rev")
if revid == "" {
var err error
revid, err = h.getEtag("If-Match")
if err != nil {
return err
}
}
newRev, err := h.collection.DeleteDoc(h.ctx(), docid, revid)
if err == nil {
h.writeRawJSONStatus(http.StatusOK, []byte(`{"id":`+base.ConvertToJSONString(docid)+`,"ok":true,"rev":"`+newRev+`"}`))
}
return err
}
// ////// LOCAL DOCS:
// HTTP handler for a GET of a _local document
func (h *handler) handleGetLocalDoc() error {
docid := h.PathVar("docid")
localDocID := db.LocalDocPrefix + docid
value, err := h.collection.GetSpecial(db.DocTypeLocal, docid)
if err != nil {
return err
}
if value == nil {
return kNotFoundError
}
value[db.BodyId] = localDocID
docVersion := value[db.BodyRev]
base.Audit(h.ctx(), base.AuditIDDocumentRead, base.AuditFields{
base.AuditFieldDocID: localDocID,
base.AuditFieldDocVersion: docVersion,
})
h.writeJSON(value)
return nil
}
// HTTP handler for a PUT of a _local document
func (h *handler) handlePutLocalDoc() error {
docid := h.PathVar("docid")
localDocID := db.LocalDocPrefix + docid
body, err := h.readJSON()
if err != nil {
return err
}
revid, isNewDoc, err := h.collection.PutSpecial(db.DocTypeLocal, docid, body)
if err != nil {
return err
}
auditEventForDocumentUpsert(h.ctx(), localDocID, revid, isNewDoc)
h.writeRawJSONStatus(http.StatusCreated, []byte(`{"id":`+base.ConvertToJSONString(localDocID)+`,"ok":true,"rev":"`+revid+`"}`))
return nil
}
func auditEventForDocumentUpsert(ctx context.Context, docid string, revid string, isNewDoc bool) {
auditEvent := base.AuditIDDocumentUpdate
if isNewDoc {
auditEvent = base.AuditIDDocumentCreate
}
base.Audit(ctx, auditEvent, base.AuditFields{
base.AuditFieldDocID: docid,
base.AuditFieldDocVersion: revid,
})
}
// HTTP handler for a DELETE of a _local document
func (h *handler) handleDelLocalDoc() error {
docid := h.PathVar("docid")
rev := h.getQuery("rev")
localDocID := db.LocalDocPrefix + docid
if err := h.collection.DeleteSpecial(db.DocTypeLocal, docid, rev); err != nil {
return err
}
base.Audit(h.ctx(), base.AuditIDDocumentDelete, base.AuditFields{
base.AuditFieldDocID: localDocID,
base.AuditFieldDocVersion: rev,
})
return nil
}
// isGuest returns true if the current user is a guest
func (h *handler) isGuest() bool {
return h.user != nil && h.user.Name() == ""
}
// helper for read only check
func (h *handler) isReadOnlyGuest() bool {
if h.db.IsGuestReadOnly() && h.db.User() != nil && h.db.User().Name() == "" {
return true
}
return false
}