nickva commented on a change in pull request #1195: Add support for bulk get
with Accept:"multipart/mixed" or "multipart/related"
URL: https://github.com/apache/couchdb/pull/1195#discussion_r232007407
##########
File path: src/chttpd/src/chttpd_db.erl
##########
@@ -476,19 +477,59 @@ db_req(#httpd{method='POST', path_parts=[_,
<<"_bulk_get">>]}=Req, Db) ->
#doc_query_args{
options = Options
} = bulk_get_parse_doc_query(Req),
-
- {ok, Resp} = start_json_response(Req, 200),
- send_chunk(Resp, <<"{\"results\": [">>),
-
- lists:foldl(fun(Doc, Sep) ->
- {DocId, Results, Options1} = bulk_get_open_doc_revs(Db, Doc,
- Options),
- bulk_get_send_docs_json(Resp, DocId, Results, Options1, Sep),
- <<",">>
- end, <<"">>, Docs),
-
- send_chunk(Resp, <<"]}">>),
- end_json_response(Resp)
+ % Decide whether to use multipart response or application/json
+ AcceptMixedMp = MochiReq:accepts_content_type("multipart/mixed"),
+ AcceptRelatedMp =
MochiReq:accepts_content_type("multipart/related"),
+ AcceptMp = AcceptMixedMp orelse AcceptRelatedMp,
+ case AcceptMp of
+ false ->
+ {ok, Resp} = start_json_response(Req, 200),
+ send_chunk(Resp, <<"{\"results\": [">>),
+ lists:foldl(fun(Doc, Sep) ->
+ {DocId, Results, Options1} = bulk_get_open_doc_revs(Db,
Doc,
+
Options),
+ bulk_get_send_docs_json(Resp, DocId, Results, Options1,
Sep),
+ <<",">>
+ end, <<"">>, Docs),
+ send_chunk(Resp, <<"]}">>),
+ end_json_response(Resp);
+ true ->
+ OuterBoundary = bulk_get_multipart_boundary(),
+ MpType = case AcceptMixedMp of
+ true ->
+ "multipart/mixed";
+ _ ->
+ "multipart/related"
+ end,
+ CType = {"Content-Type", MpType ++ "; boundary=\"" ++
+ ?b2l(OuterBoundary) ++ "\""},
+ {ok, Resp} = start_chunked_response(Req, 200, [CType]),
+ lists:foldl(fun(Doc, _Pre) ->
+ case bulk_get_open_doc_revs(Db, Doc, Options) of
+ {_, {ok, []}, _Options1} ->
+ ok;
+ {_, {ok, Results}, Options1} ->
+ send_docs_multipart(bulk_get, Req, Results,
Options1,
+ OuterBoundary, Resp);
+ {DocId, {error, {RevId, Error, Reason}}, _Options1}
->
+ Json = ?JSON_ENCODE({[{<<"id">>, DocId},
+ {<<"rev">>, RevId},
+ {<<"error">>, Error},
+ {<<"reason">>, Reason}]}),
+ couch_httpd:send_chunk(Resp,
+ [<<"\r\n--", OuterBoundary/binary>>,
+ <<"\r\nContent-Type: application/json;
error=\"true\"\r\n\r\n">>,
+ Json])
+ end
+ end, <<"">>, Docs),
+ case Docs of
+ [] ->
+ ok;
+ _ ->
+ couch_httpd:send_chunk(Resp, <<"\r\n", "--",
OuterBoundary/binary, "--\r\n">>)
+ end,
+ couch_httpd:last_chunk(Resp)
Review comment:
This will affect the non-multipart response as well, should be move it in
one level so it only affects multipart. `end_json_response` already calls
last_chunk
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services