Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add index_files directive into the server part configuration

This directive sets the list of resources to look for, when a directory is
requested by the client. If the last entry begins with a "/", and none of
the earlier resources are found, Yaws will perform a redirect to this uri.

Default is:
  index_files = index.yaws index.html index.php
  • Loading branch information...
commit 6c6f62facf53ee63cf56eeb71754ce156ee6a2eb 1 parent 5b34d48
Christopher Faulet authored
View
7 doc/yaws.tex
@@ -2612,6 +2612,13 @@ \section{Server Part}
\textit{cgi}, \textit{fcgi}, \textit{php}. Default is\\
\verb+tilde_allowed_scripts =+ (i.e., empty).
+\item \verb+index_files = ListOfResources+ ---
+ This directive sets the list of resources to look for, when a
+ directory is requested by the client. If the last entry begins with
+ a `/', and none of the earlier resources are found, \Yaws\ will
+ perform a redirect to this uri.\\
+ Default is \verb+index_files = index.yaws index.html index.php+.
+
\item \verb+appmods = ListOfModuleNames+ ---
If any the names in \verb+ListOfModuleNames+ appear as components
in the path for a request, the path request parsing will terminate
View
1  include/yaws.hrl
@@ -233,6 +233,7 @@
start_mod, % user provided module to be started
allowed_scripts = [yaws,php,cgi,fcgi],
tilde_allowed_scripts = [],
+ index_files = ["index.yaws", "index.html", "index.php"],
revproxy = [],
soptions = [],
extra_cgi_vars = [],
View
6 man/yaws.conf.5
@@ -546,6 +546,12 @@ The allowed script types for this server when executing files in a users
public_html folder Recognized are `yaws', `cgi', `fcgi', `php'. Default is
\fItilde_allowed_scripts =\fR i.e. empty
+.TP
+\fBindex_files = ListOfResources\fR
+This directive sets the list of resources to look for, when a directory is
+requested by the client. If the last entry begins with a `/', and none of the
+earlier resources are found, Yaws will perform a redirect to this uri.
+Default is \fIindex_files = index.yaws index.html index.php\fR.
.TP
\fBappmods = ListOfModuleNames\fR
View
21 src/yaws_config.erl
@@ -1186,6 +1186,15 @@ fload(FD, server, GC, C, Cs, Lno, Chars) ->
Suffixes)},
fload(FD, server, GC, C2, Cs, Lno+1, Next);
+ ["index_files", '=' | Files] ->
+ case parse_index_files(Files) of
+ ok ->
+ C2 = C#sconf{index_files = Files},
+ fload(FD, server, GC, C2, Cs, Lno+1, Next);
+ {error, Str} ->
+ {error, ?F("~s at line ~w", [Str, Lno])}
+ end;
+
["revproxy", '=' | Tail] ->
case parse_revproxy(Tail) of
{ok, RevProxy} ->
@@ -2069,6 +2078,18 @@ parse_compressible_mime_types([], Acc) ->
{ok, Acc}.
+parse_index_files([]) ->
+ ok;
+parse_index_files([Idx|Rest]) ->
+ case Idx of
+ [$/|_] when Rest /= [] ->
+ {error, "Only the last index should be absolute"};
+ _ ->
+ parse_index_files(Rest)
+ end.
+
+
+
ssl_start() ->
case catch ssl:start() of
ok ->
View
49 src/yaws_server.erl
@@ -4392,45 +4392,40 @@ construct_fullpath(DocRoot,GetPath,VirtualDir) ->
%%preconditions:
%% - see 'construct_fullpath'
%%
-try_index_file(DR, GetPath, VirtualDir) ->
- FullPath = construct_fullpath(DR, GetPath, VirtualDir),
-
- case prim_file:read_file_info([FullPath, "index.yaws"]) of
+try_index_file(_FullPath, _GetPath, []) ->
+ noindex;
+try_index_file(FullPath, GetPath, [[$/|_]=Idx|Rest]) ->
+ case (GetPath =:= Idx orelse GetPath =:= Idx++"/") of
+ true -> try_index_file(FullPath, GetPath, Rest);
+ false -> {redir, Idx}
+ end;
+try_index_file(FullPath, GetPath, [Idx|Rest]) ->
+ case prim_file:read_file_info([FullPath, Idx]) of
{ok, FI} when FI#file_info.type == regular ->
- do_url_type(get(sc), GetPath ++ "index.yaws", DR, VirtualDir);
+ {index, Idx};
_ ->
- case prim_file:read_file_info([FullPath, "index.html"]) of
- {ok, FI} when FI#file_info.type == regular ->
- do_url_type(get(sc), GetPath ++ "index.html", DR,
- VirtualDir);
- _ ->
- case prim_file:read_file_info([FullPath, "index.php"]) of
- {ok, FI} when FI#file_info.type == regular ->
- do_url_type(get(sc), GetPath ++ "index.php", DR,
- VirtualDir);
- _ ->
- noindex
- end
- end
+ try_index_file(FullPath, GetPath, Rest)
end.
maybe_return_dir(DR, GetPath,VirtualDir) ->
- case try_index_file(DR, GetPath,VirtualDir) of
+ SC = get(sc),
+ FullPath = construct_fullpath(DR, GetPath, VirtualDir),
+ case try_index_file(FullPath, GetPath, SC#sconf.index_files) of
+ {index, Idx} ->
+ do_url_type(SC, GetPath ++ Idx, DR, VirtualDir);
+ {redir, NewPath} ->
+ #urltype{type=redir, path=NewPath};
noindex ->
- FullPath = construct_fullpath(DR, GetPath, VirtualDir),
-
case file:list_dir(FullPath) of
{ok, List} ->
- #urltype{type = directory,
+ #urltype{type = directory,
fullpath = FullPath,
- dir = GetPath,
- data = List -- [".yaws_auth"]};
+ dir = GetPath,
+ data = List -- [".yaws_auth"]};
_Err ->
#urltype{type=error}
- end;
- UT ->
- UT
+ end
end.
View
7 test/conf/stdconf.conf
@@ -191,6 +191,13 @@ keepalive_timeout = 10000
</server>
<server localhost>
+ port = 8010
+ listen = 0.0.0.0
+ docroot = %YTOP%/www
+ index_files = index.html /testdir
+</server>
+
+<server localhost>
port = 8443
listen = 0.0.0.0
docroot = %YTOP%/www
View
18 test/t2/app_test.erl
@@ -32,6 +32,7 @@ start() ->
sslaccept_timeout_test(),
throw_test(),
too_many_headers_test(),
+ index_files_test(),
ibrowse:stop().
@@ -794,6 +795,23 @@ too_many_headers_test() ->
?line {ok, "431", _, _} = ibrowse:send_req(Uri, Hdrs, get),
ok.
+
+index_files_test() ->
+ io:format("index_files test\n", []),
+ %% "/" should be redirected to "/testdir", then to "/testdir/" and finally
+ %% get "/testdir/index.html"
+ Uri0 = "http://localhost:8010/",
+ ?line {ok, Bin} = file:read_file("../../www/testdir/index.html"),
+ Content = binary_to_list(Bin),
+ ?line {ok, "302", Hdrs1, _} = ibrowse:send_req(Uri0, [], get),
+ ?line Uri1 = proplists:get_value("Location", Hdrs1),
+ ?line "http://localhost:8010/testdir" = Uri1,
+ ?line {ok, "302", Hdrs2, _} = ibrowse:send_req(Uri1, [], get),
+ ?line Uri2 = proplists:get_value("Location", Hdrs2),
+ ?line "http://localhost:8010/testdir/" = Uri2,
+ ?line {ok, "200", _, Content} = ibrowse:send_req(Uri2, [], get),
+ ok.
+
%% used for appmod tests
%%
out(_A) ->
Please sign in to comment.
Something went wrong with that request. Please try again.