Skip to content

Commit da7e334

Browse files
committed
Fix for reading multipart MIME dumps with attachments.
--HG-- extra : convert_revision : svn%3A7a298fb0-333a-0410-83e7-658617cd9cf3/trunk%40152
1 parent 3208316 commit da7e334

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

couchdb/multipart.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ def _current_part():
5959
sub_parts = read_multipart(fileobj, boundary=sub_boundary)
6060
if boundary is not None:
6161
yield headers, True, sub_parts
62+
headers.clear()
63+
del buf[:]
6264
else:
6365
for part in sub_parts:
6466
yield part
65-
return
67+
return
6668

6769
elif line == next_boundary:
6870
# We've reached the start of a new part, as indicated by the
@@ -75,6 +77,7 @@ def _current_part():
7577
headers.clear()
7678
del buf[:]
7779
in_headers = True
80+
7881
elif line == last_boundary:
7982
# We're done with this multipart envelope
8083
break

couchdb/tests/multipart.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ def test_flat(self):
2222
--===============1946781859==
2323
Content-Type: application/json
2424
Content-ID: bar
25-
ETag: 2235038212
25+
ETag: "1-4229094393"
2626
2727
{
28-
"_id": "bar",
29-
"_rev": "2235038212"
28+
"_id": "bar",
29+
"_rev": "1-4229094393"
3030
}
3131
--===============1946781859==
3232
Content-Type: application/json
3333
Content-ID: foo
34-
ETag: 2779219239
34+
ETag: "1-2182689334"
3535
3636
{
37-
"_id": "foo",
38-
"_rev": "2779219239",
37+
"_id": "foo",
38+
"_rev": "1-2182689334",
3939
"something": "cool"
4040
}
4141
--===============1946781859==--
@@ -47,14 +47,14 @@ def test_flat(self):
4747
self.assertEqual('application/json', headers['content-type'])
4848
if num == 0:
4949
self.assertEqual('bar', headers['content-id'])
50-
self.assertEqual('2235038212', headers['etag'])
51-
self.assertEqual('{\n "_id": "bar", \n '
52-
'"_rev": "2235038212"\n}', payload)
50+
self.assertEqual('"1-4229094393"', headers['etag'])
51+
self.assertEqual('{\n "_id": "bar",\n '
52+
'"_rev": "1-4229094393"\n}', payload)
5353
elif num == 1:
5454
self.assertEqual('foo', headers['content-id'])
55-
self.assertEqual('2779219239', headers['etag'])
56-
self.assertEqual('{\n "_id": "foo", \n "_rev": "2779219239",'
57-
' \n "something": "cool"\n}', payload)
55+
self.assertEqual('"1-2182689334"', headers['etag'])
56+
self.assertEqual('{\n "_id": "foo",\n "_rev": "1-2182689334",'
57+
'\n "something": "cool"\n}', payload)
5858
num += 1
5959
self.assertEqual(num, 2)
6060

@@ -65,23 +65,23 @@ def test_nested(self):
6565
--===============1946781859==
6666
Content-Type: application/json
6767
Content-ID: bar
68-
ETag: 2235038212
68+
ETag: "1-4229094393"
6969
7070
{
7171
"_id": "bar",
72-
"_rev": "2235038212"
72+
"_rev": "1-4229094393"
7373
}
7474
--===============1946781859==
7575
Content-Type: multipart/mixed; boundary="===============0909101126=="
7676
Content-ID: foo
77-
ETag: 2779219239
77+
ETag: "1-919589747"
7878
7979
--===============0909101126==
8080
Content-Type: application/json
8181
8282
{
8383
"_id": "foo",
84-
"_rev": "2779219239",
84+
"_rev": "1-919589747",
8585
"something": "cool"
8686
}
8787
--===============0909101126==
@@ -93,6 +93,15 @@ def test_nested(self):
9393
9494
Regards, Chris
9595
--===============0909101126==--
96+
--===============1946781859==
97+
Content-Type: application/json
98+
Content-ID: baz
99+
ETag: "1-3482142493"
100+
101+
{
102+
"_id": "baz",
103+
"_rev": "1-3482142493"
104+
}
96105
--===============1946781859==--
97106
'''
98107
num = 0
@@ -102,13 +111,13 @@ def test_nested(self):
102111
self.assertEqual(is_multipart, False)
103112
self.assertEqual('application/json', headers['content-type'])
104113
self.assertEqual('bar', headers['content-id'])
105-
self.assertEqual('2235038212', headers['etag'])
114+
self.assertEqual('"1-4229094393"', headers['etag'])
106115
self.assertEqual('{\n "_id": "bar", \n '
107-
'"_rev": "2235038212"\n}', payload)
116+
'"_rev": "1-4229094393"\n}', payload)
108117
elif num == 1:
109118
self.assertEqual(is_multipart, True)
110119
self.assertEqual('foo', headers['content-id'])
111-
self.assertEqual('2779219239', headers['etag'])
120+
self.assertEqual('"1-919589747"', headers['etag'])
112121

113122
partnum = 0
114123
for headers, is_multipart, payload in payload:
@@ -117,7 +126,7 @@ def test_nested(self):
117126
self.assertEqual('application/json',
118127
headers['content-type'])
119128
self.assertEqual('{\n "_id": "foo", \n "_rev": '
120-
'"2779219239", \n "something": '
129+
'"1-919589747", \n "something": '
121130
'"cool"\n}', payload)
122131
elif partnum == 1:
123132
self.assertEqual('text/plain', headers['content-type'])
@@ -127,9 +136,17 @@ def test_nested(self):
127136

128137
partnum += 1
129138

139+
elif num == 2:
140+
self.assertEqual(is_multipart, False)
141+
self.assertEqual('application/json', headers['content-type'])
142+
self.assertEqual('baz', headers['content-id'])
143+
self.assertEqual('"1-3482142493"', headers['etag'])
144+
self.assertEqual('{\n "_id": "baz", \n '
145+
'"_rev": "1-3482142493"\n}', payload)
146+
130147

131148
num += 1
132-
self.assertEqual(num, 2)
149+
self.assertEqual(num, 3)
133150

134151

135152
def suite():

couchdb/tools/dump.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def dump_db(dburl, username=None, password=None, boundary=None):
4949
part = inner
5050

5151
part['Content-ID'] = doc.id
52-
part['ETag'] = doc.rev
52+
part['ETag'] = '"%s"' % doc.rev
5353

5454
envelope.attach(part)
5555
return envelope.as_string()

0 commit comments

Comments
 (0)