Skip to content

Commit

Permalink
Merge branch 'maint'
Browse files Browse the repository at this point in the history
  • Loading branch information
u3s committed Aug 10, 2023
2 parents 87d24fe + 68b2df7 commit 0dfe6d9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 48 deletions.
16 changes: 8 additions & 8 deletions lib/inets/src/http_server/httpd_logger.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ error_report(Protocol, Reason, #mod{init_data = #init_data{peername = PeerName,
Location) ->
ServerName = httpd_util:lookup(Db, server_name),
Report0 = #{protocol => Protocol,
reason => Reason,
peer => PeerName,
host => SockName,
server_name => ServerName,
metadata => Location},
reason => Reason,
peer => PeerName,
host => SockName,
server_name => ServerName,
metadata => Location},
Report1 = case URI of
undefined ->
Report0;
Expand All @@ -46,11 +46,11 @@ error_report(Protocol, Reason, #mod{init_data = #init_data{peername = PeerName,
end,
case Protocol of
'HTTP' ->
Report1#{transport => transport_type(Type)};
Report1#{transport => transport_type(Type)};
_ ->
Report1
end.
end.

log(Level, #{metadata := MetaData} = Report, Domain) ->
logger:log(Level, maps:without([metadata], Report),
MetaData#{domain => [otp,inets, httpd, Domain, Level],
Expand Down
7 changes: 6 additions & 1 deletion lib/inets/src/http_server/httpd_request_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ continue_init(Manager, ConfigDB, SocketType, Socket, Peername, Sockname,
{Result, Status} = httpd_manager:new_connection(Manager),
case Result of
error ->
%% this error might happen when httpd manager is stopped
%% during execution of httpd_transport:negotiate; this is
%% most likely to happen for TLS requiring more processing
%% 'HTTP' as error category(Protocol) because transport
%% information is wanted in logs
httpd_util:error_log(ConfigDB,
httpd_logger:error_report('TLS', Status,
httpd_logger:error_report('HTTP', Status,
Mod, ?LOCATION)),
exit({shutdown, Status});
_ ->
Expand Down
83 changes: 44 additions & 39 deletions lib/inets/src/http_server/mod_esi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@

-define(VMODULE,"ESI").
-define(DEFAULT_ERL_TIMEOUT,15).

-define(ERROR_404,
[{status, {404, ModData#mod.request_uri, "Not found"}} |
ModData#mod.data]).

%%%=========================================================================
%%% API
Expand Down Expand Up @@ -220,47 +222,50 @@ erl(#mod{method = "POST", entity_body = Body} = ModData, ESIBody, Modules) ->
{proceed,[{status, {400, none, BadRequest}} | ModData#mod.data]}
end.

generate_webpage(ModData, ESIBody, [all], Module, FunctionName,
Input, ScriptElements) ->
try
ModuleAtom = list_to_existing_atom(Module),
generate_webpage(ModData, ESIBody, [ModuleAtom], Module,
FunctionName, Input, ScriptElements)
catch
_:_ ->
{proceed, [{status, {404, ModData#mod.request_uri, "Not found"}}
| ModData#mod.data]}
end;
generate_webpage(ModData, ESIBody, Modules, Module, Function,
Input, ScriptElements) when is_atom(Module), is_atom(Function) ->
case lists:member(Module, Modules) of
true ->
Env = httpd_script_env:create_env(esi, ModData, ScriptElements),
case erl_scheme_webpage_chunk(Module, Function,
Env, Input, ModData) of
{error, erl_scheme_webpage_chunk_undefined} ->
{proceed, [{status, {404, ModData#mod.request_uri, "Not found"}}
| ModData#mod.data]};
ResponseResult ->
ResponseResult
end;
false ->
{proceed, [{status, {403, ModData#mod.request_uri,
?NICE("Client not authorized to evaluate: "
++ ESIBody)}} | ModData#mod.data]}
end;
generate_webpage(ModData, ESIBody, Modules, ModuleName, FunctionName,
Input, ScriptElements) ->
generate_webpage(ModData, ESIBody, AllowedModules0, ModuleString, FunctionString,
Input, ScriptElements)
when is_list(ModuleString), is_list(FunctionString) ->
case convert_to_atoms(ModuleString, FunctionString, ModData) of
{ok, Module, Function} ->
verify_module(ModData, ESIBody, AllowedModules0, Module, Function,
Input, ScriptElements);
Result ->
Result
end.

convert_to_atoms(ModuleString, FunctionString, ModData) ->
try
Module = list_to_existing_atom(ModuleName),
Module = list_to_existing_atom(ModuleString),
_ = code:ensure_loaded(Module),
Function = list_to_existing_atom(FunctionName),
generate_webpage(ModData, ESIBody, Modules, Module, Function,
Input, ScriptElements)
Function = list_to_existing_atom(FunctionString),
{ok, Module, Function}
catch
_:_ ->
{proceed, [{status, {404, ModData#mod.request_uri, "Not found"}}
| ModData#mod.data]}
error:badarg:_Stacktrace ->
{proceed, ?ERROR_404}
end.

verify_module(ModData, _ESIBody, [all], Module, Function, Input, ScriptElements) ->
do_generate_webpage(ModData, Module, Function, Input, ScriptElements);
verify_module(ModData, ESIBody, Allowed, Module, Function, Input, ScriptElements) ->
case lists:member(Module, Allowed) of
true ->
do_generate_webpage(ModData, Module, Function, Input, ScriptElements);
_ ->
Error403 =
[{status,
{403, ModData#mod.request_uri,
?NICE("Client not authorized to evaluate: " ++ ESIBody)}} |
ModData#mod.data],
{proceed, Error403}
end.

do_generate_webpage(ModData, Module, Function, Input, ScriptElements) ->
Env = httpd_script_env:create_env(esi, ModData, ScriptElements),
case erl_scheme_webpage_chunk(Module, Function, Env, Input, ModData) of
{error, erl_scheme_webpage_chunk_undefined} ->
{proceed, ?ERROR_404};
ResponseResult ->
ResponseResult
end.

%% API that allows the dynamic wepage to be sent back to the client
Expand Down

0 comments on commit 0dfe6d9

Please sign in to comment.