You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2015/08/12 17:54:32 UTC
[08/11] chttpd commit: updated refs/heads/master to 238b8c4
Add chttpd_plugin hooks
We add following hooks:
- chttpd_plugin:before_request/1
- chttpd_plugin:after_request/2
- chttpd_plugin:handle_error/1
Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/64aa2fd0
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/64aa2fd0
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/64aa2fd0
Branch: refs/heads/master
Commit: 64aa2fd0933f1ac8beb3584441663b20cf6e3179
Parents: 2b42759
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Authored: Wed Jul 29 11:31:28 2015 -0700
Committer: ILYA Khlopotov <ii...@ca.ibm.com>
Committed: Tue Aug 11 09:14:29 2015 -0700
----------------------------------------------------------------------
src/chttpd.erl | 32 +++++++++++++++++++++++--------
src/chttpd_plugin.erl | 48 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/64aa2fd0/src/chttpd.erl
----------------------------------------------------------------------
diff --git a/src/chttpd.erl b/src/chttpd.erl
index 6a80f05..ec15033 100644
--- a/src/chttpd.erl
+++ b/src/chttpd.erl
@@ -193,7 +193,7 @@ handle_request_int(MochiReq) ->
Nonce = couch_util:to_hex(crypto:rand_bytes(5)),
- HttpReq = #httpd{
+ HttpReq0 = #httpd{
mochi_req = MochiReq,
begin_ts = Begin,
peer = Peer,
@@ -204,6 +204,8 @@ handle_request_int(MochiReq) ->
|| Part <- string:tokens(Path, "/")]
},
+ {ok, HttpReq} = chttpd_plugin:before_request(HttpReq0),
+
HandlerKey =
case HttpReq#httpd.path_parts of
[] -> <<>>;
@@ -229,7 +231,7 @@ handle_request_int(MochiReq) ->
HandlerKey, fun chttpd_db:handle_request/1),
AuthorizedReq = chttpd_auth:authorize(possibly_hack(Req),
fun chttpd_auth_request:authorize_request/1),
- HandlerFun(AuthorizedReq);
+ {AuthorizedReq, HandlerFun(AuthorizedReq)};
Response ->
Response
end;
@@ -268,11 +270,12 @@ handle_request_int(MochiReq) ->
end,
{HttpReq1, HttpResp0} = result(Result0, HttpReq),
+ {ok, HttpResp1} = chttpd_plugin:after_request(HttpReq1, HttpResp0),
- HttpResp1 = update_stats(HttpReq1, HttpResp0),
- maybe_log(HttpReq1, HttpResp1),
+ HttpResp2 = update_stats(HttpReq1, HttpResp1),
+ maybe_log(HttpReq1, HttpResp2),
- case HttpResp1 of
+ case HttpResp2 of
#httpd_resp{status = ok, response = Resp} ->
{ok, Resp};
#httpd_resp{status = aborted, reason = Reason} ->
@@ -280,6 +283,8 @@ handle_request_int(MochiReq) ->
exit(normal)
end.
+result({#httpd{} = Req, Result}, _) ->
+ result(Result, Req);
result(Result, #httpd{nonce = Nonce} = Req) ->
{Status, Code, Reason} = case Result of
{ok, #delayed_resp{resp=Resp}} ->
@@ -817,14 +822,25 @@ error_info({timeout, _Reason}) ->
error_info(timeout);
error_info({Error, null}) ->
error_info(Error);
-error_info({Error, Reason}) ->
- {500, couch_util:to_binary(Error), couch_util:to_binary(Reason)};
+error_info({_Error, _Reason} = Error) ->
+ maybe_handle_error(Error);
error_info({Error, nil, _Stack}) ->
error_info(Error);
error_info({Error, Reason, _Stack}) ->
{500, couch_util:to_binary(Error), couch_util:to_binary(Reason)};
error_info(Error) ->
- {500, <<"unknown_error">>, couch_util:to_binary(Error)}.
+ maybe_handle_error({<<"unknown_error">>, Error}).
+
+maybe_handle_error(Error) ->
+ case chttpd_plugin:handle_error(Error) of
+ {_Code, _Reason, _Description} = Result ->
+ Result;
+ {Err, Reason} ->
+ {500, couch_util:to_binary(Err), couch_util:to_binary(Reason)};
+ Error ->
+ {500, couch_util:to_binary(Error), null}
+ end.
+
error_headers(#httpd{mochi_req=MochiReq}=Req, 401=Code, ErrorStr, ReasonStr) ->
% this is where the basic auth popup is triggered
http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/64aa2fd0/src/chttpd_plugin.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_plugin.erl b/src/chttpd_plugin.erl
new file mode 100644
index 0000000..e174a68
--- /dev/null
+++ b/src/chttpd_plugin.erl
@@ -0,0 +1,48 @@
+% 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(chttpd_plugin).
+
+-export([
+ before_request/1,
+ after_request/2,
+ handle_error/1
+]).
+
+-include_lib("couch/include/couch_db.hrl").
+
+%% ------------------------------------------------------------------
+%% API Function Definitions
+%% ------------------------------------------------------------------
+
+before_request(HttpReq) ->
+ [HttpReq1] = with_pipe(before_request, [HttpReq]),
+ {ok, HttpReq1}.
+
+after_request(HttpReq, Result) ->
+ [_, Result1] = with_pipe(after_request, [HttpReq, Result]),
+ {ok, Result1}.
+
+handle_error(Error) ->
+ [Error1] = with_pipe(after_request, [Error]),
+ Error1.
+
+%% ------------------------------------------------------------------
+%% Internal Function Definitions
+%% ------------------------------------------------------------------
+
+with_pipe(Func, Args) ->
+ do_apply(Func, Args, [ignore_providers, pipe]).
+
+do_apply(Func, Args, Opts) ->
+ Handle = couch_epi:get_handle(chttpd),
+ couch_epi:apply(Handle, chttpd, Func, Args, Opts).