Author: fdmanana Date: Tue Dec 7 20:26:32 2010 New Revision: 1043187 URL: http://svn.apache.org/viewvc?rev=1043187&view=rev Log: Merged revision 1043186 from trunk:
Replicator improvement: send "unauthorized" error message instead of "db_not_found" when a remote endpoint can not be accessed due to authorization. Modified: couchdb/branches/1.0.x/share/www/script/test/replication.js couchdb/branches/1.0.x/src/couchdb/couch_httpd_misc_handlers.erl couchdb/branches/1.0.x/src/couchdb/couch_rep.erl couchdb/branches/1.0.x/src/couchdb/couch_rep_httpc.erl Modified: couchdb/branches/1.0.x/share/www/script/test/replication.js URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/share/www/script/test/replication.js?rev=1043187&r1=1043186&r2=1043187&view=diff ============================================================================== --- couchdb/branches/1.0.x/share/www/script/test/replication.js (original) +++ couchdb/branches/1.0.x/share/www/script/test/replication.js Tue Dec 7 20:26:32 2010 @@ -667,7 +667,7 @@ couchTests.replication = function(debug) ); T(false, "replication should have failed"); } catch(x) { - T(x.error === "db_not_found"); + T(x.error === "unauthorized"); } atts_ddoc_copy = dbB.open(atts_ddoc._id); Modified: couchdb/branches/1.0.x/src/couchdb/couch_httpd_misc_handlers.erl URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/src/couchdb/couch_httpd_misc_handlers.erl?rev=1043187&r1=1043186&r2=1043187&view=diff ============================================================================== --- couchdb/branches/1.0.x/src/couchdb/couch_httpd_misc_handlers.erl (original) +++ couchdb/branches/1.0.x/src/couchdb/couch_httpd_misc_handlers.erl Tue Dec 7 20:26:32 2010 @@ -101,7 +101,9 @@ handle_replicate_req(#httpd{method='POST end catch throw:{db_not_found, Msg} -> - send_json(Req, 404, {[{error, db_not_found}, {reason, Msg}]}) + send_json(Req, 404, {[{error, db_not_found}, {reason, Msg}]}); + throw:{unauthorized, Msg} -> + send_json(Req, 404, {[{error, unauthorized}, {reason, Msg}]}) end; handle_replicate_req(Req) -> send_method_not_allowed(Req, "POST"). Modified: couchdb/branches/1.0.x/src/couchdb/couch_rep.erl URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/src/couchdb/couch_rep.erl?rev=1043187&r1=1043186&r2=1043187&view=diff ============================================================================== --- couchdb/branches/1.0.x/src/couchdb/couch_rep.erl (original) +++ couchdb/branches/1.0.x/src/couchdb/couch_rep.erl Tue Dec 7 20:26:32 2010 @@ -108,8 +108,12 @@ get_result(Server, PostBody, UserCtx) -> end. init(InitArgs) -> - try do_init(InitArgs) - catch throw:{db_not_found, DbUrl} -> {stop, {db_not_found, DbUrl}} end. + try + do_init(InitArgs) + catch + throw:Error -> + {stop, Error} + end. do_init([RepId, {PostProps}, UserCtx] = InitArgs) -> process_flag(trap_exit, true), @@ -314,13 +318,19 @@ start_replication_server(Replicator) -> ?LOG_DEBUG("replication ~p already running at ~p", [RepId, Pid]), Pid; {error, {db_not_found, DbUrl}} -> - throw({db_not_found, <<"could not open ", DbUrl/binary>>}) + throw({db_not_found, <<"could not open ", DbUrl/binary>>}); + {error, {unauthorized, DbUrl}} -> + throw({unauthorized, + <<"unauthorized to access database ", DbUrl/binary>>}) end; {error, {already_started, Pid}} -> ?LOG_DEBUG("replication ~p already running at ~p", [RepId, Pid]), Pid; {error, {{db_not_found, DbUrl}, _}} -> - throw({db_not_found, <<"could not open ", DbUrl/binary>>}) + throw({db_not_found, <<"could not open ", DbUrl/binary>>}); + {error, {{unauthorized, DbUrl}, _}} -> + throw({unauthorized, + <<"unauthorized to access database ", DbUrl/binary>>}) end. compare_replication_logs(SrcDoc, TgtDoc) -> @@ -561,18 +571,24 @@ open_db(<<"http://",_/binary>>=Url, _, P open_db(<<"https://",_/binary>>=Url, _, ProxyParams, CreateTarget) -> open_db({[{<<"url">>,Url}]}, [], ProxyParams, CreateTarget); open_db(<<DbName/binary>>, UserCtx, _ProxyParams, CreateTarget) -> - case CreateTarget of - true -> - ok = couch_httpd:verify_is_server_admin(UserCtx), - couch_server:create(DbName, [{user_ctx, UserCtx}]); - false -> ok - end, + try + case CreateTarget of + true -> + ok = couch_httpd:verify_is_server_admin(UserCtx), + couch_server:create(DbName, [{user_ctx, UserCtx}]); + false -> + ok + end, - case couch_db:open(DbName, [{user_ctx, UserCtx}]) of - {ok, Db} -> - couch_db:monitor(Db), - Db; - {not_found, no_db_file} -> throw({db_not_found, DbName}) + case couch_db:open(DbName, [{user_ctx, UserCtx}]) of + {ok, Db} -> + couch_db:monitor(Db), + Db; + {not_found, no_db_file} -> + throw({db_not_found, DbName}) + end + catch throw:{unauthorized, _} -> + throw({unauthorized, DbName}) end. schedule_checkpoint(#state{checkpoint_scheduled = nil} = State) -> Modified: couchdb/branches/1.0.x/src/couchdb/couch_rep_httpc.erl URL: http://svn.apache.org/viewvc/couchdb/branches/1.0.x/src/couchdb/couch_rep_httpc.erl?rev=1043187&r1=1043186&r2=1043187&view=diff ============================================================================== --- couchdb/branches/1.0.x/src/couchdb/couch_rep_httpc.erl (original) +++ couchdb/branches/1.0.x/src/couchdb/couch_rep_httpc.erl Tue Dec 7 20:26:32 2010 @@ -98,6 +98,8 @@ db_exists(Req, CanonicalUrl, CreateDB) - {ok, "302", RespHeaders, _} -> RedirectUrl = redirect_url(RespHeaders, Req#http_db.url), db_exists(Req#http_db{url = RedirectUrl}, CanonicalUrl); + {ok, "401", _, _} -> + throw({unauthorized, ?l2b(Url)}); Error -> ?LOG_DEBUG("DB at ~s could not be found because ~p", [Url, Error]), throw({db_not_found, ?l2b(Url)})