1
%% The contents of this file are subject to the Mozilla Public License
2
%% Version 1.1 (the "License"); you may not use this file except in
3
%% compliance with the License. You may obtain a copy of the License at
4
%% http://www.mozilla.org/MPL/
6
%% Software distributed under the License is distributed on an "AS IS"
7
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
8
%% License for the specific language governing rights and limitations
11
%% The Original Code is RabbitMQ Management Console.
13
%% The Initial Developer of the Original Code is VMware, Inc.
14
%% Copyright (c) 2012 VMware, Inc. All rights reserved.
19
-export([marshal/2, marshal/3, unmarshal/1]).
21
-export([list_to_hex/1]).
23
marshal(Command, Headers) ->
24
marshal(Command, Headers, <<>>).
25
marshal(Command, Headers, Body) ->
26
Lines = [Command] ++ [[K, ":", V] || {K, V} <- Headers] ++ [["\n", Body]],
27
iolist_to_binary([iolist_join(Lines, "\n"), "\x00"]).
30
[Head, Body] = binary:split(Frame, <<"\n\n">>),
31
[Command | HeaderLines] = binary:split(Head, <<"\n">>, [global]),
32
Headers = [list_to_tuple(binary:split(Line, <<":">>)) || Line <- HeaderLines],
33
[Body1, <<>>] = binary:split(Body, [<<0>>],[{scope,{byte_size(Body)-1, 1}}]),
34
{Command, Headers, Body1}.
40
iolist_join(List, Separator) ->
41
lists:reverse(iolist_join2(List, Separator, [])).
43
iolist_join2([], _Separator, Acc) ->
45
iolist_join2([E | List], Separator, Acc) ->
46
iolist_join2(List, Separator, [E, Separator | Acc]).
51
lists:flatten(lists:map(fun(X) -> int_to_hex(X) end, L)).
52
int_to_hex(N) when N < 256 ->
53
[hex(N div 16), hex(N rem 16)].
56
hex(N) when N >= 10, N < 16 ->