From 6db50940a15c6e5c53b3f6bf0062d6969da011ed Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Sun, 23 Aug 2015 17:47:19 +0200 Subject: [PATCH] mod_http_upload: Expand @HOME@ and @HOST@ keywords Replace @HOST@ with the virtual host name if the keyword is specified within the "put_url", "get_url", and/or "service_url" values. Also, replace @HOME@ with the home directory of the user running ejabberd if this keyword is specified within the "docroot" value. --- mod_http_upload/README.txt | 16 +++++++------ mod_http_upload/src/mod_http_upload.erl | 31 ++++++++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/mod_http_upload/README.txt b/mod_http_upload/README.txt index 1395fe2..f5a7919 100644 --- a/mod_http_upload/README.txt +++ b/mod_http_upload/README.txt @@ -83,28 +83,30 @@ The configurable mod_http_upload options are: Uploaded files are stored below the directory specified (as an absolute path) with this option. It is mandatory to specify either this option or - the 'service_url' option. + the 'service_url' option. The keyword @HOME@ is replaced with the home + directory of the user running ejabberd. - put_url (default: "https://@HOST@:5443") This option specifies the initial part of the PUT URLs used for file - uploads. Note that @HOST@ can NOT be specified for this option in the - configuration file, but the virtual host name is used as part of the URL - by default. + uploads. The keyword @HOST@ is replaced with the virtual host name. - get_url (default: $put_url) This option specifies the initial part of the GET URLs used for downloading the files. By default, it is set to the same value as the 'put_url', but you can set it to a different value in order to have the - files served by a proper HTTP server such as Nginx or Apache. + files served by a proper HTTP server such as Nginx or Apache. The keyword + @HOST@ is replaced with the virtual host name. - service_url (default: 'undefined') If a 'service_url' is specified, HTTP upload slot requests are forwarded to this external service instead of being handled by mod_http_upload - itself. An HTTP GET query such as the following is issued whenever an - HTTP upload slot request is accepted as per the 'access' rule: + itself. The keyword @HOST@ is replaced with the virtual host name. + + An HTTP GET query such as the following is issued whenever an HTTP upload + slot request is accepted as per the 'access' rule: http://localhost:5444/?jid=juliet%40example.com&size=10240&name=example.jpg diff --git a/mod_http_upload/src/mod_http_upload.erl b/mod_http_upload/src/mod_http_upload.erl index 26366ae..93cdf06 100644 --- a/mod_http_upload/src/mod_http_upload.erl +++ b/mod_http_upload/src/mod_http_upload.erl @@ -113,10 +113,10 @@ start(ServerHost, Opts) -> PutURL = gen_mod:get_opt(put_url, Opts, fun(<<"http://", _/binary>> = URL) -> URL; (<<"https://", _/binary>> = URL) -> URL; - (_) -> <<"https://", ServerHost/binary>> + (_) -> <<"https://@HOST@">> end, - <<"https://", ServerHost/binary>>), - [_, ProcHost | _] = binary:split(PutURL, + <<"https://@HOST@">>), + [_, ProcHost | _] = binary:split(expand_host(PutURL, ServerHost), [<<"http://">>, <<"https://">>, <<":">>, <<"/">>], [global]), Proc = gen_mod:get_module_proc(ProcHost, ?PROCNAME), @@ -218,7 +218,7 @@ init({ServerHost, Opts}) -> fun(<<"http://", _/binary>> = URL) -> URL; (<<"https://", _/binary>> = URL) -> URL end, - <<"https://", ServerHost/binary, ":5443">>), + <<"https://@HOST@:5443">>), GetURL = gen_mod:get_opt(get_url, Opts, fun(<<"http://", _/binary>> = URL) -> URL; (<<"https://", _/binary>> = URL) -> URL @@ -251,10 +251,10 @@ init({ServerHost, Opts}) -> {ok, #state{server_host = ServerHost, host = Host, name = Name, access = Access, max_size = MaxSize, secret_length = SecretLength, jid_in_url = JIDinURL, - docroot = DocRoot, - put_url = str:strip(PutURL, right, $/), - get_url = str:strip(GetURL, right, $/), - service_url = ServiceURL}}. + docroot = expand_home(DocRoot), + put_url = expand_host(str:strip(PutURL, right, $/), ServerHost), + get_url = expand_host(str:strip(GetURL, right, $/), ServerHost), + service_url = expand_host(ServiceURL, ServerHost)}}. -spec handle_call(_, {pid(), _}, state()) -> {noreply, state()}. @@ -596,6 +596,19 @@ map_int_to_char(N) when N =< 9 -> N + 48; % Digit. map_int_to_char(N) when N =< 35 -> N + 55; % Upper-case character. map_int_to_char(N) when N =< 61 -> N + 61. % Lower-case character. +-spec expand_home(binary()) -> binary(). + +expand_home(Subject) -> + {ok, [[Home]]} = init:get_argument(home), + Parts = binary:split(Subject, <<"@HOME@">>, [global]), + str:join(Parts, list_to_binary(Home)). + +-spec expand_host(binary(), binary()) -> binary(). + +expand_host(Subject, Host) -> + Parts = binary:split(Subject, <<"@HOST@">>, [global]), + str:join(Parts, Host). + -spec yield_content_type(binary()) -> binary(). yield_content_type(<<"">>) -> <<"application/octet-stream">>; @@ -692,7 +705,7 @@ remove_user(User, Server) -> sha1), UserStr = make_user_string(<>, JIDinURL), - UserDir = str:join([DocRoot, UserStr], <<$/>>), + UserDir = str:join([expand_home(DocRoot), UserStr], <<$/>>), case del_tree(UserDir) of ok -> ?INFO_MSG("Removed HTTP upload directory of ~s@~s",