summaryrefslogtreecommitdiff
path: root/src/couchdb
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2008-12-29 23:43:10 +0000
committerJohn Christopher Anderson <jchris@apache.org>2008-12-29 23:43:10 +0000
commit0eaf109d9d2c5bbda4a33d7c106c3a9f06f52d2a (patch)
tree759167072f99606fe082864c1ffe5068dae0e4f3 /src/couchdb
parent46874e0e90a46fefe0a6f7e1ef574bb6a2fcaf78 (diff)
merge form branch to trunk
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@730016 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb')
-rw-r--r--src/couchdb/Makefile.am2
-rw-r--r--src/couchdb/couch_external_manager.erl6
-rw-r--r--src/couchdb/couch_external_server.erl28
-rw-r--r--src/couchdb/couch_httpd_external.erl93
-rw-r--r--src/couchdb/couch_httpd_form.erl114
-rw-r--r--src/couchdb/couch_httpd_misc_handlers.erl4
-rw-r--r--src/couchdb/couch_query_servers.erl13
7 files changed, 202 insertions, 58 deletions
diff --git a/src/couchdb/Makefile.am b/src/couchdb/Makefile.am
index ae7a06e1..50e51c62 100644
--- a/src/couchdb/Makefile.am
+++ b/src/couchdb/Makefile.am
@@ -55,6 +55,7 @@ source_files = \
couch_httpd.erl \
couch_httpd_db.erl \
couch_httpd_external.erl \
+ couch_httpd_form.erl \
couch_httpd_view.erl \
couch_httpd_misc_handlers.erl \
couch_key_tree.erl \
@@ -90,6 +91,7 @@ compiled_files = \
couch_httpd.beam \
couch_httpd_db.beam \
couch_httpd_external.beam \
+ couch_httpd_form.beam \
couch_httpd_view.beam \
couch_httpd_misc_handlers.beam \
couch_key_tree.beam \
diff --git a/src/couchdb/couch_external_manager.erl b/src/couchdb/couch_external_manager.erl
index d86013eb..9bcef57b 100644
--- a/src/couchdb/couch_external_manager.erl
+++ b/src/couchdb/couch_external_manager.erl
@@ -13,7 +13,7 @@
-module(couch_external_manager).
-behaviour(gen_server).
--export([start_link/0, execute/8, config_change/2]).
+-export([start_link/0, execute/2, config_change/2]).
-export([init/1, terminate/2, code_change/3, handle_call/3, handle_cast/2, handle_info/2]).
-include("couch_db.hrl").
@@ -21,13 +21,13 @@
start_link() ->
gen_server:start_link({local, couch_external_manager}, couch_external_manager, [], []).
-execute(UrlName, Db, Verb, Path, Query, Body, Post, Cookie) ->
+execute(UrlName, JsonReq) ->
Pid = gen_server:call(couch_external_manager, {get, UrlName}),
case Pid of
{error, Reason} ->
Reason;
_ ->
- couch_external_server:execute(Pid, Db, Verb, Path, Query, Body, Post, Cookie)
+ couch_external_server:execute(Pid, JsonReq)
end.
config_change("external", UrlName) ->
diff --git a/src/couchdb/couch_external_server.erl b/src/couchdb/couch_external_server.erl
index afbb729e..49a31c06 100644
--- a/src/couchdb/couch_external_server.erl
+++ b/src/couchdb/couch_external_server.erl
@@ -13,7 +13,7 @@
-module(couch_external_server).
-behaviour(gen_server).
--export([start_link/2, stop/1, execute/8]).
+-export([start_link/2, stop/1, execute/2]).
-export([init/1, terminate/2, handle_call/3, handle_cast/2, handle_info/2, code_change/3]).
-define(TIMEOUT, 5000).
@@ -28,8 +28,8 @@ start_link(Name, Command) ->
stop(Pid) ->
gen_server:cast(Pid, stop).
-execute(Pid, Db, Verb, Path, Query, Body, Post, Cookie) ->
- gen_server:call(Pid, {execute, Db, Verb, Path, Query, Body, Post, Cookie}).
+execute(Pid, JsonReq) ->
+ gen_server:call(Pid, {execute, JsonReq}).
% Gen Server Handlers
@@ -43,18 +43,8 @@ terminate(_Reason, {Name, _Command, Pid}) ->
couch_os_process:stop(Pid),
ok.
-handle_call({execute, Db, Verb, Path, Query, Body, Post, Cookie}, _From, {Name, Command, Pid}) ->
- ?LOG_DEBUG("Query Params ~p",[Query]),
- {ok, Info} = couch_db:get_db_info(Db),
- Json = {[
- {<<"info">>, {Info}},
- {<<"verb">>, Verb},
- {<<"path">>, Path},
- {<<"query">>, to_json_terms(Query)},
- {<<"body">>, Body},
- {<<"form">>, to_json_terms(Post)},
- {<<"cookie">>, to_json_terms(Cookie)}]},
- {reply, couch_os_process:prompt(Pid, Json), {Name, Command, Pid}}.
+handle_call({execute, JsonReq}, _From, {Name, Command, Pid}) ->
+ {reply, couch_os_process:prompt(Pid, JsonReq), {Name, Command, Pid}}.
handle_info({'EXIT', Pid, Reason}, {Name, Command, Pid}) ->
?LOG_INFO("EXTERNAL: Restarting process for ~s (reason: ~w)", [Name, Reason]),
@@ -69,12 +59,4 @@ handle_cast(_Whatever, State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-% Internal API
-
-to_json_terms(Data) ->
- to_json_terms(Data, []).
-to_json_terms([], Acc) ->
- {lists:reverse(Acc)};
-to_json_terms([{Key, Value} | Rest], Acc) ->
- to_json_terms(Rest, [{list_to_binary(Key), list_to_binary(Value)} | Acc]).
diff --git a/src/couchdb/couch_httpd_external.erl b/src/couchdb/couch_httpd_external.erl
index 84f1c444..7a682ddd 100644
--- a/src/couchdb/couch_httpd_external.erl
+++ b/src/couchdb/couch_httpd_external.erl
@@ -13,6 +13,7 @@
-module(couch_httpd_external).
-export([handle_external_req/2, handle_external_req/3]).
+-export([send_external_response/2, json_req_obj/2]).
-import(couch_httpd,[send_error/4]).
@@ -25,9 +26,44 @@
headers = []
}).
-process_external_req(#httpd{mochi_req=Req,
- method=Verb
- }=HttpReq, Db, Name, Path) ->
+% handle_external_req/2
+% for the old type of config usage:
+% _external = {couch_httpd_external, handle_external_req}
+% with urls like
+% /db/_external/action/design/name
+handle_external_req(#httpd{
+ path_parts=[_DbName, _External, UrlName | _Path]
+ }=HttpReq, Db) ->
+ process_external_req(HttpReq, Db, UrlName);
+handle_external_req(#httpd{path_parts=[_, _]}=Req, _Db) ->
+ send_error(Req, 404, <<"external_server_error">>, <<"No server name specified.">>);
+handle_external_req(Req, _) ->
+ send_error(Req, 404, <<"external_server_error">>, <<"Broken assumption">>).
+
+% handle_external_req/3
+% for this type of config usage:
+% _action = {couch_httpd_external, handle_external_req, <<"action">>}
+% with urls like
+% /db/_action/design/name
+handle_external_req(HttpReq, Db, Name) ->
+ process_external_req(HttpReq, Db, Name).
+
+process_external_req(HttpReq, Db, Name) ->
+
+ Response = couch_external_manager:execute(binary_to_list(Name),
+ json_req_obj(HttpReq, Db)),
+
+ case Response of
+ {unknown_external_server, Msg} ->
+ send_error(HttpReq, 404, <<"external_server_error">>, Msg);
+ _ ->
+ send_external_response(HttpReq, Response)
+ end.
+
+json_req_obj(#httpd{mochi_req=Req,
+ method=Verb,
+ path_parts=Path
+ }, Db) ->
ReqBody = Req:recv_body(),
ParsedForm = case Req:get_primary_header_value("content-type") of
"application/x-www-form-urlencoded" ++ _ ->
@@ -35,40 +71,37 @@ process_external_req(#httpd{mochi_req=Req,
_ ->
[]
end,
- Response = couch_external_manager:execute(binary_to_list(Name),
- Db, Verb, Path, Req:parse_qs(), ReqBody, ParsedForm,
- Req:parse_cookie()),
-
- case Response of
- {unknown_external_server, Msg} ->
- send_error(HttpReq, 404, <<"external_server_error">>, Msg);
- _ ->
- send_external_response(Req, Response)
- end.
+ Headers = Req:get(headers),
+ Hlist = mochiweb_headers:to_list(Headers),
+ {ok, Info} = couch_db:get_db_info(Db),
+ % add headers...
+ {[{<<"info">>, {Info}},
+ {<<"verb">>, Verb},
+ {<<"path">>, Path},
+ {<<"query">>, to_json_terms(Req:parse_qs())},
+ {<<"headers">>, to_json_terms(Hlist)},
+ {<<"body">>, ReqBody},
+ {<<"form">>, to_json_terms(ParsedForm)},
+ {<<"cookie">>, to_json_terms(Req:parse_cookie())}]}.
-handle_external_req(#httpd{
- path_parts=[_DbName, _External, UrlName | Path]
- }=HttpReq, Db) ->
- process_external_req(HttpReq, Db, UrlName, Path);
-handle_external_req(#httpd{path_parts=[_, _]}=Req, _Db) ->
- send_error(Req, 404, <<"external_server_error">>, <<"No server name specified.">>);
-handle_external_req(Req, _) ->
- send_error(Req, 404, <<"external_server_error">>, <<"Broken assumption">>).
+to_json_terms(Data) ->
+ to_json_terms(Data, []).
+to_json_terms([], Acc) ->
+ {lists:reverse(Acc)};
+to_json_terms([{Key, Value} | Rest], Acc) when is_atom(Key) ->
+ to_json_terms(Rest, [{list_to_binary(atom_to_list(Key)), list_to_binary(Value)} | Acc]);
+to_json_terms([{Key, Value} | Rest], Acc) ->
+ to_json_terms(Rest, [{list_to_binary(Key), list_to_binary(Value)} | Acc]).
-handle_external_req(#httpd{
- path_parts=[_DbName, _External | Path]
- }=HttpReq, Db, Name) ->
- process_external_req(HttpReq, Db, Name, Path).
-send_external_response(Req, Response) ->
+send_external_response(#httpd{mochi_req=MochiReq}, Response) ->
#extern_resp_args{
code = Code,
data = Data,
ctype = CType,
headers = Headers
} = parse_external_response(Response),
- ?LOG_DEBUG("External Response ~p",[Response]),
- Resp = Req:respond({Code,
+ Resp = MochiReq:respond({Code,
default_or_content_type(CType, Headers), chunked}),
Resp:write_chunk(Data),
Resp:write_chunk(""),
@@ -87,13 +120,15 @@ parse_external_response({Response}) ->
ctype="application/json"};
{<<"body">>, Value} ->
Args#extern_resp_args{data=Value, ctype="text/html"};
+ {<<"base64">>, Value} ->
+ Args#extern_resp_args{data=couch_util:decodeBase64(Value), ctype="application/binary"};
{<<"headers">>, {Headers}} ->
NewHeaders = lists:map(fun({Header, HVal}) ->
{binary_to_list(Header), binary_to_list(HVal)}
end, Headers),
Args#extern_resp_args{headers=NewHeaders};
_ -> % unknown key
- Msg = lists:flatten(io_lib:format("Invalid data from external server: ~s = ~p", [Key, Value])),
+ Msg = lists:flatten(io_lib:format("Invalid data from external server: ~p", [{Key, Value}])),
throw({external_response_error, Msg})
end
end, #extern_resp_args{}, Response).
diff --git a/src/couchdb/couch_httpd_form.erl b/src/couchdb/couch_httpd_form.erl
new file mode 100644
index 00000000..f4fa2c18
--- /dev/null
+++ b/src/couchdb/couch_httpd_form.erl
@@ -0,0 +1,114 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License. You may obtain a copy of
+% the License at
+%
+% http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_httpd_form).
+
+-export([handle_form_req/2]).
+
+
+-include("couch_db.hrl").
+
+-import(couch_httpd,
+ [send_json/2,send_json/3,send_json/4,send_method_not_allowed/2,
+ start_json_response/2,send_chunk/2,end_json_response/1,
+ start_chunked_response/3, send_error/4]).
+
+handle_form_req(#httpd{method='GET',path_parts=[_, _, DesignName, FormName, Docid]}=Req, Db) ->
+ DesignId = <<"_design/", DesignName/binary>>,
+ % Anyway we can dry up this error handling?
+ case (catch couch_httpd_db:couch_doc_open(Db, DesignId, [], [])) of
+ {not_found, missing} ->
+ throw({not_found, missing_design_doc});
+ {not_found, deleted} ->
+ throw({not_found, deleted_design_doc});
+ DesignDoc ->
+ #doc{body={Props}} = DesignDoc,
+ Lang = proplists:get_value(<<"language">>, Props, <<"javascript">>),
+ case proplists:get_value(<<"forms">>, Props, nil) of
+ {Forms} ->
+ case proplists:get_value(FormName, Forms, nil) of
+ nil ->
+ throw({not_found, missing_form});
+ FormSrc ->
+ case (catch couch_httpd_db:couch_doc_open(Db, Docid, [], [])) of
+ {not_found, missing} ->
+ throw({not_found, missing});
+ {not_found, deleted} ->
+ throw({not_found, deleted});
+ Doc ->
+ % ok we have everythign we need. let's make it happen.
+ send_form_response(Lang, FormSrc, Doc, Req, Db)
+ end
+ end;
+ nil ->
+ throw({not_found, missing_form})
+ end
+ end;
+
+handle_form_req(#httpd{method='GET'}=Req, _Db) ->
+ send_error(Req, 404, <<"form_error">>, <<"Invalid path.">>);
+
+handle_form_req(Req, _Db) ->
+ send_method_not_allowed(Req, "GET,HEAD").
+
+
+send_form_response(Lang, FormSrc, #doc{revs=[DocRev|_]}=Doc, #httpd{mochi_req=MReq}=Req, Db) ->
+ % make a term with etag-effecting Req components, but not always changing ones.
+ Headers = MReq:get(headers),
+ Hlist = mochiweb_headers:to_list(Headers),
+ Accept = proplists:get_value('Accept', Hlist),
+ <<SigInt:128/integer>> = erlang:md5(term_to_binary({Lang, FormSrc, DocRev, Accept})),
+ CurrentEtag = list_to_binary("\"" ++ lists:flatten(io_lib:format("form_~.36B",[SigInt])) ++ "\""),
+ EtagsToMatch = string:tokens(
+ couch_httpd:header_value(Req, "If-None-Match", ""), ", "),
+ % We know our etag now
+ case lists:member(binary_to_list(CurrentEtag), EtagsToMatch) of
+ true ->
+ % the client has this in their cache.
+ couch_httpd:send_response(Req, 304, [{"Etag", CurrentEtag}], <<>>);
+ false ->
+ % Run the external form renderer.
+ {JsonResponse} = couch_query_servers:render_doc_form(Lang, FormSrc, Doc, Req, Db),
+ % Here we embark on the delicate task of replacing or creating the
+ % headers on the JsonResponse object. We need to control the Etag and
+ % Vary headers. If the external function controls the Etag, we'd have to
+ % run it to check for a match, which sort of defeats the purpose.
+ JsonResponse2 = case proplists:get_value(<<"headers">>, JsonResponse, nil) of
+ nil ->
+ % no JSON headers
+ % add our Etag and Vary headers to the response
+ [{<<"headers">>, {[{<<"Etag">>, CurrentEtag}, {<<"Vary">>, <<"Accept">>}]}} | JsonResponse];
+ {JsonHeaders} ->
+ [case Field of
+ {<<"headers">>, {JsonHeaders}} -> % add our headers
+ JsonHeadersEtagged = set_or_replace_header({<<"Etag">>, CurrentEtag}, JsonHeaders),
+ JsonHeadersVaried = set_or_replace_header({<<"Vary">>, <<"Accept">>}, JsonHeadersEtagged),
+ {<<"headers">>, {JsonHeadersVaried}};
+ _ -> % skip non-header fields
+ Field
+ end || Field <- JsonResponse]
+ end,
+ couch_httpd_external:send_external_response(Req, {JsonResponse2})
+ end.
+
+set_or_replace_header(H, L) ->
+ set_or_replace_header(H, L, []).
+
+set_or_replace_header({Key, NewValue}, [{Key, _OldVal} | Headers], Acc) ->
+ % drop matching keys
+ set_or_replace_header({Key, NewValue}, Headers, Acc);
+set_or_replace_header({Key, NewValue}, [{OtherKey, OtherVal} | Headers], Acc) ->
+ % something else is next, leave it alone.
+ set_or_replace_header({Key, NewValue}, Headers, [{OtherKey, OtherVal} | Acc]);
+set_or_replace_header({Key, NewValue}, [], Acc) ->
+ % end of list, add ours
+ [{Key, NewValue}|Acc]. \ No newline at end of file
diff --git a/src/couchdb/couch_httpd_misc_handlers.erl b/src/couchdb/couch_httpd_misc_handlers.erl
index 9651436c..c9a56c5c 100644
--- a/src/couchdb/couch_httpd_misc_handlers.erl
+++ b/src/couchdb/couch_httpd_misc_handlers.erl
@@ -24,7 +24,7 @@
-import(couch_httpd,
[send_json/2,send_json/3,send_json/4,send_method_not_allowed/2,
start_json_response/2,send_chunk/2,end_json_response/1,
- start_chunked_response/3]).
+ start_chunked_response/3, send_error/4]).
% httpd global handlers
@@ -173,5 +173,5 @@ increment_update_seq_req(#httpd{method='POST'}=Req, Db) ->
{update_seq, NewSeq}
]});
increment_update_seq_req(Req, _Db) ->
- send_method_not_allowed(Req, "GET,PUT,DELETE").
+ send_method_not_allowed(Req, "POST").
diff --git a/src/couchdb/couch_query_servers.erl b/src/couchdb/couch_query_servers.erl
index b694d5e3..c4ed5c8b 100644
--- a/src/couchdb/couch_query_servers.erl
+++ b/src/couchdb/couch_query_servers.erl
@@ -17,7 +17,7 @@
-export([init/1, terminate/2, handle_call/3, handle_cast/2, handle_info/2,code_change/3,stop/0]).
-export([start_doc_map/2, map_docs/2, stop_doc_map/1]).
--export([reduce/3, rereduce/3,validate_doc_update/5]).
+-export([reduce/3, rereduce/3,validate_doc_update/5,render_doc_form/5]).
% -export([test/0]).
-include("couch_db.hrl").
@@ -122,6 +122,17 @@ validate_doc_update(Lang, FunSrc, EditDoc, DiskDoc, Ctx) ->
ok = ret_os_process(Lang, Pid)
end.
+render_doc_form(Lang, FormSrc, Doc, Req, Db) ->
+ Pid = get_os_process(Lang),
+ JsonDoc = couch_doc:to_json_obj(Doc, [revs]),
+ JsonReq = couch_httpd_external:json_req_obj(Req, Db),
+ try couch_os_process:prompt(Pid, [<<"form">>, FormSrc, JsonDoc, JsonReq]) of
+ FormResp ->
+ FormResp
+ after
+ ok = ret_os_process(Lang, Pid)
+ end.
+
init([]) ->
% read config and register for configuration changes