This is an automated email from the ASF dual-hosted git repository. rnewson pushed a commit to branch remove-local-source-target in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit c73e03ff869a3c71a51cdfb6d20838f79deca88d Author: Robert Newson <[email protected]> AuthorDate: Thu Oct 29 23:25:36 2026 +0000 finally remove "local" source/target support in _replicate --- src/chttpd/src/chttpd.erl | 73 ++-------------------- src/chttpd/src/chttpd_misc.erl | 4 +- src/chttpd/test/eunit/chttpd_handlers_tests.erl | 12 +++- .../src/couch_replicator_parse.erl | 2 +- 4 files changed, 17 insertions(+), 74 deletions(-) diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl index 4fd809b97..5964a848b 100644 --- a/src/chttpd/src/chttpd.erl +++ b/src/chttpd/src/chttpd.erl @@ -426,7 +426,7 @@ handle_req_after_auth(HandlerKey, HttpReq) -> fun chttpd_db:handle_request/1 ), AuthorizedReq = chttpd_auth:authorize( - possibly_hack(HttpReq), + HttpReq, fun chttpd_auth_request:authorize_request/1 ), {AuthorizedReq, HandlerFun(AuthorizedReq)} @@ -554,19 +554,6 @@ maybe_log(#httpd{} = HttpReq, #httpd_resp{should_log = true} = HttpResp) -> maybe_log(_HttpReq, #httpd_resp{should_log = false}) -> ok. -%% HACK: replication currently handles two forms of input, #db{} style -%% and #http_db style. We need a third that makes use of fabric. #db{} -%% works fine for replicating the dbs and nodes database because they -%% aren't sharded. So for now when a local db is specified as the source or -%% the target, it's hacked to make it a full url and treated as a remote. -possibly_hack(#httpd{path_parts = [<<"_replicate">>]} = Req) -> - {Props0} = json_body_obj(Req), - Props1 = fix_uri(Req, Props0, <<"source">>), - Props2 = fix_uri(Req, Props1, <<"target">>), - Req#httpd{req_body = {Props2}}; -possibly_hack(Req) -> - Req. - check_request_uri_length(Uri) -> check_request_uri_length( Uri, @@ -592,60 +579,6 @@ check_url_encoding([$% | _]) -> check_url_encoding([_ | Rest]) -> check_url_encoding(Rest). -fix_uri(Req, Props, Type) -> - case replication_uri(Type, Props) of - undefined -> - Props; - Uri0 -> - case is_http(Uri0) of - true -> - Props; - false -> - Uri = make_uri(Req, quote(Uri0)), - [{Type, Uri} | proplists:delete(Type, Props)] - end - end. - -replication_uri(Type, PostProps) -> - case couch_util:get_value(Type, PostProps) of - {Props} -> - couch_util:get_value(<<"url">>, Props); - Else -> - Else - end. - -is_http(<<"http://", _/binary>>) -> - true; -is_http(<<"https://", _/binary>>) -> - true; -is_http(_) -> - false. - -make_uri(Req, Raw) -> - Port = integer_to_list(mochiweb_socket_server:get(chttpd, port)), - Url = list_to_binary([ - "http://", - config:get("chttpd", "bind_address"), - ":", - Port, - "/", - Raw - ]), - Headers = [ - {<<"authorization">>, ?l2b(header_value(Req, "authorization", ""))}, - {<<"cookie">>, ?l2b(extract_cookie(Req))} - ], - {[{<<"url">>, Url}, {<<"headers">>, {Headers}}]}. - -extract_cookie(#httpd{mochi_req = MochiReq}) -> - case MochiReq:get_cookie_value("AuthSession") of - undefined -> - ""; - AuthSession -> - "AuthSession=" ++ AuthSession - end. -%%% end hack - %% erlfmt-ignore set_auth_handlers() -> AuthenticationHandlers0 = config:get("chttpd", "authentication_handlers", "cookie, default"), @@ -1155,6 +1088,10 @@ error_info({error, {database_name_too_long, DbName}}) -> <<"At least one path segment of `", DbName/binary, "` is too long.">>}; error_info({doc_validation, Reason}) -> {400, <<"doc_validation">>, Reason}; +error_info({error, local_endpoints_not_supported}) -> + {400, ~"invalid_replication", ~"local database names are not supported, use a full URL"}; +error_info({error, ~"Missing replicator database" = Reason}) -> + {400, ~"invalid_replication", Reason}; error_info({error, <<"endpoint has an invalid url">> = Reason}) -> {400, <<"invalid_replication">>, Reason}; error_info({error, <<"proxy has an invalid url">> = Reason}) -> diff --git a/src/chttpd/src/chttpd_misc.erl b/src/chttpd/src/chttpd_misc.erl index 761b01a1c..e823f1e08 100644 --- a/src/chttpd/src/chttpd_misc.erl +++ b/src/chttpd/src/chttpd_misc.erl @@ -225,9 +225,9 @@ handle_task_status_req(#httpd{method = 'GET'} = Req) -> handle_task_status_req(Req) -> send_method_not_allowed(Req, "GET,HEAD"). -handle_replicate_req(#httpd{method = 'POST', user_ctx = Ctx, req_body = PostBody} = Req) -> +handle_replicate_req(#httpd{method = 'POST', user_ctx = Ctx} = Req) -> chttpd:validate_ctype(Req, "application/json"), - %% see HACK in chttpd.erl about replication + PostBody = chttpd:json_body_obj(Req), case replicate(PostBody, Ctx) of {ok, {continuous, RepId}} -> send_json(Req, 202, {[{ok, true}, {<<"_local_id">>, RepId}]}); diff --git a/src/chttpd/test/eunit/chttpd_handlers_tests.erl b/src/chttpd/test/eunit/chttpd_handlers_tests.erl index b3e9f0337..39f089fcb 100644 --- a/src/chttpd/test/eunit/chttpd_handlers_tests.erl +++ b/src/chttpd/test/eunit/chttpd_handlers_tests.erl @@ -26,12 +26,12 @@ replicate_endpoint_test_() -> fun setup/0, fun teardown/1, [ - ?TDEF_FE(should_escape_local_dbname_on_replicate), + ?TDEF_FE(should_reject_local_dbname_on_replicate), ?TDEF_FE(require_authenticated_user) ] }. -should_escape_local_dbname_on_replicate({_Ctx, Url}) -> +should_reject_local_dbname_on_replicate({_Ctx, Url}) -> SrcPrefix = ?tempdb(), TgtPrefix = ?tempdb(), SrcDb = <<SrcPrefix/binary, "%2Fsrc">>, @@ -43,7 +43,13 @@ should_escape_local_dbname_on_replicate({_Ctx, Url}) -> <<"source">> => endpoint(Url, SrcDb, ?USER, ?PASS), <<"target">> => endpoint("", <<TgtPrefix/binary, "/tgt">>, ?USER, ?PASS) }), - ?assertMatch({200, #{<<"ok">> := true}}, Res), + ?assertMatch( + {400, #{ + <<"error">> := <<"invalid_replication">>, + <<"reason">> := <<"endpoint has an invalid url">> + }}, + Res + ), delete_db(Url, SrcDb), delete_db(Url, TgtDb). diff --git a/src/couch_replicator/src/couch_replicator_parse.erl b/src/couch_replicator/src/couch_replicator_parse.erl index 8713a61f4..0387efde8 100644 --- a/src/couch_replicator/src/couch_replicator_parse.erl +++ b/src/couch_replicator/src/couch_replicator_parse.erl @@ -228,7 +228,7 @@ parse_rep_db(<<"https://", _/binary>> = Url, Proxy, Options) -> parse_rep_db(<<_/binary>>, _Proxy, _Options) -> throw({error, local_endpoints_not_supported}); parse_rep_db(undefined, _Proxy, _Options) -> - throw({error, <<"Missing replicator database">>}). + throw({error, missing_replicator_database}). check_url(<<_/binary>> = Url, Type) when is_atom(Type) -> case ibrowse_lib:parse_url(?b2l(Url)) of
