-
Notifications
You must be signed in to change notification settings - Fork 42
/
json.go
354 lines (318 loc) · 9.26 KB
/
json.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
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package sqlite
import (
"bytes"
"crypto/md5"
"database/sql/driver"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"sort"
"strconv"
"strings"
"github.com/go-kivik/kivik/v4/internal"
)
type revision struct {
rev int
id string
}
func (r revision) String() string {
if r.rev == 0 {
return ""
}
return strconv.Itoa(r.rev) + "-" + r.id
}
func parseRev(s string) (revision, error) {
if s == "" {
return revision{}, &internal.Error{Status: http.StatusBadRequest, Message: "missing _rev"}
}
const revElements = 2
parts := strings.SplitN(s, "-", revElements)
id, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return revision{}, &internal.Error{Status: http.StatusBadRequest, Message: "invalid rev format"}
}
if len(parts) == 1 {
// A rev that contains only a number is technically valid.
return revision{rev: int(id)}, nil
}
return revision{rev: int(id), id: parts[1]}, nil
}
// docData represents the document id, rev, deleted status, etc.
type docData struct {
ID string `json:"_id"`
Revisions revsInfo `json:"_revisions"`
Deleted bool `json:"_deleted"`
Attachments map[string]attachment `json:"_attachments"`
RemovedAttachments []string `json:"-"`
Doc []byte
// MD5sum is the MD5sum of the document data. It, along with a hash of
// attachment metadata, is used to calculate the document revision.
MD5sum md5sum `json:"-"`
}
// RevID returns calculated revision ID, possibly setting the MD5sum if it is
// not already set.
func (d *docData) RevID() string {
if d.MD5sum.IsZero() {
if len(d.Doc) == 0 {
panic("MD5sum not set")
}
h := md5.New()
_, _ = io.Copy(h, bytes.NewReader(d.Doc))
copy(d.MD5sum[:], h.Sum(nil))
}
// The revision ID is a hash of:
// - The MD5sum of the document data
// - filenames and digests of attachments sorted by filename
// - the deleted flag, if true
h := md5.New()
_, _ = h.Write(d.MD5sum[:])
if len(d.Attachments) > 0 {
filenames := make([]string, 0, len(d.Attachments))
for filename := range d.Attachments {
filenames = append(filenames, filename)
}
sort.Strings(filenames)
for _, filename := range filenames {
_, _ = h.Write(d.Attachments[filename].Digest.Bytes())
_, _ = h.Write([]byte(filename))
_, _ = h.Write([]byte{0})
}
}
if d.Deleted {
_, _ = h.Write([]byte{0xff})
}
return hex.EncodeToString(h.Sum(nil))
}
const md5sumLen = 16
type md5sum [md5sumLen]byte
func parseMD5sum(s string) (md5sum, error) {
x, err := hex.DecodeString(s)
if err != nil {
return md5sum{}, err
}
var m md5sum
copy(m[:], x)
return m, nil
}
func parseDigest(s string) (md5sum, error) {
if !strings.HasPrefix(s, "md5-") {
return md5sum{}, fmt.Errorf("invalid digest: %s", s)
}
x, err := base64.StdEncoding.DecodeString(s[4:])
if err != nil {
return md5sum{}, err
}
var m md5sum
copy(m[:], x)
return m, nil
}
func (m md5sum) IsZero() bool {
return m == md5sum{}
}
func (m md5sum) String() string {
return hex.EncodeToString(m[:])
}
func (m md5sum) Value() (driver.Value, error) {
if m.IsZero() {
panic("zero value")
}
return m[:], nil
}
func (m *md5sum) Scan(src interface{}) error {
x, ok := src.([]byte)
if !ok {
return fmt.Errorf("unsupported type: %T", src)
}
if len(x) != md5sumLen {
return fmt.Errorf("invalid length: %d", len(x))
}
copy(m[:], x)
return nil
}
func (m md5sum) Digest() string {
s, _ := m.MarshalText()
return string(s)
}
func (m md5sum) MarshalText() ([]byte, error) {
const prefix = "md5-"
enc := base64.StdEncoding
b := make([]byte, len(prefix)+enc.EncodedLen(md5sumLen))
copy(b, "md5-")
enc.Encode(b[4:], m[:])
return b, nil
}
func (m md5sum) Bytes() []byte {
return m[:]
}
type revsInfo struct {
Start int `json:"start"`
IDs []string `json:"ids"`
}
type attachment struct {
ContentType string `json:"content_type"`
Digest md5sum `json:"digest"`
Length int64 `json:"length"`
RevPos int `json:"revpos"`
Stub bool `json:"stub,omitempty"`
// TODO: Add encoding support to compress certain types of attachments.
// Encoding string `json:"encoding"`
// EncodedLength int64 `json:"encoded_length"`
// Data is the raw JSON representation of the attachment data. It is decoded
// into Content by the [attachment.calculate] method.
Data json.RawMessage `json:"data,omitempty"`
Content []byte `json:"-"`
}
// calculate calculates the length, digest, and content of the attachment.
func (a *attachment) calculate(filename string) error {
if a.Data == nil && len(a.Content) == 0 {
return &internal.Error{Status: http.StatusBadRequest, Err: fmt.Errorf("invalid attachment data for %q", filename)}
}
if len(a.Content) == 0 {
if err := json.Unmarshal(a.Data, &a.Content); err != nil {
return &internal.Error{Status: http.StatusBadRequest, Err: fmt.Errorf("invalid attachment data for %q: %w", filename, err)}
}
}
a.Length = int64(len(a.Content))
h := md5.New()
if _, err := io.Copy(h, bytes.NewReader(a.Content)); err != nil {
return err
}
copy(a.Digest[:], h.Sum(nil))
return nil
}
// revs returns the revision list in oldest first order.
func (r *revsInfo) revs() []revision {
revs := make([]revision, len(r.IDs))
for i, id := range r.IDs {
revs[len(r.IDs)-i-1] = revision{rev: r.Start - i, id: id}
}
return revs
}
// leaf returns the leaf revision of the revsInfo.
func (r *revsInfo) leaf() revision {
return revision{rev: r.Start, id: r.IDs[0]}
}
// prepareDoc prepares the doc for insertion. It returns the new docID, rev, and
// marshaled doc with rev and id removed.
func prepareDoc(docID string, doc interface{}) (*docData, error) {
tmpJSON, err := json.Marshal(doc)
if err != nil {
return nil, err
}
var tmp map[string]interface{}
if err := json.Unmarshal(tmpJSON, &tmp); err != nil {
return nil, err
}
data := &docData{}
if err := json.Unmarshal(tmpJSON, &data); err != nil {
return nil, &internal.Error{Status: http.StatusBadRequest, Err: err}
}
for key := range tmp {
if strings.HasPrefix(key, "_") {
delete(tmp, key)
}
}
if !data.Deleted {
delete(tmp, "_deleted")
}
delete(tmp, "_rev")
delete(tmp, "_revisions")
if docID != "" && data.ID != "" && docID != data.ID {
return nil, &internal.Error{Status: http.StatusBadRequest, Message: "Document ID must match _id in document"}
}
if data.ID == "" {
data.ID = docID
}
h := md5.New()
b, _ := json.Marshal(tmp)
if _, err := io.Copy(h, bytes.NewReader(b)); err != nil {
return nil, err
}
sum := h.Sum(nil)
data.Doc = b
copy(data.MD5sum[:], sum)
return data, nil
}
// extractRev extracts the rev from the document.
func extractRev(doc interface{}) (string, error) {
switch t := doc.(type) {
case map[string]interface{}:
r, _ := t["_rev"].(string)
return r, nil
case map[string]string:
return t["_rev"], nil
default:
tmpJSON, err := json.Marshal(doc)
if err != nil {
return "", &internal.Error{Status: http.StatusBadRequest, Err: err}
}
var revDoc struct {
Rev string `json:"_rev"`
}
if err := json.Unmarshal(tmpJSON, &revDoc); err != nil {
return "", &internal.Error{Status: http.StatusBadRequest, Err: err}
}
return revDoc.Rev, nil
}
}
type fullDoc struct {
ID string `json:"-"`
Rev string `json:"-"`
Doc json.RawMessage `json:"-"`
Conflicts []string `json:"_conflicts,omitempty"`
DeletedConflicts []string `json:"_deleted_conflicts,omitempty"`
RevsInfo []map[string]string `json:"_revs_info,omitempty"`
Revisions *revsInfo `json:"_revisions,omitempty"`
LocalSeq int `json:"_local_seq,omitempty"`
Attachments map[string]attachment `json:"_attachments,omitempty"`
Deleted bool `json:"_deleted,omitempty"`
}
func (d *fullDoc) toRaw() json.RawMessage {
buf := bytes.Buffer{}
_ = buf.WriteByte('{')
if id := d.ID; id != "" {
_, _ = buf.WriteString(`"_id":`)
_, _ = buf.Write(jsonMarshal(id))
_ = buf.WriteByte(',')
}
if rev := d.Rev; rev != "" {
_, _ = buf.WriteString(`"_rev":`)
_, _ = buf.Write(jsonMarshal(rev))
_ = buf.WriteByte(',')
}
const minJSONObjectLen = 2
if len(d.Doc) > minJSONObjectLen {
// The main doc
_, _ = buf.Write(d.Doc[1 : len(d.Doc)-1]) // Omit opening and closing braces
_ = buf.WriteByte(',')
}
if tmp, _ := json.Marshal(d); len(tmp) > minJSONObjectLen {
_, _ = buf.Write(tmp[1 : len(tmp)-1])
_ = buf.WriteByte(',')
}
result := buf.Bytes()
// replace final ',' with '}'
result[len(result)-1] = '}'
return result
}
func (d *fullDoc) toReader() io.ReadCloser {
return io.NopCloser(bytes.NewReader(d.toRaw()))
}
func jsonMarshal(s interface{}) []byte {
j, _ := json.Marshal(s)
return j
}