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: httpd_acceptor.erl,v 1.1 2008/12/17 09:53:33 mikpe Exp $
18
-module(httpd_acceptor).
20
-include("httpd.hrl").
21
-include("httpd_verbosity.hrl").
25
-export([start_link/6]).
27
%% Other exports (for spawn's etc.)
28
-export([acceptor/4, acceptor/7]).
37
start_link(Manager, SocketType, Addr, Port, ConfigDb, Verbosity) ->
38
Args = [self(), Manager, SocketType, Addr, Port, ConfigDb, Verbosity],
39
proc_lib:start_link(?MODULE, acceptor, Args).
42
acceptor(Parent, Manager, SocketType, Addr, Port, ConfigDb, Verbosity) ->
44
put(verbosity,Verbosity),
46
case (catch do_init(SocketType, Addr, Port)) of
48
proc_lib:init_ack(Parent, {ok, self()}),
49
acceptor(Manager, SocketType, ListenSocket, ConfigDb);
51
proc_lib:init_ack(Parent, Error),
55
do_init(SocketType, Addr, Port) ->
56
do_socket_start(SocketType),
57
ListenSocket = do_socket_listen(SocketType, Addr, Port),
61
do_socket_start(SocketType) ->
62
case httpd_socket:start(SocketType) of
66
?vinfo("failed socket start: ~p",[Reason]),
67
throw({error, {socket_start_failed, Reason}})
71
do_socket_listen(SocketType, Addr, Port) ->
72
case httpd_socket:listen(SocketType, Addr, Port) of
74
?vinfo("failed socket listen operation: ~p", [Reason]),
75
throw({error, {listen, Reason}});
83
acceptor(Manager, SocketType, ListenSocket, ConfigDb) ->
84
?vdebug("await connection",[]),
85
case (catch httpd_socket:accept(SocketType, ListenSocket, 30000)) of
87
handle_error(Reason, ConfigDb, SocketType),
88
?MODULE:acceptor(Manager, SocketType, ListenSocket, ConfigDb);
91
handle_error({'EXIT', Reason}, ConfigDb, SocketType),
92
?MODULE:acceptor(Manager, SocketType, ListenSocket, ConfigDb);
95
handle_connection(Manager, ConfigDb, SocketType, Socket),
96
?MODULE:acceptor(Manager, SocketType, ListenSocket, ConfigDb)
100
handle_connection(Manager, ConfigDb, SocketType, Socket) ->
101
case httpd_request_handler:start_link(Manager, ConfigDb) of
103
httpd_socket:controlling_process(SocketType, Socket, Pid),
104
httpd_request_handler:synchronize(Pid, SocketType, Socket);
106
handle_connection_err(SocketType, Socket, ConfigDb, Reason)
110
handle_connection_err(SocketType, Socket, ConfigDb, Reason) ->
113
io_lib:format("failed starting request handler:~n ~p", [Reason])),
114
report_error(ConfigDb, String),
115
httpd_socket:close(SocketType, Socket).
118
handle_error(timeout, _, _) ->
119
?vtrace("Accept timeout",[]),
122
handle_error({enfile, _}, _, _) ->
123
?vinfo("Accept error: enfile",[]),
127
handle_error(emfile, _, _) ->
128
?vinfo("Accept error: emfile",[]),
129
%% Too many open files -> Out of sockets...
132
handle_error(closed, _, _) ->
133
?vlog("Accept error: closed",[]),
134
%% This propably only means that the application is stopping,
138
handle_error(econnaborted, _, _) ->
139
?vlog("Accept aborted",[]),
142
handle_error(esslaccept, _, _) ->
143
%% The user has selected to cancel the installation of
144
%% the certifikate, This is not a real error, so we do
145
%% not write an error message.
148
handle_error({'EXIT', Reason}, ConfigDb, SocketType) ->
149
?vinfo("Accept exit:~n ~p",[Reason]),
150
String = lists:flatten(io_lib:format("Accept exit: ~p", [Reason])),
151
accept_failed(SocketType, ConfigDb, String);
153
handle_error(Reason, ConfigDb, SocketType) ->
154
?vinfo("Accept error:~n ~p",[Reason]),
155
String = lists:flatten(io_lib:format("Accept error: ~p", [Reason])),
156
accept_failed(SocketType, ConfigDb, String).
159
accept_failed(SocketType, ConfigDb, String) ->
160
error_logger:error_report(String),
161
mod_log:error_log(SocketType, undefined, ConfigDb,
162
{0, "unknown"}, String),
163
mod_disk_log:error_log(SocketType, undefined, ConfigDb,
164
{0, "unknown"}, String),
165
exit({accept_failed, String}).
168
report_error(Db, String) ->
169
error_logger:error_report(String),
170
mod_log:report_error(Db, String),
171
mod_disk_log:report_error(Db, String).
174
sleep(T) -> receive after T -> ok end.