]> granicus.if.org Git - ejabberd/commitdiff
Accept routes in Host header and map them to vhosts
authorEvgeniy Khramtsov <ekhramtsov@process-one.net>
Thu, 5 Oct 2017 07:26:10 +0000 (10:26 +0300)
committerEvgeniy Khramtsov <ekhramtsov@process-one.net>
Thu, 5 Oct 2017 07:26:10 +0000 (10:26 +0300)
src/mod_http_fileserver.erl

index c1c5b6461bc6884b8e8993841f5068b88494f737..b3e75c495c2f99fa91c769273d02202d14dcdb7b 100644 (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