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
//! The `attachment` module provides types for working with CouchDB document
//! attachments.

use {Error, base64, serde, std};
use mime::Mime;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;

/// `Attachment` is a state-aware representation of a CouchDB document
/// attachment.
///
/// # Summary
///
/// * `Attachment` maintains state about whether it already exists on the server
///   (i.e., _originates from the server_) or not (i.e., _originates from the
///   client_).
///
/// * A CouchDB attachment may be stubbed, meaning it has no content but instead
///   is a placeholder for attachment content that already exists on the server.
///
/// * An `Attachment` instance deserialized from JSON is server-originating.
///
/// * An `Attachment` instance constructed from content (e.g., via the
///   `Attachment::new` method) is client-originating.
///
/// * When serialized to JSON, a server-originating `Attachment` instance emits
///   a stub object—regardless whether the `Attachment` instance is a stub.
///
/// * When serialized to JSON, a client-originating `Attachment` instance emits
///   a non-stub object that uses base64-encoding to encapsulate its content.
///
/// * `Attachment` supports conversion into a stub, which is useful when either:
///
///     * Updating a document but not making changes to its existing
///       attachments, or,
///     * Uploading attachments via multipart-encoding.
///
/// # Remarks
///
/// CouchDB document attachments are versatile but tricky. Generally speaking,
/// there are several things the application must get right:
///
/// * When updating a document on the server, the client must send existing
///   attachments—either stubbed or containing full content—otherwise the server
///   will delete missing attachments as part of the document update.
///
/// * When enclosing attachment content directly in JSON, the content must be
///   base64-encoded.
///
/// * To prevent sending redundant data to the server, the application
///   serializes unmodified attachments as stubs (via `"stub": true` within the
///   attachment object).
///
/// * When using multipart-encoding in lieu of base64-encoding, the application
///   serializes attachments into yet another form (via `"follows": true` within
///   the attachment object).
///
/// # TODO
///
/// * Add a means for applications to construct server-originating attachments
///   from multipart data.
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Attachment {
    content_type: Mime,
    inner: Inner,
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum Inner {
    ServerOrigin {
        content: Content,
        digest: Digest,
        encoding: Option<Encoding>,
        revpos: u64,
    },
    ClientOrigin { content: Vec<u8> },
    Follows { content_length: u64 },
}

#[derive(Clone, Debug, Eq, PartialEq)]
enum Content {
    WithBytes(Vec<u8>),
    WithLength(u64),
}

/// `Digest` is a hashed sum of an attachment's content.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Digest {
    #[doc(hidden)]
    Md5 { value: Vec<u8> },
    #[doc(hidden)]
    Other { name: String, value: Vec<u8> },
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
enum EncodingCodec {
    Gzip,
    Other(String),
}

/// `Encoding` contains information about the compression the CouchDB server
/// uses to store an attachment's content.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Encoding {
    length: u64,
    codec: EncodingCodec,
}

impl Attachment {
    /// Constructs a new attachment.
    ///
    /// The newly constructed `Attachment` is internally marked as having
    /// originated from the client and therefore, when serialized as JSON, will
    /// include all content as a base64-encoded string (as opposed to being
    /// stubbed out). This may incur significant overhead when sent to the
    /// CouchDB server within an enclosed document because base64-encoding uses
    /// four encoded bytes to represent every three encoded bytes.
    ///
    /// One way to reduce base64  overhead is to stub the attachment and instead
    /// use multipart-encoding when uploading the document. See the [CouchDB
    /// documentation](http://docs.couchdb.org/en/2.0.0/api/document/common.html#attachments)
    /// for details.
    ///
    pub fn new(content_type: Mime, content: Vec<u8>) -> Self {
        Attachment {
            content_type: content_type,
            inner: Inner::ClientOrigin { content: content },
        }
    }

    /// Returns whether the attachment originates from the server.
    pub fn is_server_origin(&self) -> bool {
        match self.inner {
            Inner::ServerOrigin { .. } => true,
            Inner::ClientOrigin { .. } => false,
            Inner::Follows { .. } => false,
        }
    }

