Support jsonp in externals

Allow ?callback= for any external that returns json (i.e,
uses "json":{} instead of "data":"data".

BugzID: 12748


Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/f40f349b
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/f40f349b
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/f40f349b

Branch: refs/heads/import
Commit: f40f349bceef8a305ceab61116297815e1f3b86e
Parents: bfa73ab
Author: Robert Newson <[email protected]>
Authored: Tue Oct 18 15:21:53 2011 +0100
Committer: Robert Newson <[email protected]>
Committed: Wed Nov 14 15:00:46 2012 +0000

----------------------------------------------------------------------
 src/chttpd_external.erl | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/f40f349b/src/chttpd_external.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_external.erl b/src/chttpd_external.erl
index 9795d2d..e24d216 100644
--- a/src/chttpd_external.erl
+++ b/src/chttpd_external.erl
@@ -116,16 +116,21 @@ json_query_keys([{<<"descending">>, Value} | Rest], Acc) 
->
 json_query_keys([Term | Rest], Acc) ->
     json_query_keys(Rest, [Term|Acc]).
 
-send_external_response(#httpd{mochi_req=MochiReq}, 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),
-    Resp = MochiReq:respond({Code,
-        default_or_content_type(CType, Headers ++ chttpd: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) ->
@@ -138,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"};

Reply via email to