From 4faf660a4dfe5849fae96ea7eb15379069c27405 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Sat, 14 Nov 2009 01:14:59 +0000 Subject: Better testing of multipart/related git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@836087 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_doc.erl | 13 +++++++------ src/couchdb/couch_httpd_db.erl | 12 ++++++------ 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/couchdb/couch_doc.erl b/src/couchdb/couch_doc.erl index 4cb20d6c..079d9dfa 100644 --- a/src/couchdb/couch_doc.erl +++ b/src/couchdb/couch_doc.erl @@ -340,14 +340,14 @@ fold_streamed_data(RcvFun, LenLeft, Fun, Acc) when LenLeft > 0-> len_doc_to_multi_part_stream(Boundary,JsonBytes,Atts,AttsSinceRevPos) -> 2 + % "--" size(Boundary) + - 34 + % "\r\ncontent-type: application/json\r\n" + 36 + % "\r\ncontent-type: application/json\r\n\r\n" iolist_size(JsonBytes) + 4 + % "\r\n--" size(Boundary) + + lists:foldl(fun(#att{revpos=RevPos,len=Len}, AccAttsSize) -> if RevPos > AttsSinceRevPos -> AccAttsSize + - 2 + % "\r\n" + 4 + % "\r\n\r\n" Len + 4 + % "\r\n--" size(Boundary); @@ -358,17 +358,18 @@ len_doc_to_multi_part_stream(Boundary,JsonBytes,Atts,AttsSinceRevPos) -> 2. % "--" doc_to_multi_part_stream(Boundary,JsonBytes,Atts,AttsSinceRevPos,WriteFun) -> - WriteFun([<<"--", Boundary, "\r\ncontent-type: application/json\r\n">>, - JsonBytes, <<"\r\n--", Boundary>>]), + WriteFun([<<"--", Boundary/binary, + "\r\ncontent-type: application/json\r\n\r\n">>, + JsonBytes, <<"\r\n--", Boundary/binary>>]), atts_to_mp(Atts, Boundary, WriteFun, AttsSinceRevPos). atts_to_mp([], _Boundary, WriteFun, _AttsSinceRevPos) -> WriteFun(<<"--">>); atts_to_mp([#att{revpos=RevPos} = Att | RestAtts], Boundary, WriteFun, AttsSinceRevPos) when RevPos > AttsSinceRevPos -> - WriteFun(<<"\r\n">>), + WriteFun(<<"\r\n\r\n">>), att_foldl(Att, fun(Data, ok) -> WriteFun(Data) end, ok), - WriteFun(<<"\r\n--", Boundary>>), + WriteFun(<<"\r\n--", Boundary/binary>>), atts_to_mp(RestAtts, Boundary, WriteFun, AttsSinceRevPos); atts_to_mp([_ | RestAtts], Boundary, WriteFun, AttsSinceRevPos) -> atts_to_mp(RestAtts, Boundary, WriteFun, AttsSinceRevPos). diff --git a/src/couchdb/couch_httpd_db.erl b/src/couchdb/couch_httpd_db.erl index 7be10d42..4138db7d 100644 --- a/src/couchdb/couch_httpd_db.erl +++ b/src/couchdb/couch_httpd_db.erl @@ -784,7 +784,7 @@ db_doc_req(#httpd{method='PUT'}=Req, Db, DocId) -> Loc = absolute_uri(Req, "/" ++ ?b2l(Db#db.name) ++ "/" ++ ?b2l(DocId)), RespHeaders = [{"Location", Loc}], - case couch_httpd:header_value(Req, "content-type") of + case couch_httpd:header_value(Req, "Content-Type") of ("multipart/related" ++ _Rest) = ContentType-> Doc0 = couch_doc:doc_from_multi_part_stream(ContentType, fun() -> receive_request_data(Req) end), @@ -869,18 +869,18 @@ send_doc_efficiently(Req, #doc{atts=Atts}=Doc, Headers, Options) -> undefined -> []; AcceptHeader -> string:tokens(AcceptHeader, ", ") end, - case lists:member(AcceptedTypes, "multipart/related") of + case lists:member("multipart/related", AcceptedTypes) of false -> send_json(Req, 200, [], couch_doc:to_json_obj(Doc, Options)); true -> Boundary = couch_uuids:random(), - JsonBytes = ?JSON_ENCODE(couch_doc:to_json_obj(Doc, Options)), + JsonBytes = ?JSON_ENCODE(couch_doc:to_json_obj(Doc, [follows|Options])), AttsSinceRevPos = proplists:get_value(atts_after_revpos, Options, 0), Len = couch_doc:len_doc_to_multi_part_stream(Boundary,JsonBytes,Atts, AttsSinceRevPos), - CType = {<<"content-type">>, - <<"multipart/related; boundary=", Boundary/binary>>}, - Resp = start_response_length(Req, 200, [CType | Headers], Len), + CType = {<<"Content-Type">>, + <<"multipart/related; boundary=\"", Boundary/binary, "\"">>}, + {ok, Resp} = start_response_length(Req, 200, [CType|Headers], Len), couch_doc:doc_to_multi_part_stream(Boundary,JsonBytes,Atts, AttsSinceRevPos, fun(Data) -> couch_httpd:send(Resp, Data) end) -- cgit v1.2.3