    /// Returns whether the attachment originates from the client.
    pub fn is_client_origin(&self) -> bool {
        match self.inner {
            Inner::ServerOrigin { .. } => false,
            Inner::ClientOrigin { .. } => true,
            Inner::Follows { .. } => false,
        }
    }

    /// Borrows the attachment's content MIME type.
    pub fn content_type(&self) -> &Mime {
        &self.content_type
    }

    /// Borrows the attachment's content, if available.
    ///
    /// Content is available if and only if:
    ///
    /// * The attachment originates from the client, or,
    /// * The attachment originates from the server and is not a stub.
    ///
    pub fn content(&self) -> Option<&[u8]> {
        match self.inner {
            Inner::ServerOrigin { content: Content::WithBytes(ref bytes), .. } => Some(bytes),
            Inner::ServerOrigin { content: Content::WithLength(_), .. } => None,
            Inner::ClientOrigin { ref content } => Some(content),
            Inner::Follows { .. } => None,
        }
    }

    /// Returns the size of the attachment's content, in bytes.
    pub fn content_length(&self) -> u64 {
        match self.inner {
            Inner::ServerOrigin { content: Content::WithBytes(ref bytes), .. } => bytes.len() as u64,
            Inner::ServerOrigin { content: Content::WithLength(length), .. } => length,
            Inner::ClientOrigin { ref content } => content.len() as u64,
            Inner::Follows { content_length } => content_length,
        }
    }

    /// Constructs a stubbed copy of the attachment.
    ///
    /// A stubbed attachment contains no content, instead marking itself as a
    /// stub and relying on the CouchDB server to already have the content if
    /// the attachment is sent to the server within its enclosing document.
    ///
    /// Hence, only an attachment that originates from the server can be
    /// stubbed. Otherwise, content would be lost, which this method prevents by
    /// instead returning `None` if the attachment originates from the client.
    ///
    /// **Note:** The stub retains all other information about the attachment,
    ///  such as content type and digest.
    ///
    pub fn to_stub(&self) -> Option<Attachment> {
        match self.inner {
            Inner::ServerOrigin {
                ref content,
                ref digest,
                ref encoding,
                ref revpos,
            } => {
                Some(Attachment {
                    content_type: self.content_type.clone(),
                    inner: Inner::ServerOrigin {
                        content: content.to_length_only(),
                        digest: digest.clone(),
                        encoding: encoding.clone(),
                        revpos: revpos.clone(),
                    },
                })
            }
            _ => None,
        }
    }

    /// Constructs a stubbed copy of the attachment suitable for
    /// multipart-encoding.
    ///
    /// The returned attachment loses all information about the attachment
    /// except for its content type and content length. The intention is for the
    /// application to:
    ///
    /// 1. Serialize the attachment stub within an enclosed document, as JSON,
    /// and,
    ///
    /// 2. Send the attachment content as multipart data, within the same HTTP
    /// request.
    ///
    /// See the [CouchDB
    /// documentation](http://docs.couchdb.org/en/2.0.0/api/document/common.html#creating-multiple-attachments)
    /// for details.
    ///
    /// # Example
    ///
    /// ```rust
    /// extern crate couchdb;
    /// extern crate mime;
    /// extern crate serde_json;
    ///
    /// let att = couchdb::Attachment::new(
    ///     mime::TEXT_PLAIN,
    ///     Vec::from(b"Lorem ipsum dolor sit amet".as_ref())
    /// ).to_multipart_stub();
    ///
    /// let encoded = serde_json::to_vec(&att).unwrap();
    ///
    /// # let decoded = serde_json::from_slice::<serde_json::Value>(&encoded)
    /// #     .unwrap();
    /// #
    /// # let expected = serde_json::Value::Object(
    /// #     vec![
    /// #         (String::from("content_type"), serde_json::Value::String(String::from("text/plain"))),
    /// #         (String::from("follows"), serde_json::Value::Bool(true)),
    /// #         (String::from("length"), serde_json::Value::Number(serde_json::Number::from(26))),
    /// #     ].into_iter().collect::<serde_json::value::Map<String, serde_json::Value>>()
    /// # );
    /// #
    /// # assert_eq!(decoded, expected);
    /// #
    /// // encoded:
    /// //
    /// // {
    /// //     "content_type": "text/plain",
    /// //     "follows": true,
    /// //     "length": 26
    /// // }
    /// ```
    ///
    pub fn to_multipart_stub(&self) -> Attachment {
        Attachment {
            content_type: self.content_type.clone(),
            inner: Inner::Follows { content_length: self.content_length() },
        }
    }

