Skip to content

Commit

Permalink
support JSONP in externals
Browse files Browse the repository at this point in the history
COUCHDB-1313
  • Loading branch information
rnewson authored and benoitc committed Feb 9, 2013
1 parent a2b2828 commit 52444eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 2 additions & 1 deletion apps/couch/include/couch_db.hrl
Expand Up @@ -207,7 +207,8 @@
stop = false, stop = false,
data = <<>>, data = <<>>,
ctype = "application/json", ctype = "application/json",
headers = [] headers = [],
json = nil
}). }).


-record(index_header, -record(index_header,
Expand Down
18 changes: 11 additions & 7 deletions apps/couch_httpd/src/couch_httpd_external.erl
Expand Up @@ -122,17 +122,21 @@ json_query_keys([{<<"key">>, Value} | Rest], Acc) ->
json_query_keys([Term | Rest], Acc) -> json_query_keys([Term | Rest], Acc) ->
json_query_keys(Rest, [Term|Acc]). json_query_keys(Rest, [Term|Acc]).


send_external_response(#httpd{mochi_req=MochiReq}=Req, Response) -> send_external_response(Req, Response) ->
#extern_resp_args{ #extern_resp_args{
code = Code, code = Code,
data = Data, data = Data,
ctype = CType, ctype = CType,
headers = Headers headers = Headers,
json = Json
} = parse_external_response(Response), } = parse_external_response(Response),
couch_httpd:log_request(Req, Code), Headers1 = default_or_content_type(CType, Headers),
Resp = MochiReq:respond({Code, case Json of
default_or_content_type(CType, Headers ++ couch_httpd:server_header()), Data}), nil ->
{ok, Resp}. couch_httpd:send_response(Req, Code, Headers1, Data);
Json ->
couch_httpd:send_json(Req, Code, Headers1, Json)
end.


parse_external_response({Response}) -> parse_external_response({Response}) ->
lists:foldl(fun({Key,Value}, Args) -> lists:foldl(fun({Key,Value}, Args) ->
Expand All @@ -145,7 +149,7 @@ parse_external_response({Response}) ->
Args#extern_resp_args{stop=true}; Args#extern_resp_args{stop=true};
{<<"json">>, Value} -> {<<"json">>, Value} ->
Args#extern_resp_args{ Args#extern_resp_args{
data=?JSON_ENCODE(Value), json=Value,
ctype="application/json"}; ctype="application/json"};
{<<"body">>, Value} -> {<<"body">>, Value} ->
Args#extern_resp_args{data=Value, ctype="text/html; charset=utf-8"}; Args#extern_resp_args{data=Value, ctype="text/html; charset=utf-8"};
Expand Down

0 comments on commit 52444eb

Please sign in to comment.