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

nickva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/main by this push:
     new 66bb79b07 Improve _design%2f handling
66bb79b07 is described below

commit 66bb79b070f0edc3742daaeb4bc573f828478486
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Thu Oct 29 14:38:11 2026 -0400

    Improve _design%2f handling
    
    Stop redirecting to ourselves, instead just handle the request.
    
    This also fixes a regression introduced in #6187
    
    ```sh
    
    set -eu
    
    DB=http://127.0.0.1:15984
    
    curl -sS -u adm:pass -o /dev/null -XDELETE "$DB/db" || true
    curl -sS -u adm:pass -o /dev/null -XPUT "$DB/db?q=1"
    curl -sS -u adm:pass -o /dev/null -XPUT "$DB/db/_design/ddoc" -H 
'Content-Type: application/json' -d '{"views":{"v":{"map":"function(doc){ if 
(doc.x) emit(doc.x, 1); }"}}}'
    curl -sS -u adm:pass -o /dev/null -XPUT "$DB/db/a" -H 'Content-Type: 
application/json' -d '{"x":"a"}'
    echo "*********** see the redirects"
    curl -sS -u adm:pass -g -i 
"$DB/db/_design%2Fddoc/_view/v?startkey=[\"a\"]&endkey=[\"z\"]"
    echo "*********** follow redirects"
    curl -L -sS -u adm:pass -g -i
    "$DB/db/_design%2Fddoc/_view/v?startkey=[\"a\"]&endkey=[\"z\"]"
    ```
    
    With the fix:
    ```
    ./redir.sh
    *********** see the redirects
    HTTP/1.1 200 OK
    
    {"total_rows":1,"offset":1,"rows":[
    
    ]}
    
    *********** follow redirects
    HTTP/1.1 200 OK
    {"total_rows":1,"offset":1,"rows":[
    
    ]}
    ```
---
 src/chttpd/src/chttpd_db.erl                      | 13 +---
 src/couch/src/couch_httpd_db.erl                  | 22 +------
 src/couch/test/eunit/couchdb_design_doc_tests.erl | 76 ++++++++++++++---------
 test/elixir/test/design_paths_test.exs            | 14 +++--
 4 files changed, 63 insertions(+), 62 deletions(-)

diff --git a/src/chttpd/src/chttpd_db.erl b/src/chttpd/src/chttpd_db.erl
index 2b8452b3f..6c29c83f0 100644
--- a/src/chttpd/src/chttpd_db.erl
+++ b/src/chttpd/src/chttpd_db.erl
@@ -89,6 +89,9 @@
 -define(MAX_DDOC_NUM_FOR_INDEX_INFO, 1000).
 
 % Database request handlers
+handle_request(#httpd{path_parts = [DbName, <<"_design/", Name/binary>> | 
Rest]} = Req) ->
+    % Handle _design%2fname as _design/name
+    handle_request(Req#httpd{path_parts = [DbName, ~"_design", Name | Rest]});
 handle_request(#httpd{path_parts = [DbName | RestParts], method = Method} = 
Req) ->
     case {Method, RestParts} of
         {'PUT', []} ->
@@ -1087,16 +1090,6 @@ db_req(#httpd{method = 'DELETE', path_parts = [_, 
<<"_time_seq">>]} = Req, Db) -
     end;
 db_req(#httpd{path_parts = [_, <<"_time_seq">>]} = Req, _Db) ->
     send_method_not_allowed(Req, "GET,DELETE");
-% Special case to enable using an unencoded slash in the URL of design docs,
-% as slashes in document IDs must otherwise be URL encoded.
-db_req(
-    #httpd{
-        method = 'GET', mochi_req = MochiReq, path_parts = [_DbName, 
<<"_design/", _/binary>> | _]
-    } = Req,
-    _Db
-) ->
-    [Head | Tail] = re:split(MochiReq:get(raw_path), "_design%2F", [{return, 
list}, caseless]),
-    chttpd:send_redirect(Req, Head ++ "_design/" ++ Tail);
 db_req(#httpd{path_parts = [_DbName, <<"_design">>, Name]} = Req, Db) ->
     db_doc_req(Req, Db, <<"_design/", Name/binary>>);
 db_req(#httpd{path_parts = [_DbName, <<"_design">>, Name | FileNameParts]} = 
Req, Db) ->
diff --git a/src/couch/src/couch_httpd_db.erl b/src/couch/src/couch_httpd_db.erl
index 733991d84..21b21a8e8 100644
--- a/src/couch/src/couch_httpd_db.erl
+++ b/src/couch/src/couch_httpd_db.erl
@@ -56,6 +56,9 @@
 }).
 
 % Database request handlers
