summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authordrebs <drebs@leap.se>2016-07-23 17:21:58 +0200
committerdrebs <drebs@leap.se>2016-08-01 21:09:03 -0300
commit8596668c4bb38251da8891e8fd7a23bcf972c21a (patch)
treee2cd2c0384be73ea64c8901a3a4dc1b1d0e86bca /common
parentc7b464077215425759ab402fb2314f4e8f9acd7e (diff)
[bug] fix order of multipart serialization when writing to couch
The couch backend makes use of attachments and multipart structure for writing the document to the couch database. For that to work, the order in which attachments are described must match the actual order in which attachments are written to the couch http stream. This was not being properly taken care of, and eventually the json serializer was arbitrarilly ordering the attachments description in a way that it didn't match the actual order of attachments writing. This commit fixes that by using json.dumps() sort_keys parameter and making sure conflicts are always written before content.
Diffstat (limited to 'common')
-rw-r--r--common/src/leap/soledad/common/couch/__init__.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/common/src/leap/soledad/common/couch/__init__.py b/common/src/leap/soledad/common/couch/__init__.py
index ca0c2855..ba3192a4 100644
--- a/common/src/leap/soledad/common/couch/__init__.py
+++ b/common/src/leap/soledad/common/couch/__init__.py
@@ -714,7 +714,18 @@ class CouchDatabase(object):
if not self.batching:
buf = StringIO()
envelope = MultipartWriter(buf)
- envelope.add('application/json', json.dumps(couch_doc))
+ # the order in which attachments are described inside the
+ # serialization of the couch document must match the order in which
+ # they are actually written in the multipart structure. Because of
+ # that, we use `sorted_keys=True` in the json serialization (so
+ # "u1db_conflicts" comes before "u1db_content" on the couch
+ # document attachments description), and also reverse the order of
+ # the parts before writing them, so the "conflict" part is written
+ # before the "content" part.
+ envelope.add(
+ 'application/json',
+ json.dumps(couch_doc, sort_keys=True))
+ parts.reverse()
for part in parts:
envelope.add('application/octet-stream', part)
envelope.close()