~ubuntu-branches/ubuntu/saucy/rabbitmq-server/saucy

« back to all changes in this revision

Viewing changes to plugins-src/rabbitmq-web-stomp/test/src/stomp.erl

  • Committer: Package Import Robot
  • Author(s): Emile Joubert
  • Date: 2012-11-19 11:42:31 UTC
  • mfrom: (0.2.18) (0.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20121119114231-hvapkn4akng09etr
Tags: 3.0.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/
 
5
%%
 
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
 
9
%%   under the License.
 
10
%%
 
11
%%   The Original Code is RabbitMQ Management Console.
 
12
%%
 
13
%%   The Initial Developer of the Original Code is VMware, Inc.
 
14
%%   Copyright (c) 2012 VMware, Inc.  All rights reserved.
 
15
%%
 
16
 
 
17
-module(stomp).
 
18
 
 
19
-export([marshal/2, marshal/3, unmarshal/1]).
 
20
 
 
21
-export([list_to_hex/1]).
 
22
 
 
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"]).
 
28
 
 
29
unmarshal(Frame) ->
 
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}.
 
35
    
 
36
    
 
37
 
 
38
%% ----------
 
39
 
 
40
iolist_join(List, Separator) ->
 
41
    lists:reverse(iolist_join2(List, Separator, [])).
 
42
 
 
43
iolist_join2([], _Separator, Acc) ->
 
44
    Acc;
 
45
iolist_join2([E | List], Separator, Acc) ->
 
46
    iolist_join2(List, Separator, [E, Separator | Acc]).
 
47
 
 
48
    
 
49
 
 
50
list_to_hex(L) ->
 
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)].
 
54
hex(N) when N < 10 ->
 
55
    $0+N;
 
56
hex(N) when N >= 10, N < 16 ->
 
57
    $a + (N-10).
 
58