    /// Borrows the attachment's digest, if available.
    ///
    /// An attachment's digest is available if and only if it originates from
    /// the server.
    ///
    pub fn digest(&self) -> Option<&Digest> {
        match self.inner {
            Inner::ServerOrigin { ref digest, .. } => Some(digest),
            Inner::ClientOrigin { .. } => None,
            Inner::Follows { .. } => None,
        }
    }

    /// Returns the attachment's encoding information, if available.
    pub fn encoding(&self) -> Option<&Encoding> {
        match self.inner {
            Inner::ServerOrigin { ref encoding, .. } => encoding.as_ref().clone(),
            Inner::ClientOrigin { .. } => None,
            Inner::Follows { .. } => None,
        }
    }

    /// Returns the attachment's revision sequence number—i.e., the `revpos`
    /// attachment field.
    pub fn revision_sequence(&self) -> Option<u64> {
        match self.inner {
            Inner::ServerOrigin { revpos, .. } => Some(revpos),
            Inner::ClientOrigin { .. } => None,
            Inner::Follows { .. } => None,
        }
    }
}

impl<'a> Deserialize<'a> for Attachment {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'a>,
    {
        #[derive(Deserialize)]
        struct T {
            content_type: SerializableMime,
            data: Option<SerializableBase64>,
            digest: Digest,
            encoded_length: Option<u64>,
            encoding: Option<String>,
            length: Option<u64>,
            revpos: u64,
            // stub: Option<bool>, // unused
        }

        let x = T::deserialize(deserializer)?;

        let encoding = match (x.encoding, x.encoded_length) {
            (Some(codec), Some(length)) => Some(Encoding {
                codec: EncodingCodec::from(codec),
                length: length,
            }),
            (None, None) => None,
            _ => return Err(serde::de::Error::invalid_value(
                serde::de::Unexpected::Map,
                &Expectation(
                    "a JSON object with complete CouchDB attachment encoding info OR no such info",
                ),
            )),
        };

        let inner = if let Some(SerializableBase64(bytes)) = x.data {
            Inner::ServerOrigin {
                content: Content::WithBytes(bytes),
                digest: x.digest,
                encoding: encoding,
                revpos: x.revpos,
            }
        } else if let Some(content_length) = x.length {
            Inner::ServerOrigin {
                content: Content::WithLength(content_length),
                digest: x.digest,
                encoding: encoding,
                revpos: x.revpos,
            }
        } else {
            return Err(serde::de::Error::invalid_value(
                serde::de::Unexpected::Map,
                &Expectation(
                    "a JSON object with CouchDB attachment content OR content length",
                ),
            ));
        };

        Ok(Attachment {
            content_type: x.content_type.0,
            inner: inner,
        })
    }
}

impl Serialize for Attachment {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        #[derive(Debug, Default, Deserialize, Serialize)]
        struct T {
            content_type: String,
            #[serde(skip_serializing_if = "Option::is_none")]
            data: Option<String>,
            #[serde(skip_serializing_if = "Option::is_none")]
            stub: Option<bool>,
            #[serde(skip_serializing_if = "Option::is_none")]
            follows: Option<bool>,
            #[serde(skip_serializing_if = "Option::is_none")]
            length: Option<u64>,
        }

        let mut x = T::default();
        x.content_type = self.content_type.to_string();

        match self.inner {
            Inner::ServerOrigin { .. } => {
                x.stub = Some(true);
            }
            Inner::ClientOrigin { ref content } => {
                x.data = Some(base64::encode(content));
            }
            Inner::Follows { content_length } => {
                x.follows = Some(true);
                x.length = Some(content_length);
            }
        };

