From 9cd5e9810997f7255287cb283e9829f1f3512fac Mon Sep 17 00:00:00 2001 From: Christopher Lenz Date: Tue, 20 May 2008 19:59:56 +0000 Subject: Only use chunked encoding when we actually make use of it to iteratively write the response (for example for views). Otherwise just send a normal response with a Content-Length header. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@658408 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_httpd.erl | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 915889ff..694c7a85 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -870,25 +870,33 @@ send_json(Req, Code, Value) -> send_json(Req, Code, [], Value). send_json(Req, Code, Headers, Value) -> - Resp = start_json_response(Req, Code, Headers), - Resp:write_chunk(cjson:encode(Value)), - end_json_response(Resp), + ContentType = negotiate_content_type(Req), + Body = cjson:encode(Value), + Resp = Req:respond({Code, [{"Content-Type", ContentType}] ++ Headers, + Body}), {ok, Resp}. start_json_response(Req, Code) -> start_json_response(Req, Code, []). start_json_response(Req, Code, Headers) -> + ContentType = negotiate_content_type(Req), + Req:respond({Code, [{"Content-Type", ContentType}] ++ Headers, chunked}). + +end_json_response(Resp) -> + Resp:write_chunk(""), + {ok, Resp}. + +negotiate_content_type(Req) -> + %% Determine the appropriate Content-Type header for a JSON response + %% depending on the Accept header in the request. A request that explicitly + %% lists the correct JSON MIME type will get that type, otherwise the + %% response will have the generic MIME type "text/plain" AcceptedTypes = case Req:get_header_value("Accept") of undefined -> []; AcceptHeader -> string:tokens(AcceptHeader, ", ") end, - ContentType = case lists:member("application/json", AcceptedTypes) of + case lists:member("application/json", AcceptedTypes) of true -> "application/json"; false -> "text/plain;charset=utf-8" - end, - Req:respond({Code, [{"Content-Type", ContentType}] ++ Headers, chunked}). - -end_json_response(Resp) -> - Resp:write_chunk(""), - {ok, Resp}. + end. -- cgit v1.2.3