Do something simple to defend against non-UTF-8 headers. This is not round-trippable, but hopefully people doing binary headers are quite rare.

This commit is contained in:
Simon MacMullen 2011-03-10 16:34:59 +00:00
parent 1d2a565714
commit f11a34884f
1 changed files with 13 additions and 3 deletions

View File

@ -77,9 +77,19 @@ amqp_table(undefined) -> amqp_table([]);
amqp_table(Table) -> {struct, [{Name, amqp_value(Type, Value)} ||
{Name, Type, Value} <- Table]}.
amqp_value(array, Val) -> [amqp_value(T, V) || {T, V} <- Val];
amqp_value(table, Val) -> amqp_table(Val);
amqp_value(_Type, Val) -> Val.
amqp_value(array, Vs) -> [amqp_value(T, V) || {T, V} <- Vs];
amqp_value(table, V) -> amqp_table(V);
amqp_value(_Type, V) when is_binary(V) -> utf8_safe(V);
amqp_value(_Type, V) -> V.
utf8_safe(V) ->
try
xmerl_ucs:from_utf8(V),
V
catch exit:{ucs, _} ->
Enc = base64:encode(V),
<<"Invalid UTF-8, base64 is: ", Enc/binary>>
end.
tuple(unknown) -> unknown;
tuple(Tuple) when is_tuple(Tuple) -> [tuple(E) || E <- tuple_to_list(Tuple)];