+handle_request(#httpd{path_parts = [DbName, <<"_design/", Name/binary>> | 
Rest]} = Req) ->
+    % handle _design%2Fname as _design/name
+    handle_request(Req#httpd{path_parts = [DbName, ~"_design", Name | Rest]});
 handle_request(
     #httpd{
         path_parts = [DbName | RestParts],
@@ -512,25 +515,6 @@ db_req(#httpd{method = 'GET', path_parts = [_, 
<<"_revs_limit">>]} = Req, Db) ->
     send_json(Req, couch_db:get_revs_limit(Db));
 db_req(#httpd{path_parts = [_, <<"_revs_limit">>]} = Req, _Db) ->
     send_method_not_allowed(Req, "PUT,GET");
-% Special case to enable using an unencoded slash in the URL of design docs,
-% as slashes in document IDs must otherwise be URL encoded.
-db_req(
-    #httpd{
-        method = 'GET', mochi_req = MochiReq, path_parts = [DbName, 
<<"_design/", _/binary>> | _]
-    } = Req,
-    _Db
-) ->
-    PathFront = "/" ++ couch_httpd:quote(binary_to_list(DbName)) ++ "/",
-    [_ | PathTail] = re:split(
-        MochiReq:get(raw_path),
-        "_design%2F",
-        [{return, list}]
-    ),
-    couch_httpd:send_redirect(
-        Req,
-        PathFront ++ "_design/" ++
-            mochiweb_util:join(PathTail, "_design%2F")
-    );
 db_req(#httpd{path_parts = [_DbName, <<"_design">>, Name]} = Req, Db) ->
     db_doc_req(Req, Db, <<"_design/", Name/binary>>);
 db_req(#httpd{path_parts = [_DbName, <<"_design">>, Name | FileNameParts]} = 
Req, Db) ->
diff --git a/src/couch/test/eunit/couchdb_design_doc_tests.erl 
b/src/couch/test/eunit/couchdb_design_doc_tests.erl
index c51d56f0b..0cefa134b 100644
--- a/src/couch/test/eunit/couchdb_design_doc_tests.erl
+++ b/src/couch/test/eunit/couchdb_design_doc_tests.erl
@@ -19,7 +19,7 @@ setup() ->
     DbName = ?tempdb(),
     {ok, Db} = couch_db:create(DbName, [?ADMIN_CTX]),
     ok = couch_db:close(Db),
-    create_design_doc(DbName, <<"_design/foo">>),
+    create_design_doc(DbName, ~"_design/foo"),
     Addr = config:get("httpd", "bind_address", "127.0.0.1"),
     Port = integer_to_list(mochiweb_socket_server:get(couch_httpd, port)),
     BaseUrl = "http://"; ++ Addr ++ ":" ++ Port,
@@ -41,50 +41,68 @@ design_list_test_() ->
                 fun setup/0,
                 fun teardown/1,
                 [
-                    fun should_return_empty_when_plain_return/1,
-                    fun should_return_empty_when_no_docs/1
+                    ?TDEF_FE(should_return_empty_when_plain_return),
+                    ?TDEF_FE(should_return_empty_when_no_docs),
+                    ?TDEF_FE(should_serve_encoded_design_doc_path)
                 ]
             }
         }
     }.
 
 should_return_empty_when_plain_return({DbName, BaseUrl}) ->
-    ?_test(begin
-        ?assertEqual(
-            <<>>,
-            query_text(BaseUrl, DbName, "foo", 
"_list/plain_return/simple_view")
-        )
-    end).
+    ?assertEqual(
+        <<>>,
+        query_text(BaseUrl, DbName, "foo", "_list/plain_return/simple_view")
+    ).
 
 should_return_empty_when_no_docs({DbName, BaseUrl}) ->
