1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
16
%% $Id: mod_alias.erl,v 1.1 2008/12/17 09:53:34 mikpe Exp $
19
-export([do/1,real_name/3,real_script_name/3,default_index/2,load/2,path/3]).
21
-include("httpd.hrl").
26
?DEBUG("do -> entry",[]),
27
case httpd_util:key1search(Info#mod.data,status) of
28
%% A status code has been generated!
29
{StatusCode,PhraseArgs,Reason} ->
30
{proceed,Info#mod.data};
31
%% No status code has been generated!
33
case httpd_util:key1search(Info#mod.data,response) of
34
%% No response has been generated!
37
%% A response has been generated or sent!
39
{proceed,Info#mod.data}
44
?DEBUG("do_alias -> Request URI: ~p",[Info#mod.request_uri]),
45
{ShortPath,Path,AfterPath} =
46
real_name(Info#mod.config_db,Info#mod.request_uri,
47
httpd_util:multi_lookup(Info#mod.config_db,alias)),
48
%% Relocate if a trailing slash is missing else proceed!
49
LastChar = lists:last(ShortPath),
50
case file:read_file_info(ShortPath) of
51
{ok,FileInfo} when FileInfo#file_info.type == directory,LastChar /= $/ ->
56
[ShortPath,LastChar,FileInfo]),
57
ServerName = httpd_util:lookup(Info#mod.config_db,server_name),
58
Port = port_string(httpd_util:lookup(Info#mod.config_db,port,80)),
59
URL = "http://"++ServerName++Port++Info#mod.request_uri++"/",
60
ReasonPhrase = httpd_util:reason_phrase(301),
61
Message = httpd_util:message(301,URL,Info#mod.config_db),
64
{301, ["Location: ", URL, "\r\n"
65
"Content-Type: text/html\r\n",
67
"<HTML>\n<HEAD>\n<TITLE>",ReasonPhrase,
69
"<BODY>\n<H1>",ReasonPhrase,
71
"\n</BODY>\n</HTML>\n"]}}|
72
[{real_name,{Path,AfterPath}}|Info#mod.data]]};
74
{proceed,[{real_name,{Path,AfterPath}}|Info#mod.data]}
80
":"++integer_to_list(Port).
84
real_name(ConfigDB, RequestURI,[]) ->
85
DocumentRoot = httpd_util:lookup(ConfigDB, document_root, ""),
86
RealName = DocumentRoot++RequestURI,
87
{ShortPath, _AfterPath} = httpd_util:split_path(RealName),
88
{Path, AfterPath}=httpd_util:split_path(default_index(ConfigDB,RealName)),
89
{ShortPath, Path, AfterPath};
90
real_name(ConfigDB, RequestURI, [{FakeName,RealName}|Rest]) ->
91
case regexp:match(RequestURI, "^"++FakeName) of
93
{ok, ActualName, _} = regexp:sub(RequestURI,
94
"^"++FakeName, RealName),
95
{ShortPath, _AfterPath} = httpd_util:split_path(ActualName),
97
httpd_util:split_path(default_index(ConfigDB, ActualName)),
98
{ShortPath, Path, AfterPath};
100
real_name(ConfigDB,RequestURI,Rest)
105
real_script_name(ConfigDB,RequestURI,[]) ->
107
real_script_name(ConfigDB,RequestURI,[{FakeName,RealName}|Rest]) ->
108
case regexp:match(RequestURI,"^"++FakeName) of
110
{ok,ActualName,_}=regexp:sub(RequestURI,"^"++FakeName,RealName),
111
httpd_util:split_script_path(default_index(ConfigDB,ActualName));
113
real_script_name(ConfigDB,RequestURI,Rest)
118
default_index(ConfigDB, Path) ->
119
case file:read_file_info(Path) of
120
{ok, FileInfo} when FileInfo#file_info.type == directory ->
121
DirectoryIndex = httpd_util:lookup(ConfigDB, directory_index, []),
122
append_index(Path, DirectoryIndex);
127
append_index(RealName, []) ->
129
append_index(RealName, [Index|Rest]) ->
130
case file:read_file_info(filename:join(RealName, Index)) of
132
append_index(RealName, Rest);
134
filename:join(RealName,Index)
139
path(Data, ConfigDB, RequestURI) ->
140
case httpd_util:key1search(Data,real_name) of
142
DocumentRoot = httpd_util:lookup(ConfigDB, document_root, ""),
144
httpd_util:split_path(DocumentRoot++RequestURI),
156
load([$D,$i,$r,$e,$c,$t,$o,$r,$y,$I,$n,$d,$e,$x,$ |DirectoryIndex],[]) ->
157
{ok, DirectoryIndexes} = regexp:split(DirectoryIndex," "),
158
{ok,[], {directory_index, DirectoryIndexes}};
159
load([$A,$l,$i,$a,$s,$ |Alias],[]) ->
160
case regexp:split(Alias," ") of
161
{ok, [FakeName, RealName]} ->
162
{ok,[],{alias,{FakeName,RealName}}};
164
{error,?NICE(httpd_conf:clean(Alias)++" is an invalid Alias")}
166
load([$S,$c,$r,$i,$p,$t,$A,$l,$i,$a,$s,$ |ScriptAlias],[]) ->
167
case regexp:split(ScriptAlias," ") of
168
{ok, [FakeName, RealName]} ->
169
%% Make sure the path always has a trailing slash..
170
RealName1 = filename:join(filename:split(RealName)),
171
{ok, [], {script_alias,{FakeName, RealName1++"/"}}};
173
{error, ?NICE(httpd_conf:clean(ScriptAlias)++
174
" is an invalid ScriptAlias")}