-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathmultipart.go
311 lines (277 loc) · 9.32 KB
/
multipart.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
/*
Copyright 2019-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"
"compress/gzip"
"context"
"crypto/md5"
"encoding/base64"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"strings"
"github.com/couchbase/sync_gateway/db"
"github.com/pkg/errors"
"github.com/couchbase/sync_gateway/base"
)
// AttachmentsMeta shorter than this will be left in the JSON as base64 rather than being a separate
// MIME part.
const kMaxInlineAttachmentSize = 200
// JSON bodies smaller than this won't be GZip-encoded.
const kMinCompressedJSONSize = 300
// ReadJSONFromMIME parses a JSON MIME body, unmarshalling it into "into".
// Closes the input io.ReadCloser once done.
func ReadJSONFromMIME(headers http.Header, input io.ReadCloser, into interface{}) error {
err := ReadJSONFromMIMERawErr(headers, input, into)
if err != nil {
err = base.WrapJSONUnknownFieldErr(err)
if errors.Cause(err) == base.ErrUnknownField {
err = base.HTTPErrorf(http.StatusBadRequest, "JSON Unknown Field: %s", err.Error())
} else {
err = base.HTTPErrorf(http.StatusBadRequest, "Bad JSON: %s", err.Error())
}
}
return err
}
func ReadJSONFromMIMERawErr(headers http.Header, input io.ReadCloser, into interface{}) error {
input, err := processContentEncoding(headers, input, "application/json")
if err != nil {
return err
}
// Decode the body bytes into target structure.
decoder := base.JSONDecoder(input)
decoder.DisallowUnknownFields()
decoder.UseNumber()
err = decoder.Decode(into)
_ = input.Close()
return err
}
// processContentEncoding performs the Content-Type validation and Content-Encoding check.
func processContentEncoding(headers http.Header, input io.ReadCloser, expectedContentTypeMime string) (io.ReadCloser, error) {
contentType := headers.Get("Content-Type")
if contentType != "" && !strings.HasPrefix(contentType, expectedContentTypeMime) {
return input, base.HTTPErrorf(http.StatusUnsupportedMediaType, "Invalid content type %s - expected %s", contentType, expectedContentTypeMime)
}
switch headers.Get("Content-Encoding") {
case "gzip":
var err error
if input, err = gzip.NewReader(input); err != nil {
return input, err
}
case "":
break
default:
return input, base.HTTPErrorf(http.StatusUnsupportedMediaType, "Unsupported Content-Encoding; use gzip")
}
return input, nil
}
type attInfo struct {
name string
contentType string
data []byte
}
func writeJSONPart(writer *multipart.Writer, contentType string, body db.Body, compressed bool) (err error) {
bytes, err := base.JSONMarshalCanonical(body)
if err != nil {
return err
}
if len(bytes) < kMinCompressedJSONSize {
compressed = false
}
partHeaders := textproto.MIMEHeader{}
partHeaders.Set("Content-Type", contentType)
if compressed {
partHeaders.Set("Content-Encoding", "gzip")
}
part, err := writer.CreatePart(partHeaders)
if err != nil {
return err
}
if compressed {
gz := gzip.NewWriter(part)
_, err = gz.Write(bytes)
_ = gz.Close()
} else {
_, err = part.Write(bytes)
}
return
}
// Writes a revision to a MIME multipart writer, encoding large attachments as separate parts.
func WriteMultipartDocument(ctx context.Context, cblReplicationPullStats *base.CBLReplicationPullStats, body db.Body, writer *multipart.Writer, compress bool) {
// First extract the attachments that should follow:
following := []attInfo{}
for name, value := range db.GetBodyAttachments(body) {
meta := value.(map[string]interface{})
if meta["stub"] != true {
var err error
var info attInfo
info.contentType, _ = meta["content_type"].(string)
info.data, err = db.DecodeAttachment(meta["data"])
if info.data == nil {
base.WarnfCtx(ctx, "Couldn't decode attachment %q of doc %q: %v", base.UD(name), base.UD(body[db.BodyId]), err)
meta["stub"] = true
delete(meta, "data")
} else if len(info.data) > kMaxInlineAttachmentSize {
info.name = name
following = append(following, info)
meta["follows"] = true
delete(meta, "data")
}
}
}
// Write the main JSON body:
_ = writeJSONPart(writer, "application/json", body, compress)
// Write the following attachments
for _, info := range following {
partHeaders := textproto.MIMEHeader{}
if info.contentType != "" {
partHeaders.Set("Content-Type", info.contentType)
}
partHeaders.Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", info.name))
part, _ := writer.CreatePart(partHeaders)
if cblReplicationPullStats != nil {
cblReplicationPullStats.AttachmentPullCount.Add(1)
cblReplicationPullStats.AttachmentPullBytes.Add(int64(len(info.data)))
}
_, _ = part.Write(info.data)
}
}
func hasInlineAttachments(body db.Body) bool {
for _, value := range db.GetBodyAttachments(body) {
if meta, ok := value.(map[string]interface{}); ok && meta["data"] != nil {
return true
}
}
return false
}
// Adds a new part to the given multipart writer, containing the given revision.
// The revision will be written as a nested multipart body if it has attachments.
func WriteRevisionAsPart(ctx context.Context, cblReplicationPullStats *base.CBLReplicationPullStats, revBody db.Body, isError bool, compressPart bool, writer *multipart.Writer) error {
partHeaders := textproto.MIMEHeader{}
docID, _ := revBody[db.BodyId].(string)
revID, _ := revBody[db.BodyRev].(string)
if len(docID) > 0 {
partHeaders.Set("X-Doc-ID", docID)
partHeaders.Set("X-Rev-ID", revID)
}
if hasInlineAttachments(revBody) {
// Write as multipart, including attachments:
// OPT: Find a way to do this w/o having to buffer the MIME body in memory!
var buffer bytes.Buffer
docWriter := multipart.NewWriter(&buffer)
contentType := fmt.Sprintf("multipart/related; boundary=%q",
docWriter.Boundary())
partHeaders.Set("Content-Type", contentType)
WriteMultipartDocument(ctx, cblReplicationPullStats, revBody, docWriter, compressPart)
_ = docWriter.Close()
content := bytes.TrimRight(buffer.Bytes(), "\r\n")
part, err := writer.CreatePart(partHeaders)
if err == nil {
_, err = part.Write(content)
}
return err
} else {
// Write as JSON:
contentType := "application/json"
if isError {
contentType += `; error="true"`
}
return writeJSONPart(writer, contentType, revBody, compressPart)
}
}
func ReadMultipartDocument(reader *multipart.Reader) (db.Body, error) {
// First read the main JSON document body:
mainPart, err := reader.NextPart()
if err != nil {
return nil, err
}
var body db.Body
err = ReadJSONFromMIME(http.Header(mainPart.Header), mainPart, &body)
if err != nil {
return nil, err
}
// Collect the attachments with a "follows" property, which will appear as MIME parts:
followingAttachments := map[string]map[string]interface{}{}
for name, value := range db.GetBodyAttachments(body) {
if meta := value.(map[string]interface{}); meta["follows"] == true {
followingAttachments[name] = meta
}
}
// Subroutine to look up a following attachment given its digest. (I used to pre-compute a
// map from digest->name, which was faster, but that broke down if there were multiple
// attachments with the same contents! See #96)
findFollowingAttachment := func(withDigest string) (string, map[string]interface{}) {
for name, meta := range followingAttachments {
if meta["follows"] == true {
if digest, ok := meta["digest"].(string); ok && digest == withDigest {
return name, meta
}
}
}
return "", nil
}
// Read the parts one by one:
for i := 0; i < len(followingAttachments); i++ {
part, err := reader.NextPart()
if err != nil {
if err == io.EOF {
err = base.HTTPErrorf(http.StatusBadRequest,
"Too few MIME parts: expected %d attachments, got %d",
len(followingAttachments), i)
}
return nil, err
}
data, err := io.ReadAll(part)
_ = part.Close()
if err != nil {
return nil, err
}
// Look up the attachment by its digest:
digest := db.Sha1DigestKey(data)
name, meta := findFollowingAttachment(digest)
if meta == nil {
name, meta = findFollowingAttachment(md5DigestKey(data))
if meta == nil {
return nil, base.HTTPErrorf(http.StatusBadRequest,
"MIME part #%d doesn't match any attachment", i+2)
}
}
length, ok := base.ToInt64(meta["encoded_length"])
if !ok {
length, ok = base.ToInt64(meta["length"])
}
if ok {
if length != int64(len(data)) {
return nil, base.HTTPErrorf(http.StatusBadRequest, "Attachment length mismatch for %q: read %d bytes, should be %d", name, len(data), length)
}
}
// Stuff the data into the attachment metadata and remove the "follows" property:
delete(meta, "follows")
meta["data"] = data
meta["digest"] = digest
}
// Make sure there are no unused MIME parts:
if _, err = reader.NextPart(); err != io.EOF {
if err == nil {
err = base.HTTPErrorf(http.StatusBadRequest,
"Too many MIME parts (expected %d)", len(followingAttachments)+1)
}
return nil, err
}
return body, nil
}
// This is only here for backwards compatibility. Otherwise should be avoided.
func md5DigestKey(data []byte) string {
digester := md5.New()
digester.Write(data)
return "md5-" + base64.StdEncoding.EncodeToString(digester.Sum(nil))
}