Accept routes in Host header and map them to vhosts

This commit is contained in:
Evgeniy Khramtsov 2017-10-05 10:26:10 +03:00
parent 9e95150803
commit bd06bc00e2
1 changed files with 20 additions and 13 deletions

View File

@ -178,10 +178,15 @@ check_docroot_defined(DocRoot, Host) ->
end.
check_docroot_exists(DocRoot) ->
case file:read_file_info(DocRoot) of
{error, Reason} ->
throw({error_access_docroot, DocRoot, Reason});
{ok, FI} -> FI
case filelib:ensure_dir(filename:join(DocRoot, "foo")) of
ok ->
case file:read_file_info(DocRoot) of
{error, Reason} ->
throw({error_access_docroot, DocRoot, Reason});
{ok, FI} -> FI
end;
{error, Reason} ->
throw({error_access_docroot, DocRoot, Reason})
end.
check_docroot_is_dir(DRInfo, DocRoot) ->
@ -297,18 +302,20 @@ code_change(_OldVsn, State, _Extra) ->
%% Returns the page to be sent back to the client and/or HTTP status code.
process(LocalPath, #request{host = Host, auth = Auth, headers = RHeaders} = Request) ->
?DEBUG("Requested ~p", [LocalPath]),
try gen_server:call(get_proc_name(Host), {serve, LocalPath, Auth, RHeaders}) of
{FileSize, Code, Headers, Contents} ->
add_to_log(FileSize, Code, Request),
{Code, Headers, Contents}
catch
exit:{noproc, _} ->
?ERROR_MSG("Received an HTTP request with Host ~p, but couldn't find the related "
"ejabberd virtual host", [Request#request.host]),
try
VHost = ejabberd_router:host_of_route(Host),
{FileSize, Code, Headers, Contents} =
gen_server:call(get_proc_name(VHost),
{serve, LocalPath, Auth, RHeaders}),
add_to_log(FileSize, Code, Request#request{host = VHost}),
{Code, Headers, Contents}
catch _:{Why, _} when Why == noproc; Why == invalid_domain; Why == unregistered_route ->
?ERROR_MSG("Received an HTTP request with Host: ~s, "
"but couldn't find the related "
"ejabberd virtual host", [Host]),
ejabberd_web:error(not_found)
end.
serve(LocalPath, Auth, DocRoot, DirectoryIndices, CustomHeaders, DefaultContentType,
ContentTypes, UserAccess, IfModifiedSince) ->
CanProceed = case {UserAccess, Auth} of