        x.serialize(serializer)
    }
}

struct Expectation(&'static str);

impl serde::de::Expected for Expectation {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        f.write_str(self.0)
    }
}

struct SerializableBase64(Vec<u8>);

impl<'a> Deserialize<'a> for SerializableBase64 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'a>,
    {
        let s = String::deserialize(deserializer)?;

        let v = base64::decode(&s).map_err(|_| {
            serde::de::Error::invalid_value(
                serde::de::Unexpected::Str(&s),
                &Expectation("a base64-encoded string containing CouchDB attachment data"),
            )
        })?;

        Ok(SerializableBase64(v))
    }
}

impl<'a> Deserialize<'a> for Digest {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'a>,
    {
        let s = String::deserialize(deserializer)?;

        let v = Digest::from_str(&s).map_err(|_| {
            serde::de::Error::invalid_value(
                serde::de::Unexpected::Str(&s),
                &Expectation("a CouchDB attachment digest"),
            )
        })?;

        Ok(v)
    }
}

impl Content {
    /// Returns a length-only copy of the content.
    pub fn to_length_only(&self) -> Content {
        match *self {
            Content::WithBytes(ref bytes) => Content::WithLength(bytes.len() as u64),
            Content::WithLength(length) => Content::WithLength(length),
        }
    }
}

impl Digest {
    /// Borrows the encoded digest value.
    ///
    /// For example, with an MD5 digest, the value is the 16-byte MD5 sum of the
    /// attachment's content.
    ///
    pub fn bytes(&self) -> &[u8] {
        match *self {
            Digest::Md5 { ref value } => value,
            Digest::Other { ref value, .. } => value,
        }
    }

    /// Returns whether the digest is MD5-encoded.
    pub fn is_md5(&self) -> bool {
        match *self {
            Digest::Md5 { .. } => true,
            _ => false,
        }
    }
}

impl FromStr for Digest {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self, Error> {

        let mut iter = s.splitn(2, '-');
        let name = iter.next().unwrap();
        let value = iter.next().ok_or(Error::BadDigest)?;
        let value = base64::decode(&value).map_err(|_| Error::BadDigest)?;

        Ok(match name {
            "md5" => Digest::Md5 { value: value },
            _ => Digest::Other {
                name: String::from(name),
                value: value,
            },
        })
    }
}

impl Encoding {
    /// Returns the size of the attachment's compressed content, in bytes.
    pub fn length(&self) -> u64 {
        self.length
    }

    /// Returns whether the compression codec is gzip.
    pub fn is_gzip(&self) -> bool {
        self.codec == EncodingCodec::Gzip
    }
}

impl From<String> for EncodingCodec {
    fn from(s: String) -> Self {
        match s.as_str() {
            "gzip" => EncodingCodec::Gzip,
            _ => EncodingCodec::Other(s),
        }
    }
}

struct SerializableMime(Mime);

impl<'a> Deserialize<'a> for SerializableMime {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'a>,
    {
        let s = String::deserialize(deserializer)?;

        let v = Mime::from_str(&s).map_err(|_| {
            serde::de::Error::invalid_value(
                serde::de::Unexpected::Str(&s),
                &Expectation("a string specifying a MIME type"),
            )
        })?;

        Ok(SerializableMime(v))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use {mime, serde_json};

    #[test]
    fn attachment_deserializes_as_stub() {

        let source = r#"{
            "content_type": "text/plain",
            "digest": "md5-Ids41vtv725jyrN7iUvMcQ==",
            "length": 1872,
            "revpos": 4,
            "stub": true
        }"#;

        let expected = Attachment {
            content_type: mime::TEXT_PLAIN,
            inner: Inner::ServerOrigin {
                content: Content::WithLength(1872),
                digest: Digest::Md5 {
                    value: Vec::from(
                        b"\x21\xdb\x38\xd6\
                         \xfb\x6f\xef\x6e\
                         \x63\xca\xb3\x7b\
                         \x89\x4b\xcc\x71"
                            .as_ref(),
                    ),
                },
                encoding: None,
                revpos: 4,
            },
        };

