Skip to content

Instantly share code, notes, and snippets.

@daleharvey
Created October 14, 2010 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daleharvey/627022 to your computer and use it in GitHub Desktop.
Save daleharvey/627022 to your computer and use it in GitHub Desktop.
diff --git a/src/couchdb/couch_db.erl b/src/couchdb/couch_db.erl
index 36b61a7..c411ede 100644
--- a/src/couchdb/couch_db.erl
+++ b/src/couchdb/couch_db.erl
@@ -13,7 +13,7 @@
-module(couch_db).
-behaviour(gen_server).
--export([open/2,open_int/2,close/1,create/2,start_compact/1,get_db_info/1,get_design_docs/1]).
+-export([open/2,open/3,open_int/2,close/1,create/2,start_compact/1,get_db_info/1,get_design_docs/1]).
-export([open_ref_counted/2,is_idle/1,monitor/1,count_changes_since/2]).
-export([update_doc/3,update_doc/4,update_docs/4,update_docs/2,update_docs/3,delete_doc/3]).
-export([get_doc_info/2,open_doc/2,open_doc/3,open_doc_revs/4]).
@@ -73,10 +73,16 @@ open_int(DbName, Options) ->
% this should be called anytime an http request opens the database.
% it ensures that the http userCtx is a valid reader
open(DbName, Options) ->
- case couch_server:open(DbName, Options) of
+ open(DbName, Options, false).
+
+open(DbName, Options, SkipCheck) ->
+ case couch_server:open(DbName, Options) of
{ok, Db} ->
try
- check_is_reader(Db),
+ case SkipCheck of
+ false -> check_is_reader(Db);
+ true -> ok
+ end,
{ok, Db}
catch
throw:Error ->
diff --git a/src/couchdb/couch_httpd_db.erl b/src/couchdb/couch_httpd_db.erl
index 51f1f55..4590c76 100644
--- a/src/couchdb/couch_httpd_db.erl
+++ b/src/couchdb/couch_httpd_db.erl
@@ -212,17 +212,34 @@ delete_db_req(#httpd{user_ctx=UserCtx}=Req, DbName) ->
end.
do_db_req(#httpd{user_ctx=UserCtx,path_parts=[DbName|_]}=Req, Fun) ->
- case couch_db:open(DbName, [{user_ctx, UserCtx}]) of
- {ok, Db} ->
- try
- Fun(Req, Db)
- after
- catch couch_db:close(Db)
- end;
- Error ->
- throw(Error)
+ Anon = couch_config:get("couch_httpd_auth", "anonymous_design_doc", false),
+ SkipCheck = to_bool(AnonDesign) andalso
+ not requires_auth(Req#httpd.path_parts),
+ case couch_db:open(DbName, [{user_ctx, UserCtx}], SkipCheck) of
+ {ok, Db} ->
+ try
+ Fun(Req, Db)
+ after
+ catch couch_db:close(Db)
+ end;
+ Error ->
+ throw(Error)
end.
+requires_auth(Path) ->
+ case Path of
+ [_DbName, <<"_design">>, _Name, <<"_view">>|_] -> true;
+ [_DbName, <<"_design">>, _Name, <<"_show">>|_] -> true;
+ [_DbName, <<"_design">>, _Name, <<"_list">>|_] -> true;
+ [_DbName, <<"_design">>|_] -> false;
+ _ -> true
+ end.
+
+to_bool("true") ->
+ true;
+to_bool(_Else) ->
+ false.
+
db_req(#httpd{method='GET',path_parts=[_DbName]}=Req, Db) ->
{ok, DbInfo} = couch_db:get_db_info(Db),
send_json(Req, {DbInfo});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment