2009-06-16 15:53:02 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2009-06-16 15:52:07 +02:00
|
|
|
%%% File : mod_http_fileserver.erl
|
|
|
|
%%% Author : Massimiliano Mirra <mmirra [at] process-one [dot] net>
|
|
|
|
%%% Purpose : Simple file server plugin for embedded ejabberd web server
|
2009-06-16 15:53:18 +02:00
|
|
|
%%% Created :
|
|
|
|
%%%
|
|
|
|
%%%
|
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 ProcessOne
|
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2009-06-16 15:52:07 +02:00
|
|
|
-module(mod_http_fileserver).
|
|
|
|
-author('mmirra@process-one.net').
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
2009-06-16 15:53:02 +02:00
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
%% gen_mod callbacks
|
|
|
|
-export([start/2, stop/1]).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([start_link/2]).
|
|
|
|
|
|
|
|
%% gen_server callbacks
|
|
|
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
|
|
|
terminate/2, code_change/3]).
|
|
|
|
|
|
|
|
%% request_handlers callbacks
|
|
|
|
-export([process/2]).
|
2009-06-16 15:52:07 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
%% ejabberd_hooks callbacks
|
|
|
|
-export([reopen_log/1]).
|
2009-06-16 15:52:07 +02:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
-include("jlib.hrl").
|
2009-06-16 15:52:42 +02:00
|
|
|
-include_lib("kernel/include/file.hrl").
|
2009-06-16 15:52:07 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
%%-include("ejabberd_http.hrl").
|
|
|
|
%% TODO: When ejabberd-modules SVN gets the new ejabberd_http.hrl, delete this code:
|
|
|
|
-record(request, {method,
|
|
|
|
path,
|
|
|
|
q = [],
|
|
|
|
us,
|
|
|
|
auth,
|
|
|
|
lang = "",
|
|
|
|
data = "",
|
|
|
|
ip,
|
|
|
|
host, % string()
|
|
|
|
port, % integer()
|
|
|
|
tp, % transfer protocol = http | https
|
|
|
|
headers
|
|
|
|
}).
|
|
|
|
|
2009-07-21 19:32:29 +02:00
|
|
|
-record(state, {host, docroot, accesslog, accesslogfd, directory_indices,
|
|
|
|
default_content_type, content_types = []}).
|
2009-06-16 15:53:02 +02:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_http_fileserver).
|
|
|
|
|
2009-06-16 15:53:13 +02:00
|
|
|
%% Response is {DataSize, Code, [{HeaderKey, HeaderValue}], Data}
|
|
|
|
-define(HTTP_ERR_FILE_NOT_FOUND, {-1, 404, [], "Not found"}).
|
|
|
|
-define(HTTP_ERR_FORBIDDEN, {-1, 403, [], "Forbidden"}).
|
|
|
|
|
2009-07-21 19:32:29 +02:00
|
|
|
-define(DEFAULT_CONTENT_TYPE, "application/octet-stream").
|
|
|
|
-define(DEFAULT_CONTENT_TYPES, [{".css", "text/css"},
|
|
|
|
{".gif", "image/gif"},
|
|
|
|
{".html", "text/html"},
|
|
|
|
{".jar", "application/java-archive"},
|
|
|
|
{".jpeg", "image/jpeg"},
|
|
|
|
{".jpg", "image/jpeg"},
|
|
|
|
{".js", "text/javascript"},
|
|
|
|
{".png", "image/png"},
|
|
|
|
{".txt", "text/plain"},
|
|
|
|
{".xpi", "application/x-xpinstall"},
|
|
|
|
{".xul", "application/vnd.mozilla.xul+xml"}]).
|
|
|
|
|
2009-06-16 15:53:13 +02:00
|
|
|
-compile(export_all).
|
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
%%====================================================================
|
|
|
|
%% gen_mod callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
start(Host, Opts) ->
|
|
|
|
Proc = get_proc_name(Host),
|
|
|
|
ChildSpec =
|
|
|
|
{Proc,
|
|
|
|
{?MODULE, start_link, [Host, Opts]},
|
2009-07-21 19:32:29 +02:00
|
|
|
transient, % if process crashes abruptly, it gets restarted
|
2009-06-16 15:53:02 +02:00
|
|
|
1000,
|
|
|
|
worker,
|
|
|
|
[?MODULE]},
|
|
|
|
supervisor:start_child(ejabberd_sup, ChildSpec).
|
|
|
|
|
|
|
|
stop(Host) ->
|
|
|
|
Proc = get_proc_name(Host),
|
|
|
|
gen_server:call(Proc, stop),
|
|
|
|
supervisor:terminate_child(ejabberd_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_sup, Proc).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% API
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
|
|
|
|
%% Description: Starts the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = get_proc_name(Host),
|
|
|
|
gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []).
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% gen_server callbacks
|
|
|
|
%%====================================================================
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: init(Args) -> {ok, State} |
|
|
|
|
%% {ok, State, Timeout} |
|
|
|
|
%% ignore |
|
|
|
|
%% {stop, Reason}
|
|
|
|
%% Description: Initiates the server
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
init([Host, Opts]) ->
|
|
|
|
try initialize(Host, Opts) of
|
2009-07-21 19:32:29 +02:00
|
|
|
{DocRoot, AccessLog, AccessLogFD, DirectoryIndices,
|
|
|
|
DefaultContentType, ContentTypes} ->
|
2009-06-16 15:53:02 +02:00
|
|
|
{ok, #state{host = Host,
|
|
|
|
accesslog = AccessLog,
|
|
|
|
accesslogfd = AccessLogFD,
|
2009-06-16 15:53:13 +02:00
|
|
|
docroot = DocRoot,
|
2009-07-21 19:32:29 +02:00
|
|
|
directory_indices = DirectoryIndices,
|
|
|
|
default_content_type = DefaultContentType,
|
|
|
|
content_types = ContentTypes}}
|
2009-06-16 15:53:02 +02:00
|
|
|
catch
|
|
|
|
throw:Reason ->
|
|
|
|
{stop, Reason}
|
|
|
|
end.
|
|
|
|
|
|
|
|
initialize(Host, Opts) ->
|
|
|
|
DocRoot = gen_mod:get_opt(docroot, Opts, undefined),
|
|
|
|
check_docroot_defined(DocRoot, Host),
|
|
|
|
DRInfo = check_docroot_exists(DocRoot),
|
|
|
|
check_docroot_is_dir(DRInfo, DocRoot),
|
|
|
|
check_docroot_is_readable(DRInfo, DocRoot),
|
|
|
|
AccessLog = gen_mod:get_opt(accesslog, Opts, undefined),
|
|
|
|
AccessLogFD = try_open_log(AccessLog, Host),
|
2009-06-16 15:53:13 +02:00
|
|
|
DirectoryIndices = gen_mod:get_opt(directory_indices, Opts, []),
|
2009-07-21 19:32:29 +02:00
|
|
|
DefaultContentType = gen_mod:get_opt(default_content_type, Opts,
|
|
|
|
?DEFAULT_CONTENT_TYPE),
|
|
|
|
ContentTypes = build_list_content_types(gen_mod:get_opt(content_types, Opts, []), ?DEFAULT_CONTENT_TYPES),
|
|
|
|
?INFO_MSG("initialize: ~n ~p", [ContentTypes]),%+++
|
|
|
|
{DocRoot, AccessLog, AccessLogFD, DirectoryIndices,
|
|
|
|
DefaultContentType, ContentTypes}.
|
|
|
|
|
|
|
|
%% @spec (AdminCTs::[CT], Default::[CT]) -> [CT]
|
|
|
|
%% where CT = {Extension::string(), Value}
|
|
|
|
%% Value = string() | undefined
|
|
|
|
%% Returns a unified list without duplicates where elements of AdminCTs have more priority.
|
|
|
|
%% If a CT is declared as 'undefined', then it is not included in the result.
|
|
|
|
build_list_content_types(AdminCTsUnsorted, DefaultCTsUnsorted) ->
|
|
|
|
AdminCTs = lists:ukeysort(1, AdminCTsUnsorted),
|
|
|
|
DefaultCTs = lists:ukeysort(1, DefaultCTsUnsorted),
|
|
|
|
CTsUnfiltered = lists:ukeymerge(1, AdminCTs, DefaultCTs),
|
|
|
|
[{Extension, Value} || {Extension, Value} <- CTsUnfiltered, Value /= undefined].
|
2009-06-16 15:53:02 +02:00
|
|
|
|
|
|
|
check_docroot_defined(DocRoot, Host) ->
|
|
|
|
case DocRoot of
|
|
|
|
undefined -> throw({undefined_docroot_option, Host});
|
|
|
|
_ -> ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_docroot_exists(DocRoot) ->
|
|
|
|
case file:read_file_info(DocRoot) of
|
|
|
|
{error, Reason} -> throw({error_access_docroot, DocRoot, Reason});
|
|
|
|
{ok, FI} -> FI
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_docroot_is_dir(DRInfo, DocRoot) ->
|
|
|
|
case DRInfo#file_info.type of
|
|
|
|
directory -> ok;
|
|
|
|
_ -> throw({docroot_not_directory, DocRoot})
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_docroot_is_readable(DRInfo, DocRoot) ->
|
|
|
|
case DRInfo#file_info.access of
|
|
|
|
read -> ok;
|
|
|
|
read_write -> ok;
|
|
|
|
_ -> throw({docroot_not_readable, DocRoot})
|
|
|
|
end.
|
2009-06-16 15:52:07 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
try_open_log(undefined, _Host) ->
|
|
|
|
undefined;
|
|
|
|
try_open_log(FN, Host) ->
|
|
|
|
FD = try open_log(FN) of
|
|
|
|
FD1 -> FD1
|
|
|
|
catch
|
|
|
|
throw:{cannot_open_accesslog, FN, Reason} ->
|
|
|
|
?ERROR_MSG("Cannot open access log file: ~p~nReason: ~p", [FN, Reason]),
|
|
|
|
undefined
|
|
|
|
end,
|
2009-07-21 19:32:29 +02:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:add(reopen_log_hook, HostB, ?MODULE, reopen_log, 50),
|
2009-06-16 15:53:02 +02:00
|
|
|
FD.
|
2009-06-16 15:52:12 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_call(Request, From, State) -> {reply, Reply, State} |
|
|
|
|
%% {reply, Reply, State, Timeout} |
|
|
|
|
%% {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, Reply, State} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling call messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_call({serve, LocalPath}, _From, State) ->
|
2009-07-21 19:32:29 +02:00
|
|
|
Reply = serve(LocalPath, State#state.docroot, State#state.directory_indices,
|
|
|
|
State#state.default_content_type, State#state.content_types),
|
2009-06-16 15:53:02 +02:00
|
|
|
{reply, Reply, State};
|
|
|
|
handle_call(_Request, _From, State) ->
|
|
|
|
{reply, ok, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_cast(Msg, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
2009-06-16 15:53:13 +02:00
|
|
|
handle_cast({add_to_log, FileSize, Code, Request}, State) ->
|
|
|
|
add_to_log(State#state.accesslogfd, FileSize, Code, Request),
|
2009-06-16 15:53:02 +02:00
|
|
|
{noreply, State};
|
|
|
|
handle_cast(reopen_log, State) ->
|
|
|
|
FD2 = reopen_log(State#state.accesslog, State#state.accesslogfd),
|
|
|
|
{noreply, State#state{accesslogfd = FD2}};
|
|
|
|
handle_cast(_Msg, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: handle_info(Info, State) -> {noreply, State} |
|
|
|
|
%% {noreply, State, Timeout} |
|
|
|
|
%% {stop, Reason, State}
|
|
|
|
%% Description: Handling all non call/cast messages
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
handle_info(_Info, State) ->
|
|
|
|
{noreply, State}.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Function: terminate(Reason, State) -> void()
|
|
|
|
%% Description: This function is called by a gen_server when it is about to
|
|
|
|
%% terminate. It should be the opposite of Module:init/1 and do any necessary
|
|
|
|
%% cleaning up. When it returns, the gen_server terminates with Reason.
|
|
|
|
%% The return value is ignored.
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
terminate(_Reason, State) ->
|
|
|
|
close_log(State#state.accesslogfd),
|
|
|
|
ejabberd_hooks:delete(reopen_log_hook, State#state.host, ?MODULE, reopen_log, 50),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
|
|
|
|
%% Description: Convert process state when code is changed
|
|
|
|
%%--------------------------------------------------------------------
|
|
|
|
code_change(_OldVsn, State, _Extra) ->
|
|
|
|
{ok, State}.
|
|
|
|
|
|
|
|
%%====================================================================
|
|
|
|
%% request_handlers callbacks
|
|
|
|
%%====================================================================
|
|
|
|
|
|
|
|
%% @spec (LocalPath, Request) -> {HTTPCode::integer(), [Header], Page::string()}
|
|
|
|
%% @doc Handle an HTTP request.
|
|
|
|
%% LocalPath is the part of the requested URL path that is "local to the module".
|
|
|
|
%% Returns the page to be sent back to the client and/or HTTP status code.
|
2009-06-16 15:52:12 +02:00
|
|
|
process(LocalPath, Request) ->
|
2009-06-16 15:52:07 +02:00
|
|
|
?DEBUG("Requested ~p", [LocalPath]),
|
2009-06-16 15:53:02 +02:00
|
|
|
try gen_server:call(get_proc_name(Request#request.host), {serve, LocalPath}) of
|
2009-06-16 15:53:13 +02:00
|
|
|
{FileSize, Code, Headers, Contents} ->
|
|
|
|
add_to_log(FileSize, Code, Request),
|
|
|
|
{Code, Headers, Contents}
|
2009-06-16 15:53:02 +02:00
|
|
|
catch
|
|
|
|
exit:{noproc, _} ->
|
|
|
|
ejabberd_web:error(not_found)
|
|
|
|
end.
|
2009-06-16 15:52:12 +02:00
|
|
|
|
2009-07-21 19:32:29 +02:00
|
|
|
serve(LocalPath, DocRoot, DirectoryIndices, DefaultContentType, ContentTypes) ->
|
2009-06-16 15:52:07 +02:00
|
|
|
FileName = filename:join(filename:split(DocRoot) ++ LocalPath),
|
2009-06-16 15:53:13 +02:00
|
|
|
case file:read_file_info(FileName) of
|
|
|
|
{error, enoent} -> ?HTTP_ERR_FILE_NOT_FOUND;
|
|
|
|
{error, eacces} -> ?HTTP_ERR_FORBIDDEN;
|
2009-07-21 19:32:29 +02:00
|
|
|
{ok, #file_info{type = directory}} -> serve_index(FileName,
|
|
|
|
DirectoryIndices,
|
|
|
|
DefaultContentType,
|
|
|
|
ContentTypes);
|
|
|
|
{ok, FileInfo} -> serve_file(FileInfo, FileName,
|
|
|
|
DefaultContentType,
|
|
|
|
ContentTypes)
|
2009-06-16 15:53:13 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% Troll through the directory indices attempting to find one which
|
|
|
|
%% works, if none can be found, return a 404.
|
2009-07-21 19:32:29 +02:00
|
|
|
serve_index(_FileName, [], _DefaultContentType, _ContentTypes) ->
|
2009-06-16 15:53:13 +02:00
|
|
|
?HTTP_ERR_FILE_NOT_FOUND;
|
2009-07-21 19:32:29 +02:00
|
|
|
serve_index(FileName, [Index | T], DefaultContentType, ContentTypes) ->
|
2009-06-16 15:53:13 +02:00
|
|
|
IndexFileName = filename:join([FileName] ++ [Index]),
|
|
|
|
case file:read_file_info(IndexFileName) of
|
2009-07-21 19:32:29 +02:00
|
|
|
{error, _Error} -> serve_index(FileName, T, DefaultContentType, ContentTypes);
|
|
|
|
{ok, #file_info{type = directory}} -> serve_index(FileName, T, DefaultContentType, ContentTypes);
|
|
|
|
{ok, FileInfo} -> serve_file(FileInfo, IndexFileName, DefaultContentType, ContentTypes)
|
2009-06-16 15:52:07 +02:00
|
|
|
end.
|
|
|
|
|
2009-06-16 15:53:13 +02:00
|
|
|
%% Assume the file exists if we got this far and attempt to read it in
|
|
|
|
%% and serve it up.
|
2009-07-21 19:32:29 +02:00
|
|
|
serve_file(FileInfo, FileName, DefaultContentType, ContentTypes) ->
|
2009-06-16 15:53:13 +02:00
|
|
|
?DEBUG("Delivering: ~s", [FileName]),
|
|
|
|
{ok, FileContents} = file:read_file(FileName),
|
2009-07-21 19:32:29 +02:00
|
|
|
ContentType = content_type(FileName, DefaultContentType, ContentTypes),
|
2009-06-16 15:53:13 +02:00
|
|
|
{FileInfo#file_info.size,
|
|
|
|
200, [{"Server", "ejabberd"},
|
|
|
|
{"Last-Modified", last_modified(FileInfo)},
|
2009-07-21 19:32:29 +02:00
|
|
|
{"Content-Type", ContentType}],
|
2009-06-16 15:53:13 +02:00
|
|
|
FileContents}.
|
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Log file
|
|
|
|
%%----------------------------------------------------------------------
|
2009-06-16 15:52:07 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
open_log(FN) ->
|
|
|
|
case file:open(FN, [append]) of
|
|
|
|
{ok, FD} ->
|
|
|
|
FD;
|
|
|
|
{error, Reason} ->
|
|
|
|
throw({cannot_open_accesslog, FN, Reason})
|
|
|
|
end.
|
2009-06-16 15:52:07 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
close_log(FD) ->
|
|
|
|
file:close(FD).
|
|
|
|
|
|
|
|
reopen_log(undefined, undefined) ->
|
|
|
|
ok;
|
|
|
|
reopen_log(FN, FD) ->
|
|
|
|
close_log(FD),
|
|
|
|
open_log(FN).
|
2009-06-16 15:52:12 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
reopen_log(Host) ->
|
|
|
|
gen_server:cast(get_proc_name(Host), reopen_log).
|
|
|
|
|
2009-06-16 15:53:13 +02:00
|
|
|
add_to_log(FileSize, Code, Request) ->
|
2009-06-16 15:53:02 +02:00
|
|
|
gen_server:cast(get_proc_name(Request#request.host),
|
2009-06-16 15:53:13 +02:00
|
|
|
{add_to_log, FileSize, Code, Request}).
|
2009-06-16 15:53:02 +02:00
|
|
|
|
2009-06-16 15:53:13 +02:00
|
|
|
add_to_log(undefined, _FileSize, _Code, _Request) ->
|
2009-06-16 15:53:02 +02:00
|
|
|
ok;
|
2009-06-16 15:53:13 +02:00
|
|
|
add_to_log(File, FileSize, Code, Request) ->
|
2009-06-16 15:52:12 +02:00
|
|
|
{{Year, Month, Day}, {Hour, Minute, Second}} = calendar:local_time(),
|
2009-06-16 15:53:13 +02:00
|
|
|
IP = ip_to_string(element(1, Request#request.ip)),
|
2009-06-16 15:52:12 +02:00
|
|
|
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
|
2009-06-16 15:53:02 +02:00
|
|
|
[] ->
|
2009-06-16 15:52:12 +02:00
|
|
|
"";
|
|
|
|
String ->
|
|
|
|
[$? | String]
|
|
|
|
end,
|
2009-06-16 15:53:13 +02:00
|
|
|
UserAgent = find_header('User-Agent', Request#request.headers, "-"),
|
|
|
|
Referer = find_header('Referer', Request#request.headers, "-"),
|
2009-06-16 15:53:02 +02:00
|
|
|
%% Pseudo Combined Apache log format:
|
|
|
|
%% 127.0.0.1 - - [28/Mar/2007:18:41:55 +0200] "GET / HTTP/1.1" 302 303 "-" "tsung"
|
|
|
|
%% TODO some fields are harcoded/missing:
|
|
|
|
%% The date/time integers should have always 2 digits. For example day "7" should be "07"
|
|
|
|
%% Month should be 3*letter, not integer 1..12
|
|
|
|
%% Missing time zone = (`+' | `-') 4*digit
|
|
|
|
%% Missing protocol version: HTTP/1.1
|
|
|
|
%% For reference: http://httpd.apache.org/docs/2.2/logs.html
|
2009-06-16 15:53:13 +02:00
|
|
|
io:format(File, "~s - - [~p/~p/~p:~p:~p:~p] \"~s /~s~s\" ~p ~p ~p ~p~n",
|
|
|
|
[IP, Day, Month, Year, Hour, Minute, Second, Request#request.method, Path, Query, Code,
|
|
|
|
FileSize, Referer, UserAgent]).
|
|
|
|
|
|
|
|
find_header(Header, Headers, Default) ->
|
|
|
|
case lists:keysearch(Header, 1, Headers) of
|
|
|
|
{value, {_, Value}} -> Value;
|
|
|
|
false -> Default
|
|
|
|
end.
|
2009-06-16 15:52:12 +02:00
|
|
|
|
2009-06-16 15:53:02 +02:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Utilities
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
get_proc_name(Host) -> gen_mod:get_module_proc(Host, ?PROCNAME).
|
|
|
|
|
|
|
|
join([], _) ->
|
|
|
|
"";
|
|
|
|
join([E], _) ->
|
|
|
|
E;
|
|
|
|
join([H | T], Separator) ->
|
|
|
|
lists:foldl(fun(E, Acc) -> lists:concat([Acc, Separator, E]) end, H, T).
|
|
|
|
|
2009-07-21 19:32:29 +02:00
|
|
|
content_type(Filename, DefaultContentType, ContentTypes) ->
|
2009-08-15 22:10:49 +02:00
|
|
|
Extension = string:to_lower(filename:extension(Filename)),
|
2009-07-21 19:32:29 +02:00
|
|
|
case lists:keysearch(Extension, 1, ContentTypes) of
|
|
|
|
{value, {_, ContentType}} -> ContentType;
|
|
|
|
false -> DefaultContentType
|
2009-06-16 15:52:07 +02:00
|
|
|
end.
|
|
|
|
|
2009-06-16 15:53:13 +02:00
|
|
|
last_modified(FileInfo) ->
|
2009-06-16 15:52:42 +02:00
|
|
|
Then = FileInfo#file_info.mtime,
|
|
|
|
httpd_util:rfc1123_date(Then).
|
2009-06-16 15:53:13 +02:00
|
|
|
|
|
|
|
%% Convert IP address tuple to string representation. Accepts either
|
|
|
|
%% IPv4 or IPv6 address tuples.
|
|
|
|
ip_to_string(Address) when size(Address) == 4 ->
|
|
|
|
join(tuple_to_list(Address), ".");
|
|
|
|
ip_to_string(Address) when size(Address) == 8 ->
|
|
|
|
Parts = lists:map(fun (Int) -> io_lib:format("~.16B", [Int]) end, tuple_to_list(Address)),
|
2009-08-15 22:10:49 +02:00
|
|
|
string:to_lower(lists:flatten(join(Parts, ":"))).
|