diff options
author | Jakub Witczak <kuba@erlang.org> | 2023-02-17 13:57:35 +0100 |
---|---|---|
committer | Jakub Witczak <kuba@erlang.org> | 2023-02-20 09:17:50 +0100 |
commit | 550d5733b95c088cc63a6db7521b3d1e284909b0 (patch) | |
tree | 19107faf69c7c92c486f1bb5eb9fd2bbb6cc9a55 | |
parent | 0863bd30aabd035c83158c78046c5ffda16127e1 (diff) | |
download | erlang-550d5733b95c088cc63a6db7521b3d1e284909b0.tar.gz |
inets: fix httpd and folder URI alias
- for URI pointing to a folder (missing trailing /) with a query component
- insert '/' after patch component but before query
- improve alias test
-rw-r--r-- | lib/inets/src/http_server/mod_alias.erl | 4 | ||||
-rw-r--r-- | lib/inets/test/httpd_SUITE.erl | 70 |
2 files changed, 47 insertions, 27 deletions
diff --git a/lib/inets/src/http_server/mod_alias.erl b/lib/inets/src/http_server/mod_alias.erl index 94607e705a..c2365484a6 100644 --- a/lib/inets/src/http_server/mod_alias.erl +++ b/lib/inets/src/http_server/mod_alias.erl @@ -66,7 +66,9 @@ do_alias(#mod{config_db = ConfigDB, ServerName = which_server_name(ConfigDB), Port = port_string(which_port(ConfigDB)), Protocol = get_protocol(SocketType), - URL = Protocol ++ ServerName ++ Port ++ ReqURI ++ "/", + {ReqPath, ReqQuery} = httpd_util:split_path(ReqURI), + URL = Protocol ++ ServerName ++ Port ++ ReqPath ++ "/" ++ + ["?" ++ ReqQuery || [] /= ReqQuery], ReasonPhrase = httpd_util:reason_phrase(301), Message = httpd_util:message(301, URL, ConfigDB), {proceed, diff --git a/lib/inets/test/httpd_SUITE.erl b/lib/inets/test/httpd_SUITE.erl index 9645937c36..1928f6e582 100644 --- a/lib/inets/test/httpd_SUITE.erl +++ b/lib/inets/test/httpd_SUITE.erl @@ -22,7 +22,7 @@ %% %% ct:run("../inets_test", httpd_SUITE). %% - +-compile({no_auto_import,[alias/1]}). -module(httpd_SUITE). -include_lib("kernel/include/file.hrl"). @@ -30,7 +30,7 @@ -include_lib("public_key/include/public_key.hrl"). -include_lib("inets/include/httpd.hrl"). -include("inets_test_lib.hrl"). - +-include_lib("stdlib/include/assert.hrl"). %% Note: This directive should only be used in test suites. -compile(export_all). @@ -1148,30 +1148,48 @@ alias_1_0(Config) when is_list(Config) -> alias() -> [{doc, "Test mod_alias"}]. -alias(Config) when is_list(Config) -> - ok = http_status("GET /pics/icon.sheet.gif ", Config, - [{statuscode, 200}, - {header, "Content-Type","image/gif"}, - {header, "Server"}, - {header, "Date"}]), - - ok = http_status("GET / ", Config, - [{statuscode, 200}, - {header, "Content-Type","text/html"}, - {header, "Server"}, - {header, "Date"}]), - - ok = http_status("GET /misc/ ", Config, - [{statuscode, 200}, - {header, "Content-Type","text/html"}, - {header, "Server"}, - {header, "Date"}]), - - %% Check redirection if trailing slash is missing. - ok = http_status("GET /misc ", Config, - [{statuscode, 301}, - {header, "Location"}, - {header, "Content-Type","text/html"}]). +alias(Config) when is_list(Config) -> + TestURIs200 = [ + {"GET /pics/icon.sheet.gif ", 200, "image/gif"}, + {"GET / ", 200, "text/html"}, + {"GET /misc/ ", 200, "text/html"} + ], + Test200 = + fun({Request, ResultCode, ContentType}) -> + ct:log("Request: ~s Expecting: ~p ~s", + [Request, ResultCode, ContentType]), + ok = http_status(Request, Config, + [{statuscode, ResultCode}, + {header, "Content-Type", ContentType}, + {header, "Server"}, + {header, "Date"}]) + end, + [Test200(T) || T <- TestURIs200], + TestURIs301 = + [ + %% Check redirection if trailing slash is missing. + {"GET /misc ", 301, "text/html", "/misc/$"}, + %% slash character expected after path(misc) not query component + {"GET /misc?test=test ", 301, "text/html", "/misc/\\?test=test$"} + ], + Test301 = + fun({Request, ResultCode, ContentType, TargetLinkRegexp}) -> + ct:log("Request: ~s Expecting: ~p ~s RE: ~s", + [Request, ResultCode, ContentType, TargetLinkRegexp]), + {ok, [RedirectLink]} = + http_status(Request, Config, + [{statuscode, ResultCode}, + {header, "Content-Type", ContentType}, + {header, "Server"}, + {header, "Date"}, + {fetch_hrefs, true}]), + ReResult = re:run(RedirectLink, TargetLinkRegexp), + ct:log("RedirectLink = ~p", [RedirectLink]), + ?assertMatch({match, _}, ReResult) + end, + [Test301(T) || T <- TestURIs301], + ok. + %%------------------------------------------------------------------------- actions() -> [{doc, "Test mod_actions"}]. |