1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
%%%----------------------------------------------------------------------
%%% File : mod_http_fileserver.erl
%%% Author : Massimiliano Mirra <mmirra [at] process-one [dot] net>
%%% Purpose : Simple file server plugin for embedded ejabberd web server
%%% Created :
%%% Id :
%%%----------------------------------------------------------------------
-module(mod_http_fileserver).
-author('mmirra@process-one.net').
-vsn('').
-define(ejabberd_debug, true).
-compile([export_all]).
-behaviour(gen_mod).
-export([
start/2,
stop/1,
process/2,
ctl_process/2
]).
-include("ejabberd.hrl").
-include("jlib.hrl").
-include("ejabberd_http.hrl").
-include("ejabberd_ctl.hrl").
%%%----------------------------------------------------------------------
%%% REQUEST HANDLERS
%%%----------------------------------------------------------------------
%%-----------------------------------------------------------------------
%% FUNCTION
%%
%% process/2
%%
%% PURPOSE
%%
%% Handle an HTTP request.
%%
%% RETURNS
%%
%% Page to be sent back to the client and/or HTTP status code.
%%
%% ARGUMENTS
%%
%% - LocalPath: part of the requested URL path that is "local to the
%% module".
%%
%%-----------------------------------------------------------------------
process(LocalPath, Request) ->
?DEBUG("Requested ~p", [LocalPath]),
Result = serve(LocalPath),
case ets:lookup(mod_http_fileserver, accessfile) of
undefined ->
ok;
[{accessfile, AccessFile}] ->
{Code, _, _} = Result,
log(AccessFile, Code, Request)
end,
Result.
serve(LocalPath) ->
[{docroot, DocRoot}] = ets:lookup(mod_http_fileserver, docroot),
FileName = filename:join(filename:split(DocRoot) ++ LocalPath),
case file:read_file(FileName) of
{ok, FileContents} ->
?DEBUG("Delivering content.", []),
{200,
[{"Server", "ejabberd"},
{"Content-type", content_type(FileName)}],
FileContents};
{error, Error} ->
?DEBUG("Delivering error: ~p", [Error]),
case Error of
eacces -> {403, [], "Forbidden"};
enoent -> {404, [], "Not found"};
_Else -> {404, [], atom_to_list(Error)}
end
end.
ctl_process(_Val, ["reopen-weblog"]) ->
mod_http_fileserver_server ! reopenlog,
?STATUS_SUCCESS;
ctl_process(Val, _Args) ->
Val.
%%%----------------------------------------------------------------------
%%% UTILITIES
%%%----------------------------------------------------------------------
join([], _) ->
"";
join([E], _) ->
E;
join([H | T], Separator) ->
lists:foldl(fun(E, Acc) -> lists:concat([Acc, Separator, E]) end, H, T).
log(File, Code, Request) ->
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
IP = join(tuple_to_list(Request#request.ip), "."),
Path = join(Request#request.path, "/"),
Query = case join(lists:map(fun(E) -> lists:concat([element(1, E), "=", element(2, E)]) end,
Request#request.q), "&") of
[]�->
"";
String ->
[$? | String]
end,
% combined apache like log format :
% 127.0.0.1 - - [28/Mar/2007:18:41:55 +0200] "GET / HTTP/1.1" 302 303 "-" "tsung"
% XXX TODO some fields are harcoded/missing (reply size, user agent or referer for example)
io:format(File, "~p - - [~p/~p/~p:~p:~p:~p] \"~s /~s~s\" ~p -1 \"-\" \"-\"~n",
[IP, Day, Month, Year, Hour, Minute, Second, Request#request.method, Path, Query, Code]).
content_type(Filename) ->
case httpd_util:to_lower(filename:extension(Filename)) of
".jpg" -> "image/jpeg";
".jpeg" -> "image/jpeg";
".gif" -> "image/gif";
".png" -> "image/png";
".html" -> "text/html";
".css" -> "text/css";
".txt" -> "text/plain";
".xul" -> "application/vnd.mozilla.xul+xml";
".jar" -> "application/java-archive";
".xpi" -> "application/x-xpinstall";
_Else -> "application/octet-stream"
end.
open_file(Filename) ->
case file:open(Filename, [append]) of
{ok, File} ->
ets:insert(mod_http_fileserver, {accessfile, File}),
ok;
{error, _Reason} ->
{'EXIT', {unaccessible_accessfile, ?MODULE}}
end.
loop(Filename) ->
receive
reopenlog ->
case ets:lookup(mod_http_fileserver, accessfile) of
undefined ->
ok;
[{accessfile, AccessFile}] ->
file:close(AccessFile),
case open_file(Filename) of
ok ->
ok;
_ ->
error
end
end,
loop(Filename);
stop ->
ok
end.
%%%----------------------------------------------------------------------
%%% BEHAVIOUR CALLBACKS
%%%----------------------------------------------------------------------
start(_Host, Opts) ->
ejabberd_ctl:register_commands([{"reopen-weblog", "reopen http fileserver log file"}],
?MODULE, ctl_process),
case gen_mod:get_opt(docroot, Opts, undefined) of
undefined ->
{'EXIT', {missing_document_root, ?MODULE}};
DocRoot ->
case filelib:is_dir(DocRoot) of
true ->
% XXX WARNING, using a single ets table name will
% not work with virtual hosts
ets:new(mod_http_fileserver, [named_table, public]),
ets:insert(mod_http_fileserver, [{docroot, DocRoot}]),
case gen_mod:get_opt(accesslog, Opts, undefined) of
undefined ->
ok;
Filename ->
% XXX same remark as above for proc name
register(mod_http_fileserver_server, spawn(?MODULE, loop, [Filename])),
open_file(Filename)
end;
_Else ->
{'EXIT', {unaccessible_document_root, ?MODULE}}
end
end.
stop(_Host) ->
case ets:info(mod_http_fileserver, name) of
undefined ->
ok;
_ ->
case ets:lookup(mod_http_fileserver, accessfile) of
undefined ->
ok;
[{accessfile, AccessFile}] ->
mod_http_fileserver_server ! stop,
file:close(AccessFile)
end,
ets:delete(mod_http_fileserver)
end,
ok.
|