From 86bb26326e0bcc09e058d56d67dd124c5e3ecee5 Mon Sep 17 00:00:00 2001 From: Adam Kocoloski Date: Tue, 18 Aug 2009 19:47:41 +0000 Subject: cleanup application of auth functions git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@805551 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_httpd.erl | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 61bdc177..8da157b3 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -173,20 +173,11 @@ handle_request(MochiReq, DefaultFun, {ok, Resp} = try - % Try authentication handlers in order until one returns a result - case lists:foldl(fun(_Fun, #httpd{user_ctx=#user_ctx{}}=Req) -> Req; - (Fun, #httpd{}=Req) -> Fun(Req); - (_Fun, Response) -> Response - end, HttpReq, AuthenticationFuns) of - #httpd{user_ctx=#user_ctx{}}=Req -> HandlerFun(Req); - #httpd{}=Req -> - case couch_config:get("couch_httpd_auth", "require_valid_user", "false") of - "true" -> - throw({unauthorized, <<"Authentication required.">>}); - _ -> - HandlerFun(Req#httpd{user_ctx=#user_ctx{}}) - end; - Response -> Response + case authenticate_request(HttpReq, AuthenticationFuns) of + Req when is_record(Req, httpd) -> + HandlerFun(Req); + Response -> + Response end catch throw:Error -> @@ -218,6 +209,21 @@ handle_request(MochiReq, DefaultFun, couch_stats_collector:increment({httpd, requests}), {ok, Resp}. +% Try authentication handlers in order until one returns a result +authenticate_request(#httpd{user_ctx=#user_ctx{}} = Req, _AuthFuns) -> + Req; +authenticate_request(#httpd{} = Req, []) -> + case couch_config:get("couch_httpd_auth", "require_valid_user", "false") of + "true" -> + throw({unauthorized, <<"Authentication required.">>}); + "false" -> + Req#httpd{user_ctx=#user_ctx{}} + end; +authenticate_request(#httpd{} = Req, [AuthFun|Rest]) -> + authenticate_request(AuthFun(Req), Rest); +authenticate_request(Response, _AuthFuns) -> + Response. + increment_method_stats(Method) -> couch_stats_collector:increment({httpd_request_methods, Method}). -- cgit v1.2.3