couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cml...@apache.org
Subject svn commit: r658408 - /incubator/couchdb/trunk/src/couchdb/couch_httpd.erl
Date Tue, 20 May 2008 19:59:56 GMT
Author: cmlenz
Date: Tue May 20 12:59:56 2008
New Revision: 658408

URL: http://svn.apache.org/viewvc?rev=658408&view=rev
Log:
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.

Modified:
    incubator/couchdb/trunk/src/couchdb/couch_httpd.erl

Modified: incubator/couchdb/trunk/src/couchdb/couch_httpd.erl
URL: http://svn.apache.org/viewvc/incubator/couchdb/trunk/src/couchdb/couch_httpd.erl?rev=658408&r1=658407&r2=658408&view=diff
==============================================================================
--- incubator/couchdb/trunk/src/couchdb/couch_httpd.erl (original)
+++ incubator/couchdb/trunk/src/couchdb/couch_httpd.erl Tue May 20 12:59:56 2008
@@ -870,25 +870,33 @@
     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.



Mime
View raw message