Generate relative URLs in listing_fallback_handler
This commit is contained in:
parent
f0450acdfc
commit
5f97135a6e
|
|
@ -85,15 +85,23 @@ listing_fallback_handler(Req) ->
|
||||||
"<head><title>RabbitMQ Web Server</title></head>"
|
"<head><title>RabbitMQ Web Server</title></head>"
|
||||||
"<body><h1>RabbitMQ Web Server</h1><p>Contexts available:</p><ul>",
|
"<body><h1>RabbitMQ Web Server</h1><p>Contexts available:</p><ul>",
|
||||||
HTMLSuffix = "</ul></body></html>",
|
HTMLSuffix = "</ul></body></html>",
|
||||||
Contexts = [{"/" ++ P, D} || {P, D} <- gen_server:call(?MODULE, list)],
|
{ReqPath, _, _} = mochiweb_util:urlsplit_path(Req:get(raw_path)),
|
||||||
|
Contexts = gen_server:call(?MODULE, list),
|
||||||
List =
|
List =
|
||||||
case Contexts of
|
case Contexts of
|
||||||
[] ->
|
[] ->
|
||||||
"<li>No contexts installed</li>";
|
"<li>No contexts installed</li>";
|
||||||
_ ->
|
_ ->
|
||||||
[io_lib:format("<li><a href=\"~s\">~s</a></li>", [Path, Desc])
|
[handler_listing(Path, ReqPath, Desc)
|
||||||
|| {Path, Desc} <- Contexts, Desc =/= none] ++
|
|| {Path, Desc} <- Contexts]
|
||||||
[io_lib:format("<li>~s</li>", [Path])
|
|
||||||
|| {Path, Desc} <- Contexts, Desc == none]
|
|
||||||
end,
|
end,
|
||||||
Req:respond({200, [], HTMLPrefix ++ List ++ HTMLSuffix}).
|
Req:respond({200, [], HTMLPrefix ++ List ++ HTMLSuffix}).
|
||||||
|
|
||||||
|
handler_listing(Path, ReqPath, Desc) ->
|
||||||
|
handler_listing(rabbit_mochiweb_util:relativise(ReqPath, "/" ++ Path),
|
||||||
|
Desc).
|
||||||
|
|
||||||
|
handler_listing(Path, none) ->
|
||||||
|
io_lib:format("<li>~s</li>", [Path]);
|
||||||
|
handler_listing(Path, Desc) ->
|
||||||
|
io_lib:format("<li><a href=\"~s\">~s</a></li>", [Path, Desc]).
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
-module(rabbit_mochiweb_util).
|
-module(rabbit_mochiweb_util).
|
||||||
|
|
||||||
-export([parse_auth_header/1]).
|
-export([parse_auth_header/1]).
|
||||||
|
-export([relativise/2]).
|
||||||
|
|
||||||
parse_auth_header(Header) ->
|
parse_auth_header(Header) ->
|
||||||
case Header of
|
case Header of
|
||||||
|
|
@ -35,3 +36,19 @@ parse_auth_header(Header) ->
|
||||||
_ ->
|
_ ->
|
||||||
invalid
|
invalid
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
relativise("/" ++ F, "/" ++ T) ->
|
||||||
|
From = string:tokens(F, "/"),
|
||||||
|
To = string:tokens(T, "/"),
|
||||||
|
string:join(relativise0(From, To), "/").
|
||||||
|
|
||||||
|
relativise0([H], [H|_] = To) ->
|
||||||
|
To;
|
||||||
|
relativise0([H|From], [H|To]) ->
|
||||||
|
relativise0(From, To);
|
||||||
|
relativise0(From, []) ->
|
||||||
|
lists:duplicate(length(From), "..");
|
||||||
|
relativise0([_|From], To) ->
|
||||||
|
lists:duplicate(length(From), "..") ++ To;
|
||||||
|
relativise0([], To) ->
|
||||||
|
To.
|
||||||
|
|
|
||||||
|
|
@ -7,3 +7,24 @@ query_static_resource_test() ->
|
||||||
{ok, _Result} =
|
{ok, _Result} =
|
||||||
http:request("http://localhost:55672/rabbit_mochiweb_test/index.html"),
|
http:request("http://localhost:55672/rabbit_mochiweb_test/index.html"),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
relativise_test() ->
|
||||||
|
?assertEqual("baz",
|
||||||
|
rabbit_mochiweb_util:relativise("/foo/bar/bash",
|
||||||
|
"/foo/bar/baz")),
|
||||||
|
?assertEqual("../bax/baz",
|
||||||
|
rabbit_mochiweb_util:relativise("/foo/bar/bash",
|
||||||
|
"/foo/bax/baz")),
|
||||||
|
?assertEqual("../bax/baz",
|
||||||
|
rabbit_mochiweb_util:relativise("/bar/bash",
|
||||||
|
"/bax/baz")),
|
||||||
|
?assertEqual("..",
|
||||||
|
rabbit_mochiweb_util:relativise("/foo/bar/bash",
|
||||||
|
"/foo/bar")),
|
||||||
|
?assertEqual("../..",
|
||||||
|
rabbit_mochiweb_util:relativise("/foo/bar/bash",
|
||||||
|
"/foo")),
|
||||||
|
?assertEqual("bar/baz",
|
||||||
|
rabbit_mochiweb_util:relativise("/foo/bar",
|
||||||
|
"/foo/bar/baz")),
|
||||||
|
?assertEqual("foo", rabbit_mochiweb_util:relativise("/", "/foo")).
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue