summaryrefslogtreecommitdiff
path: root/apps/couch/src/couch_httpd.erl
diff options
context:
space:
mode:
authorRobert Newson <robert.newson@cloudant.com>2011-09-28 11:18:06 +0100
committerRobert Newson <robert.newson@cloudant.com>2011-09-28 11:32:50 +0100
commit954ddf0fca558f17f39e68df8311ee9057beb390 (patch)
tree2542c112210363076be1a8bc7b24e13bfaf1c055 /apps/couch/src/couch_httpd.erl
parentc8d7b6d8c3cb881d525be80bc6b16bc08822df65 (diff)
parentbefbdfb11f45bd2a5ccffb6b0d5ac04435ac9e55 (diff)
Merge 1.1.x changes
Conflicts: apps/couch/include/couch_db.hrl apps/couch/src/couch_db.erl apps/couch/src/couch_os_process.erl apps/couch/src/couch_query_servers.erl apps/couch/src/couch_rep.erl apps/couch/src/couch_replication_manager.erl apps/couch/src/couch_view_compactor.erl apps/couch/src/couch_view_group.erl apps/couch/src/couch_view_updater.erl configure.ac couchjs/c_src/http.c couchjs/c_src/main.c couchjs/c_src/utf8.c etc/windows/couchdb.iss.tpl src/couchdb/priv/Makefile.am src/couchdb/priv/couch_js/main.c test/etap/160-vhosts.t test/etap/200-view-group-no-db-leaks.t test/etap/Makefile.am BugzID: 12645
Diffstat (limited to 'apps/couch/src/couch_httpd.erl')
-rw-r--r--apps/couch/src/couch_httpd.erl55
1 files changed, 36 insertions, 19 deletions
diff --git a/apps/couch/src/couch_httpd.erl b/apps/couch/src/couch_httpd.erl
index 8fb2687c..602bdf2b 100644
--- a/apps/couch/src/couch_httpd.erl
+++ b/apps/couch/src/couch_httpd.erl
@@ -469,16 +469,24 @@ body_length(Req) ->
Unknown -> {unknown_transfer_encoding, Unknown}
end.
-body(#httpd{mochi_req=MochiReq, req_body=ReqBody}) ->
- case ReqBody of
+body(#httpd{mochi_req=MochiReq, req_body=undefined} = Req) ->
+ case body_length(Req) of
undefined ->
- % Maximum size of document PUT request body (4GB)
MaxSize = list_to_integer(
couch_config:get("couchdb", "max_document_size", "4294967296")),
MochiReq:recv_body(MaxSize);
- _Else ->
- ReqBody
- end.
+ chunked ->
+ ChunkFun = fun({0, _Footers}, Acc) ->
+ lists:reverse(Acc);
+ ({_Len, Chunk}, Acc) ->
+ [Chunk | Acc]
+ end,
+ recv_chunked(Req, 8192, ChunkFun, []);
+ Len ->
+ MochiReq:recv_body(Len)
+ end;
+body(#httpd{req_body=ReqBody}) ->
+ ReqBody.
json_body(Httpd) ->
?JSON_DECODE(body(Httpd)).
@@ -619,25 +627,25 @@ send_json(Req, Code, Value) ->
send_json(Req, Code, [], Value).
send_json(Req, Code, Headers, Value) ->
+ initialize_jsonp(Req),
DefaultHeaders = [
{"Content-Type", negotiate_content_type(Req)},
{"Cache-Control", "must-revalidate"}
],
- Body = [start_jsonp(Req), ?JSON_ENCODE(Value), end_jsonp(), $\n],
+ Body = [start_jsonp(), ?JSON_ENCODE(Value), end_jsonp(), $\n],
send_response(Req, Code, DefaultHeaders ++ Headers, Body).
start_json_response(Req, Code) ->
start_json_response(Req, Code, []).
start_json_response(Req, Code, Headers) ->
+ initialize_jsonp(Req),
DefaultHeaders = [
{"Content-Type", negotiate_content_type(Req)},
{"Cache-Control", "must-revalidate"}
],
- start_jsonp(Req), % Validate before starting chunked.
- %start_chunked_response(Req, Code, DefaultHeaders ++ Headers).
{ok, Resp} = start_chunked_response(Req, Code, DefaultHeaders ++ Headers),
- case start_jsonp(Req) of
+ case start_jsonp() of
[] -> ok;
Start -> send_chunk(Resp, Start)
end,
@@ -647,7 +655,7 @@ end_json_response(Resp) ->
send_chunk(Resp, end_jsonp() ++ [$\n]),
last_chunk(Resp).
-start_jsonp(Req) ->
+initialize_jsonp(Req) ->
case get(jsonp) of
undefined -> put(jsonp, qs_value(Req, "callback", no_jsonp));
_ -> ok
@@ -660,14 +668,9 @@ start_jsonp(Req) ->
% make sure jsonp is configured on (default off)
case couch_config:get("httpd", "allow_jsonp", "false") of
"true" ->
- validate_callback(CallBack),
- CallBack ++ "(";
+ validate_callback(CallBack);
_Else ->
- % this could throw an error message, but instead we just ignore the
- % jsonp parameter
- % throw({bad_request, <<"JSONP must be configured before using.">>})
- put(jsonp, no_jsonp),
- []
+ put(jsonp, no_jsonp)
end
catch
Error ->
@@ -676,6 +679,13 @@ start_jsonp(Req) ->
end
end.
+start_jsonp() ->
+ case get(jsonp) of
+ no_jsonp -> [];
+ [] -> [];
+ CallBack -> CallBack ++ "("
+ end.
+
end_jsonp() ->
Resp = case get(jsonp) of
no_jsonp -> [];
@@ -836,7 +846,14 @@ send_redirect(Req, Path) ->
Headers = [{"Location", couch_httpd:absolute_uri(Req, Path)}],
send_response(Req, 301, Headers, <<>>).
-negotiate_content_type(#httpd{mochi_req=MochiReq}) ->
+negotiate_content_type(Req) ->
+ case get(jsonp) of
+ no_jsonp -> negotiate_content_type1(Req);
+ [] -> negotiate_content_type1(Req);
+ _Callback -> "text/javascript"
+ end.
+
+negotiate_content_type1(#httpd{mochi_req=MochiReq}) ->
%% 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