Permalink
Browse files

support JSONP in externals

COUCHDB-1313
  • Loading branch information...
1 parent a2b2828 commit 52444eba41898c5a8116a04350f27c47f900d1d7 @rnewson rnewson committed with benoitc Oct 20, 2011
Showing with 13 additions and 8 deletions.
  1. +2 −1 apps/couch/include/couch_db.hrl
  2. +11 −7 apps/couch_httpd/src/couch_httpd_external.erl
View
3 apps/couch/include/couch_db.hrl
@@ -207,7 +207,8 @@
stop = false,
data = <<>>,
ctype = "application/json",
- headers = []
+ headers = [],
+ json = nil
}).
-record(index_header,
View
18 apps/couch_httpd/src/couch_httpd_external.erl
@@ -122,17 +122,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) ->
@@ -145,7 +149,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"};

0 comments on commit 52444eb

Please sign in to comment.