Skip to content

Commit

Permalink
Add index_files directive into the server part configuration
Browse files Browse the repository at this point in the history
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
Christopher Faulet committed Jul 11, 2012
1 parent 5b34d48 commit 6c6f62f
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 27 deletions.
7 changes: 7 additions & 0 deletions doc/yaws.tex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions include/yaws.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
Expand Down
6 changes: 6 additions & 0 deletions man/yaws.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/yaws_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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} ->
Expand Down Expand Up @@ -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 ->
Expand Down
49 changes: 22 additions & 27 deletions src/yaws_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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.


Expand Down
7 changes: 7 additions & 0 deletions test/conf/stdconf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ keepalive_timeout = 10000
appmods = </, throwtest>
</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
Expand Down
18 changes: 18 additions & 0 deletions test/t2/app_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ start() ->
sslaccept_timeout_test(),
throw_test(),
too_many_headers_test(),
index_files_test(),
ibrowse:stop().


Expand Down Expand Up @@ -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) ->
Expand Down

0 comments on commit 6c6f62f

Please sign in to comment.