Skip to content

Commit

Permalink
inets: httpd avoid function_clause during startup
Browse files Browse the repository at this point in the history
- avoid function_clause httpd_request:body_data/2
- happening when httpd is stopped during TLS negotiation
- in such cases Manager process is killed and then continue_init fails
  • Loading branch information
u3s committed Jul 26, 2023
1 parent 0418c10 commit 074c707
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
14 changes: 12 additions & 2 deletions lib/inets/src/http_server/httpd_manager.erl
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,18 @@ report_error(State,String) ->
call(ServerRef, Request) ->
try gen_server:call(ServerRef, Request, infinity)
catch
exit:_ ->
{error, closed}
exit:Reason:Stacktrace ->
String =
lists:flatten(
io_lib:format(
"Request"
"~n ~p"
"~nto manager (~p) from ~p failed:"
"~n ~p"
"~n ~p",
[Request, ServerRef, self(), Reason, Stacktrace])),
error_logger:error_report(String),
{error, Reason}
end.

count_children(Sup) ->
Expand Down
56 changes: 32 additions & 24 deletions lib/inets/src/http_server/httpd_request_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,42 @@ continue_init(Manager, ConfigDB, SocketType, Socket, Peername, Sockname,
socket_type = SocketType,
socket = Socket,
init_data = InitData},
MaxHeaderSize = max_header_size(ConfigDB),
MaxURISize = max_uri_size(ConfigDB),
NrOfRequest = max_keep_alive_request(ConfigDB),

MaxHeaderSize = max_header_size(ConfigDB),
MaxURISize = max_uri_size(ConfigDB),
NrOfRequest = max_keep_alive_request(ConfigDB),
MaxContentLen = max_content_length(ConfigDB),
Customize = customize(ConfigDB),
MaxChunk = max_client_body_chunk(ConfigDB),

{_, Status} = httpd_manager:new_connection(Manager),

MFA = {httpd_request, parse, [[{max_uri, MaxURISize}, {max_header, MaxHeaderSize},
{max_version, ?HTTP_MAX_VERSION_STRING},
{max_method, ?HTTP_MAX_METHOD_STRING},
{max_content_length, MaxContentLen},
{customize, Customize}
]]},

State = #state{mod = Mod,
manager = Manager,
status = Status,
timeout = TimeOut,
max_keep_alive_request = NrOfRequest,
mfa = MFA,
chunk = chunk_start(MaxChunk)},
setopts(Socket, SocketType, [binary, {packet, 0}, {active, once}]),
NewState = data_receive_counter(activate_request_timeout(State), httpd_util:lookup(ConfigDB, minimum_bytes_per_second, false)),
gen_server:enter_loop(?MODULE, [], NewState).

{Result, Status} = httpd_manager:new_connection(Manager),
case Result of
error ->
httpd_util:error_log(ConfigDB,
httpd_logger:error_report('TLS', Status,
Mod, ?LOCATION)),
exit({shutdown, Status});
_ ->
MFA = {httpd_request, parse, [[{max_uri, MaxURISize}, {max_header, MaxHeaderSize},
{max_version, ?HTTP_MAX_VERSION_STRING},
{max_method, ?HTTP_MAX_METHOD_STRING},
{max_content_length, MaxContentLen},
{customize, Customize}
]]},

State = #state{mod = Mod,
manager = Manager,
status = Status,
timeout = TimeOut,
max_keep_alive_request = NrOfRequest,
mfa = MFA,
chunk = chunk_start(MaxChunk)},
setopts(Socket, SocketType, [binary, {packet, 0}, {active, once}]),
NewState =
data_receive_counter(activate_request_timeout(State),
httpd_util:lookup(ConfigDB, minimum_bytes_per_second, false)),
gen_server:enter_loop(?MODULE, [], NewState)
end.

%%====================================================================
%% gen_server callbacks
Expand Down

0 comments on commit 074c707

Please sign in to comment.