]> granicus.if.org Git - ejabberd/commitdiff
ejabberd_http: Expand @VERSION@ in custom headers
authorHolger Weiss <holger@zedat.fu-berlin.de>
Mon, 27 Mar 2017 21:52:49 +0000 (23:52 +0200)
committerHolger Weiss <holger@zedat.fu-berlin.de>
Mon, 27 Mar 2017 21:52:49 +0000 (23:52 +0200)
Let ejabberd_http expand the @VERSION@ keyword to the ejabberd version
if specified in the "custom_headers" listener option.

Closes #1414.

src/ejabberd_http.erl
src/jlib.erl
src/mod_http_upload.erl

index 4c5dd8ebe0b6d09eac33c842cb601e0e3eb17d13..c3f962fbc068e4655a38abae176afd30f6b3823c 100644 (file)
@@ -169,7 +169,7 @@ init({SockMod, Socket}, Opts) ->
     {ok, RE} = re:compile(<<"^(?:\\[(.*?)\\]|(.*?))(?::(\\d+))?$">>),
 
     CustomHeaders = gen_mod:get_opt(custom_headers, Opts,
-                                   fun(L) when is_list(L) -> L end,
+                                   fun expand_custom_headers/1,
                                    []),
 
     ?INFO_MSG("started: ~p", [{SockMod1, Socket1}]),
@@ -733,6 +733,11 @@ rest_dir(0, Path, <<H, T/binary>>) ->
     rest_dir(0, <<H, Path/binary>>, T);
 rest_dir(N, Path, <<_H, T/binary>>) -> rest_dir(N, Path, T).
 
+expand_custom_headers(Headers) ->
+    lists:map(fun({K, V}) ->
+                     {K, jlib:expand_keyword(<<"@VERSION@">>, V, ?VERSION)}
+             end, Headers).
+
 %% hex_to_integer
 
 hex_to_integer(Hex) ->
index 12bedbc7ea960875e3e3a0a2103310f4047611ba..3b6a6556a2dab2c914b0f3eea6303ca0960dba19 100644 (file)
@@ -37,7 +37,7 @@
 
 -export([tolower/1, term_to_base64/1, base64_to_term/1,
         decode_base64/1, encode_base64/1, ip_to_list/1,
-        hex_to_bin/1, hex_to_base64/1,
+        hex_to_bin/1, hex_to_base64/1, expand_keyword/3,
         atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1,
         l2i/1, i2l/1, i2l/2, expr_to_term/1, term_to_expr/1]).
 
@@ -934,6 +934,12 @@ hex_to_bin([H1, H2 | T], Acc) ->
 
 hex_to_base64(Hex) -> encode_base64(hex_to_bin(Hex)).
 
+-spec expand_keyword(binary(), binary(), binary()) -> binary().
+
+expand_keyword(Keyword, Input, Replacement) ->
+    Parts = binary:split(Input, Keyword, [global]),
+    str:join(Parts, Replacement).
+
 binary_to_atom(Bin) ->
     erlang:binary_to_atom(Bin, utf8).
 
index 59570b0b5c339472584db0ab2ef7b1614a4154c0..31494c2e1f8c5c61eb78b03c0f3a3c4106ec5135 100644 (file)
@@ -500,16 +500,14 @@ get_proc_name(ServerHost, ModuleName) ->
 
 -spec expand_home(binary()) -> binary().
 
-expand_home(Subject) ->
+expand_home(Input) ->
     {ok, [[Home]]} = init:get_argument(home),
-    Parts = binary:split(Subject, <<"@HOME@">>, [global]),
-    str:join(Parts, list_to_binary(Home)).
+    jlib:expand_keyword(<<"@HOME@">>, Input, Home).
 
 -spec expand_host(binary(), binary()) -> binary().
 
-expand_host(Subject, Host) ->
-    Parts = binary:split(Subject, <<"@HOST@">>, [global]),
-    str:join(Parts, Host).
+expand_host(Input, Host) ->
+    jlib:expand_keyword(<<"@HOST@">>, Input, Host).
 
 %%--------------------------------------------------------------------
 %% Internal functions.