summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Newson <robert.newson@cloudant.com>2011-10-18 15:16:57 +0100
committerRobert Newson <robert.newson@cloudant.com>2012-11-14 15:25:06 +0000
commitd9ad14be41bc341ba507d629ef0bde5139cd8029 (patch)
tree710d13ca63d5177ae55ca343fd45ce0a8c1aef5e
parentdfddfeb0507342c1dac04aee80058cdc8fb50eac (diff)
Support jsonp in externals.
Allow ?callback= for any external that returns json (i.e, uses "json":{} instead of "data":"data". BugzID: 12748
-rw-r--r--apps/couch/include/couch_db.hrl3
-rw-r--r--apps/couch/src/couch_httpd_external.erl18
2 files changed, 13 insertions, 8 deletions
diff --git a/apps/couch/include/couch_db.hrl b/apps/couch/include/couch_db.hrl
index 1c425e8d..1b46786c 100644
--- a/apps/couch/include/couch_db.hrl
+++ b/apps/couch/include/couch_db.hrl
@@ -217,7 +217,8 @@
stop = false,
data = <<>>,
ctype = "application/json",
- headers = []
+ headers = [],
+ json = nil
}).
-record(group, {
diff --git a/apps/couch/src/couch_httpd_external.erl b/apps/couch/src/couch_httpd_external.erl
index 2e91fb50..11977390 100644
--- a/apps/couch/src/couch_httpd_external.erl
+++ b/apps/couch/src/couch_httpd_external.erl
@@ -116,17 +116,21 @@ json_query_keys([{<<"key">>, Value} | Rest], Acc) ->
json_query_keys([Term | Rest], Acc) ->
json_query_keys(Rest, [Term|Acc]).
-send_external_response(#httpd{mochi_req=MochiReq}=Req, Response) ->
+send_external_response(Req, Response) ->
#extern_resp_args{
code = Code,
data = Data,
ctype = CType,
- headers = Headers
+ headers = Headers,
+ json = Json
} = parse_external_response(Response),
- couch_httpd:log_request(Req, Code),
- Resp = MochiReq:respond({Code,
- default_or_content_type(CType, Headers ++ couch_httpd:server_header()), Data}),
- {ok, Resp}.
+ Headers1 = default_or_content_type(CType, Headers),
+ case Json of
+ nil ->
+ couch_httpd:send_response(Req, Code, Headers1, Data);
+ Json ->
+ couch_httpd:send_json(Req, Code, Headers1, Json)
+ end.
parse_external_response({Response}) ->
lists:foldl(fun({Key,Value}, Args) ->
@@ -139,7 +143,7 @@ parse_external_response({Response}) ->
Args#extern_resp_args{stop=true};
{<<"json">>, Value} ->
Args#extern_resp_args{
- data=?JSON_ENCODE(Value),
+ json=Value,
ctype="application/json"};
{<<"body">>, Value} ->
Args#extern_resp_args{data=Value, ctype="text/html; charset=utf-8"};