summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Lenz <cmlenz@apache.org>2008-05-20 19:59:56 +0000
committerChristopher Lenz <cmlenz@apache.org>2008-05-20 19:59:56 +0000
commit9cd5e9810997f7255287cb283e9829f1f3512fac (patch)
tree92d38dd1691b99fdc7b63ed96882aa6aa46fcc6d
parent5da7c1a8ba70f41a8e92cb1efee1f6c6898a2901 (diff)
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
-rw-r--r--src/couchdb/couch_httpd.erl28
1 files changed, 18 insertions, 10 deletions
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.