You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2020/04/06 22:01:59 UTC

[couchdb] 01/05: Return better responses for endpoints which are not implemented

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch fix-api-corner-cases-and-make-chttpd-pass
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 3c816f9112f32b0c36ba8fe22fdcbe832b9c9ce3
Author: Nick Vatamaniuc <va...@apache.org>
AuthorDate: Mon Apr 6 17:39:22 2020 -0400

    Return better responses for endpoints which are not implemented
    
    Endpoints which are removed return a 410 response:
    
     - _show
     - _list
     - _rewrite
    
    Endpoints which will be implemented in CouchDB 4.x eventually now return a 510
    response:
    
     - _purge
     - _purge_infos_limit
    
    Endpoints which return a 2xx but are a no-op effectively:
    
     - _compact
     - _view_cleanup
---
 src/chttpd/src/chttpd_db.erl             | 24 ++++++++----------------
 src/chttpd/src/chttpd_httpd_handlers.erl | 30 ++++++++++++++++++++++++++----
 src/chttpd/src/chttpd_view.erl           |  5 +----
 3 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index e9b33f0..49d6e87 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -252,27 +252,19 @@ maybe_flush_changes_feed(Acc0, Data, Len) ->
     },
     {ok, Acc}.
 
-handle_compact_req(#httpd{method='POST'}=Req, Db) ->
+
+% Return the same response as if a compaction succeeded even though _compaction
+% isn't a valid operation in CouchDB >= 4.x anymore. This is mostly to not
+% break existing user script which maybe periodically call this endpoint. In
+% the future this endpoint will return a 410 response then it will be removed.
+handle_compact_req(#httpd{method='POST'}=Req, _Db) ->
     chttpd:validate_ctype(Req, "application/json"),
-    case Req#httpd.path_parts of
-        [_DbName, <<"_compact">>] ->
-            ok = fabric:compact(Db),
-            send_json(Req, 202, {[{ok, true}]});
-        [DbName, <<"_compact">>, DesignName | _] ->
-            case ddoc_cache:open(DbName, <<"_design/", DesignName/binary>>) of
-                {ok, _DDoc} ->
-                    ok = fabric:compact(Db, DesignName),
-                    send_json(Req, 202, {[{ok, true}]});
-                Error ->
-                    throw(Error)
-            end
-    end;
+    send_json(Req, 202, {[{ok, true}]});
 
 handle_compact_req(Req, _Db) ->
     send_method_not_allowed(Req, "POST").
 
-handle_view_cleanup_req(Req, Db) ->
-    ok = fabric:cleanup_index_files_all_nodes(Db),
+handle_view_cleanup_req(Req, _Db) ->
     send_json(Req, 202, {[{ok, true}]}).
 
 
diff --git a/src/chttpd/src/chttpd_httpd_handlers.erl b/src/chttpd/src/chttpd_httpd_handlers.erl
index 831c014..4d9acd4 100644
--- a/src/chttpd/src/chttpd_httpd_handlers.erl
+++ b/src/chttpd/src/chttpd_httpd_handlers.erl
@@ -14,6 +14,13 @@
 
 -export([url_handler/1, db_handler/1, design_handler/1, handler_info/3]).
 
+-export([
+    not_supported/2,
+    not_supported/3,
+    not_implemented/2
+]).
+
+
 -include_lib("couch/include/couch_db.hrl").
 
 
@@ -36,16 +43,18 @@ db_handler(<<"_view_cleanup">>) -> fun chttpd_db:handle_view_cleanup_req/2;
 db_handler(<<"_compact">>)      -> fun chttpd_db:handle_compact_req/2;
 db_handler(<<"_design">>)       -> fun chttpd_db:handle_design_req/2;
 db_handler(<<"_partition">>)    -> fun chttpd_db:handle_partition_req/2;
-db_handler(<<"_temp_view">>)    -> fun chttpd_view:handle_temp_view_req/2;
+db_handler(<<"_temp_view">>)    -> fun ?MODULE:not_supported/2;
 db_handler(<<"_changes">>)      -> fun chttpd_db:handle_changes_req/2;
+db_handler(<<"_purge">>)        -> fun ?MODULE:not_implemented/2;
+db_handler(<<"_purged_infos_limit">>) -> fun ?MODULE:not_implemented/2;
 db_handler(_) -> no_match.
 
 design_handler(<<"_view">>)    -> fun chttpd_view:handle_view_req/3;
-design_handler(<<"_show">>)    -> fun chttpd_show:handle_doc_show_req/3;
-design_handler(<<"_list">>)    -> fun chttpd_show:handle_view_list_req/3;
+design_handler(<<"_show">>)    -> fun ?MODULE:not_supported/3;
+design_handler(<<"_list">>)    -> fun ?MODULE:not_supported/3;
 design_handler(<<"_update">>)  -> fun chttpd_show:handle_doc_update_req/3;
 design_handler(<<"_info">>)    -> fun chttpd_db:handle_design_info_req/3;
-design_handler(<<"_rewrite">>) -> fun chttpd_rewrite:handle_rewrite_req/3;
+design_handler(<<"_rewrite">>) -> fun ?MODULE:not_supported/3;
 design_handler(_) -> no_match.
 
 
@@ -484,3 +493,16 @@ get_copy_destination(Req) ->
         unknown
     end.
 
+
+not_supported(#httpd{} = Req, Db, _DDoc) ->
+    not_supported(Req, Db).
+
+
+not_supported(#httpd{} = Req, _Db) ->
+    Msg = <<"resource is not supported in CouchDB >= 4.x">>,
+    chttpd:send_error(Req, 410, gone, Msg).
+
+
+not_implemented(#httpd{} = Req, _Db) ->
+    Msg = <<"resouce is not implemented">>,
+    chttpd:send_error(Req, 501, not_implemented, Msg).
diff --git a/src/chttpd/src/chttpd_view.erl b/src/chttpd/src/chttpd_view.erl
index 49ca1a7..84997e5 100644
--- a/src/chttpd/src/chttpd_view.erl
+++ b/src/chttpd/src/chttpd_view.erl
@@ -14,7 +14,7 @@
 -include_lib("couch/include/couch_db.hrl").
 -include_lib("couch_mrview/include/couch_mrview.hrl").
 
--export([handle_view_req/3, handle_temp_view_req/2]).
+-export([handle_view_req/3]).
 
 multi_query_view(Req, Db, DDoc, ViewName, Queries) ->
     Args0 = couch_mrview_http:parse_params(Req, undefined),
@@ -101,9 +101,6 @@ handle_view_req(#httpd{method='POST',
 handle_view_req(Req, _Db, _DDoc) ->
     chttpd:send_method_not_allowed(Req, "GET,POST,HEAD").
 
-handle_temp_view_req(Req, _Db) ->
-    Msg = <<"Temporary views are not supported in CouchDB">>,
-    chttpd:send_error(Req, 410, gone, Msg).
 
 % See https://github.com/apache/couchdb/issues/2168
 assert_no_queries_param(undefined) ->