        let got: Attachment = serde_json::from_str(source).unwrap();
        assert_eq!(got, expected);
    }

    #[test]
    fn attachment_deserializes_with_data() {

        let source = r#"{
            "content_type": "image/gif",
            "data": "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7",
            "digest": "md5-2JdGiI2i2VELZKnwMers1Q==",
            "revpos": 2
        }"#;

        let expected = Attachment {
            content_type: mime::IMAGE_GIF,
            inner: Inner::ServerOrigin {
                content: Content::WithBytes(Vec::from(
                    b"\x47\x49\x46\x38\
                    \x39\x61\x01\x00\
                    \x01\x00\x80\x00\
                    \x00\x00\x00\x00\
                    \xff\xff\xff\x21\
                    \xf9\x04\x01\x00\
                    \x00\x00\x00\x2c\
                    \x00\x00\x00\x00\
                    \x01\x00\x01\x00\
                    \x00\x02\x01\x44\
                    \x00\x3b"
                        .as_ref(),
                )),
                digest: Digest::Md5 {
                    value: Vec::from(
                        b"\xd8\x97\x46\x88\
                        \x8d\xa2\xd9\x51\
                        \x0b\x64\xa9\xf0\
                        \x31\xea\xec\xd5"
                            .as_ref(),
                    ),
                },
                encoding: None,
                revpos: 2,
            },
        };

        let got: Attachment = serde_json::from_str(source).unwrap();
        assert_eq!(got, expected);
    }

    #[test]
    fn attachment_deserializes_with_encoding_info() {

        let source = r#"{
            "content_type": "text/plain",
            "digest": "md5-Ids41vtv725jyrN7iUvMcQ==",
            "encoded_length": 693,
            "encoding": "gzip",
            "length": 1872,
            "revpos": 4,
            "stub": true
        }"#;

        let expected = Attachment {
            content_type: mime::TEXT_PLAIN,
            inner: Inner::ServerOrigin {
                content: Content::WithLength(1872),
                digest: Digest::Md5 {
                    value: Vec::from(
                        b"\x21\xdb\x38\xd6\
                         \xfb\x6f\xef\x6e\
                         \x63\xca\xb3\x7b\
                         \x89\x4b\xcc\x71"
                            .as_ref(),
                    ),
                },
                encoding: Some(Encoding {
                    length: 693,
                    codec: EncodingCodec::Gzip,
                }),
                revpos: 4,
            },
        };

        let got: Attachment = serde_json::from_str(source).unwrap();
        assert_eq!(got, expected);
    }

    #[test]
    fn client_origin_attachment_serializes_with_content() {

        let source = Attachment::new(
            mime::TEXT_PLAIN,
            Vec::from(b"Lorem ipsum dolor sit amet".as_ref()),
        );

        let encoded = serde_json::to_vec(&source).unwrap();
        let decoded: serde_json::Value = serde_json::from_slice(&encoded).unwrap();

        let expected = json!({
            "content_type": "text/plain",
            "data": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ=",
        });

        assert_eq!(decoded, expected);
    }

    #[test]
    fn server_origin_attachment_serializes_as_stub() {

        let source = Attachment {
            content_type: mime::TEXT_PLAIN,
            inner: Inner::ServerOrigin {
                content: Content::WithLength(1872),
                digest: Digest::Md5 {
                    value: Vec::from(
                        b"\x21\xdb\x38\xd6\
                         \xfb\x6f\xef\x6e\
                         \x63\xca\xb3\x7b\
                         \x89\x4b\xcc\x71"
                            .as_ref(),
                    ),
                },
                encoding: Some(Encoding {
                    length: 693,
                    codec: EncodingCodec::Gzip,
                }),
                revpos: 4,
            },
        };

        let encoded = serde_json::to_vec(&source).unwrap();
        let decoded: serde_json::Value = serde_json::from_slice(&encoded).unwrap();

        let expected = json!({
            "content_type": "text/plain",
            "stub": true,
        });

        assert_eq!(decoded, expected);
    }
}