summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_db.erl
diff options
context:
space:
mode:
authorAdam Kocoloski <kocolosk@apache.org>2009-05-17 17:51:42 +0000
committerAdam Kocoloski <kocolosk@apache.org>2009-05-17 17:51:42 +0000
commit5ff2faec548f4db6688a25aac2f704e6894c7bc9 (patch)
tree60500acf51105e915d0d8ee70b0f0b83b4e9c452 /src/couchdb/couch_db.erl
parent58f764496d3f3b32d9ce2cc701633271292aa5d2 (diff)
work around ibrowse giving response chunks too many bytes. Thanks Antony.
Initially committed on the 0.9.x branch by accident in 775634. We want to include this in 0.9.1 anyway, though, so shouldn't ultimately be a problem. git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@775724 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_db.erl')
-rw-r--r--src/couchdb/couch_db.erl20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/couchdb/couch_db.erl b/src/couchdb/couch_db.erl
index 95a68311..82231924 100644
--- a/src/couchdb/couch_db.erl
+++ b/src/couchdb/couch_db.erl
@@ -644,12 +644,24 @@ write_streamed_attachment(_Stream, _F, 0, SpAcc) ->
{ok, SpAcc};
write_streamed_attachment(Stream, F, LenLeft, nil) ->
Bin = F(),
- {ok, StreamPointer} = couch_stream:write(Stream, Bin),
- write_streamed_attachment(Stream, F, LenLeft - size(Bin), StreamPointer);
+ TruncatedBin = check_bin_length(LenLeft, Bin),
+ {ok, SpAcc} = couch_stream:write(Stream, TruncatedBin),
+ write_streamed_attachment(Stream, F, LenLeft - size(TruncatedBin), SpAcc);
write_streamed_attachment(Stream, F, LenLeft, SpAcc) ->
Bin = F(),
- {ok, _} = couch_stream:write(Stream, Bin),
- write_streamed_attachment(Stream, F, LenLeft - size(Bin), SpAcc).
+ TruncatedBin = check_bin_length(LenLeft, Bin),
+ {ok, _} = couch_stream:write(Stream, TruncatedBin),
+ write_streamed_attachment(Stream, F, LenLeft - size(TruncatedBin), SpAcc).
+
+%% on rare occasions ibrowse seems to process a chunked response incorrectly
+%% and include an extra "\r" in the last chunk. This code ensures that we
+%% truncate the downloaed attachment at the length specified in the metadata.
+check_bin_length(LenLeft, Bin) when size(Bin) > LenLeft ->
+ <<ValidData:LenLeft/binary, Crap/binary>> = Bin,
+ ?LOG_ERROR("write_streamed_attachment has written too much expected: ~p" ++
+ " got: ~p tail: ~p", [LenLeft, size(Bin), Crap]),
+ ValidData;
+check_bin_length(_, Bin) -> Bin.
enum_docs_since_reduce_to_count(Reds) ->
couch_btree:final_reduce(