-    ?_test(begin
-        ?assertEqual(
-            <<>>,
-            query_text(BaseUrl, DbName, "foo", 
"_list/simple_render/simple_view")
-        )
-    end).
+    ?assertEqual(
+        <<>>,
+        query_text(BaseUrl, DbName, "foo", "_list/simple_render/simple_view")
+    ).
+
+% test %2F slash in the ddoc id
+should_serve_encoded_design_doc_path({DbName, BaseUrl}) ->
+    Url = BaseUrl ++ "/" ++ DbName ++ "/_design%2Ffoo",
+    {ok, Code, _Headers, Body} = test_request:get(Url),
+    ?assertEqual(200, Code),
+    {Props} = couch_util:json_decode(Body),
+    ?assertEqual(~"_design/foo", couch_util:get_value(~"_id", Props)),
+    ViewUrl = Url ++ "/_view/simple_view?reduce=false",
+    Headers = [{"Content-Type", "application/json"}],
+    {ok, ViewCode, _, ViewBody} = test_request:post(ViewUrl, Headers, 
"{\"keys\": [\"x\"]}"),
+    ?assertEqual(200, ViewCode),
+    {ViewProps} = couch_util:json_decode(ViewBody),
+    ?assertEqual([], couch_util:get_value(~"rows", ViewProps)).
 
 create_design_doc(DbName, DDName) ->
     {ok, Db} = couch_db:open(DbName, [?ADMIN_CTX]),
     DDoc = couch_doc:from_json_obj(
         {[
-            {<<"_id">>, DDName},
-            {<<"language">>, <<"javascript">>},
-            {<<"views">>,
+            {~"_id", DDName},
+            {~"language", ~"javascript"},
+            {
+                ~"views",
                 {[
-                    {<<"simple_view">>,
+                    {
+                        ~"simple_view",
                         {[
-                            {<<"map">>, <<"function(doc) {emit(doc._id, 
doc)}">>},
-                            {<<"reduce">>,
-                                <<"function (key, values, rereduce) {return 
sum(values);}">>}
-                        ]}}
-                ]}},
-            {<<"lists">>,
+                            {~"map", ~"function(doc) {emit(doc._id, doc)}"},
+                            {~"reduce", ~"function (key, values, rereduce) 
{return sum(values);}"}
+                        ]}
+                    }
+                ]}
+            },
+            {
+                ~"lists",
                 {[
-                    {<<"plain_return">>, <<"function(head, req) {return;}">>},
-                    {<<"simple_render">>,
-                        <<"function(head, req) {var row; while(row=getRow()) 
{send(JSON.stringify(row)); }}">>}
-                ]}}
+                    {~"plain_return", ~"function(head, req) {return;}"},
+                    {
+                        ~"simple_render",
+                        ~"function(head, req) {var row; while(row=getRow()) 
{send(JSON.stringify(row)); }}"
+                    }
+                ]}
+            }
         ]}
     ),
     {ok, Rev} = couch_db:update_doc(Db, DDoc, []),
diff --git a/test/elixir/test/design_paths_test.exs 
b/test/elixir/test/design_paths_test.exs
index 1485eb878..aabf71816 100644
--- a/test/elixir/test/design_paths_test.exs
+++ b/test/elixir/test/design_paths_test.exs
@@ -26,10 +26,17 @@ defmodule DesignPathTest do
     assert resp.body["_id"] == "_design/test"
 
     resp = Couch.get("/#{db_name}/_design%2Ftest")
-    assert resp.status_code == 301
-    resp = Couch.get(resp.headers["location"])
+    assert resp.status_code == 200
     assert resp.body["_id"] == "_design/test"
 
+    resp = Couch.get("/#{db_name}/_design%2Ftest/_view/testing")
+    assert resp.status_code == 200
+    assert Enum.empty?(resp.body["rows"])
+
+    resp = Couch.post("/#{db_name}/_design%2Ftest/_view/testing", body: 
%{keys: [1]})
+    assert resp.status_code == 200
+    assert Enum.empty?(resp.body["rows"])
+
     resp = Couch.get("/#{db_name}/_design/test/_view/testing")
     assert Enum.empty?(Map.get(resp, :body)["rows"])
 
@@ -49,8 +56,7 @@ defmodule DesignPathTest do
     assert resp.body["_id"] == "_design/test2"
 
     resp = Couch.get("/#{db_name}/_design%2Ftest2")
-    assert resp.status_code == 301
-    resp = Couch.get(resp.headers["location"])
+    assert resp.status_code == 200
     assert resp.body["_id"] == "_design/test2"
 
     resp = Couch.get("/#{db_name}/_design/test2/_view/testing")

Reply via email to