couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From beno...@apache.org
Subject [1/3] couch commit: updated refs/heads/1994-merge-rcouch to 7ad4bc6
Date Sun, 13 Jul 2014 13:00:46 GMT
Repository: couchdb-couch
Updated Branches:
  refs/heads/1994-merge-rcouch 5eb93a4f2 -> 7ad4bc655


send a doc without attachment as multipart if needded


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/7ad4bc65
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/7ad4bc65
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/7ad4bc65

Branch: refs/heads/1994-merge-rcouch
Commit: 7ad4bc655a7c40b3eddc8eb4096576ff7b293180
Parents: 0aa924a
Author: benoitc <bchesneau@gmail.com>
Authored: Tue Feb 18 21:52:57 2014 +0100
Committer: benoitc <bchesneau@gmail.com>
Committed: Sun Jul 13 14:59:53 2014 +0200

----------------------------------------------------------------------
 src/couch_doc.erl | 56 ++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 41 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/7ad4bc65/src/couch_doc.erl
----------------------------------------------------------------------
diff --git a/src/couch_doc.erl b/src/couch_doc.erl
index efde8c1..6c27e3d 100644
--- a/src/couch_doc.erl
+++ b/src/couch_doc.erl
@@ -18,7 +18,8 @@
 -export([from_json_obj/1,to_json_obj/2,has_stubs/1, merge_stubs/2]).
 -export([validate_docid/1]).
 -export([doc_from_multi_part_stream/2]).
--export([doc_to_multi_part_stream/5, len_doc_to_multi_part_stream/4]).
+-export([doc_to_multi_part_stream/5, doc_to_multi_part_stream/6,
+         len_doc_to_multi_part_stream/4, len_doc_to_multi_part_stream/5]).
 -export([abort_multi_part_stream/1]).
 -export([to_path/1]).
 -export([mp_parse_doc/2]).
@@ -461,6 +462,12 @@ fold_streamed_data(RcvFun, LenLeft, Fun, Acc) when LenLeft > 0->
     fold_streamed_data(RcvFun, LenLeft - size(Bin), Fun, ResultAcc).
 
 len_doc_to_multi_part_stream(Boundary, JsonBytes, Atts, SendEncodedAtts) ->
+    len_doc_to_multi_part_stream(Boundary, JsonBytes, Atts,
+                                 SendEncodedAtts, false).
+
+len_doc_to_multi_part_stream(Boundary, JsonBytes, Atts, SendEncodedAtts,
+                             ForceMp) ->
+
     AttsSize = lists:foldl(fun(Att, AccAttsSize) ->
             #att{
                 data=Data,
@@ -505,29 +512,48 @@ len_doc_to_multi_part_stream(Boundary, JsonBytes, Atts, SendEncodedAtts)
->
                 end
             end
         end, 0, Atts),
-    if AttsSize == 0 ->
-        {<<"application/json">>, iolist_size(JsonBytes)};
-    true ->
-        {<<"multipart/related; boundary=\"", Boundary/binary, "\"">>,
-            2 + % "--"
-            size(Boundary) +
-            36 + % "\r\ncontent-type: application/json\r\n\r\n"
-            iolist_size(JsonBytes) +
-            4 + % "\r\n--"
-            size(Boundary) +
-            + AttsSize +
-            2 % "--"
-            }
+    case AttsSize of
+        0 when ForceMp /= true ->
+            {<<"application/json">>, iolist_size(JsonBytes)};
+        0 ->
+            {<<"multipart/related; boundary=\"", Boundary/binary, "\"">>,
+                2 + % "--"
+                size(Boundary) +
+                36 + % "\r\ncontent-type: application/json\r\n\r\n"
+                iolist_size(JsonBytes) +
+                4 + % "\r\n--"
+                size(Boundary) +
+                2};
+        _ ->
+            {<<"multipart/related; boundary=\"", Boundary/binary, "\"">>,
+                2 + % "--"
+                size(Boundary) +
+                36 + % "\r\ncontent-type: application/json\r\n\r\n"
+                iolist_size(JsonBytes) +
+                4 + % "\r\n--"
+                size(Boundary) +
+                + AttsSize +
+                2 % "--"
+                }
     end.
 
 doc_to_multi_part_stream(Boundary, JsonBytes, Atts, WriteFun,
-    SendEncodedAtts) ->
+                         SendEncodedAtts) ->
+    doc_to_multi_part_stream(Boundary, JsonBytes, Atts, WriteFun,
+                         SendEncodedAtts, false).
+
+doc_to_multi_part_stream(Boundary, JsonBytes, Atts, WriteFun,
+    SendEncodedAtts, ForceMp) ->
     case lists:any(fun(#att{data=Data})-> Data /= stub end, Atts) of
     true ->
         WriteFun([<<"--", Boundary/binary,
                 "\r\nContent-Type: application/json\r\n\r\n">>,
                 JsonBytes, <<"\r\n--", Boundary/binary>>]),
         atts_to_mp(Atts, Boundary, WriteFun, SendEncodedAtts);
+    false when ForceMp ->
+        WriteFun([<<"--", Boundary/binary,
+                "\r\nContent-Type: application/json\r\n\r\n">>,
+                  JsonBytes, <<"\r\n--", Boundary/binary, "--" >>]);
     false ->
         WriteFun(JsonBytes)
     end.


Mime
View raw message