Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 64 lines (55 sloc) 2.715 kb
180bb0d @trung refactor types to differentiate between amf data types
authored
1 -module(decoder).
6ab3d15 @trung amf3 read complete
authored
2 -author("trung@mdkt.org").
3
4 -export([read/1]).
5
5ae03f2 @trung restructure include files
authored
6 -include("../include/action_message.hrl").
6ab3d15 @trung amf3 read complete
authored
7
8 read_header(Bin) ->
9 io:fwrite("Bin = ~n~p~n", [Bin]),
10 {ok, HeaderName, BinAfterHeaderName} = amf0:read_string(Bin),
11 {ok, MustUnderstand, BinAfterMustUnderstand} = amf0:read_u8(BinAfterHeaderName),
12 {ok, _DataLen, DataBin} = amf0:read_u32(BinAfterMustUnderstand),
13 %% Don't care about DataLen, just read
14 %% {DataBin, NextBin} = split_binary(Rest2, DataLen),
15 %% clear ETS tables
16 amf0:reset(),
17 {ok, Data, NextBin} = amf0:read_object(DataBin),
18 Header = #header{headerName = HeaderName, mustUnderstand = case MustUnderstand of 0 -> false; _Other -> true end, data = Data},
19 {ok, Header, NextBin}.
20
21 read_headers(Bin, Count, Total, Acc) when Count == Total ->
22 {ok, Acc, Bin};
23 read_headers(Bin, Count, Total, Acc) ->
24 {ok, Header, NextBin} = read_header(Bin),
25 io:fwrite("Parsed header ~p : ~p~n", [Count + 1, Header]),
26 read_headers(NextBin, Count + 1, Total, Acc ++ [Header]).
27
28 read_body(Bin) ->
29 {ok, TargetUri, BinAfterTargetUri} = amf0:read_string(Bin),
30 {ok, ResponseUri, BinAfterResponseUri} = amf0:read_string(BinAfterTargetUri),
31 {ok, _DataLen, DataBin} = amf0:read_u32(BinAfterResponseUri),
32 %% Don't care about DataLen, just read
33 %% {DataBin, NextBin} = split_binary(Rest2, DataLen),
34 %% clear ETS tables
35 amf0:reset(),
36 {ok, Data, NextBin} = amf0:read_object(DataBin),
37 Body = #body{targetUri = TargetUri, responseUri = ResponseUri, data=Data},
38 {ok, Body, NextBin}.
39
40 read_bodies(Bin, Count, Total, Acc) when Count == Total ->
41 {ok, Acc, Bin};
42 read_bodies(Bin, Count, Total, Acc) ->
43 {ok, Body, NextBin} = read_body(Bin),
44 read_bodies(NextBin, Count + 1, Total, Acc ++ [Body]).
45
46 %% Read binary and translate to action_message record
47 %% Msg = #action_message
48 %% return {ok, Msg, Rest} or {bad, Reason}
49 %% ideally, Rest must be <<>>
50 read(<<?VERSION_1:16, Rest/binary>>) -> read_now(?VERSION_1, Rest);
51 read(<<?VERSION_3:16, Rest/binary>>) -> read_now(?VERSION_3, Rest);
52 read(_) ->
53 {bad, "Not a valid binary or version not supported"}.
54
55 read_now(Version, Bin) ->
56 {ok, HeaderCount, BinAfterHeaderCount} = amf0:read_u16(Bin),
57 io:fwrite("There are ~p header(s)~n", [HeaderCount]),
58 {ok, Headers, BinAfterHeaders} = read_headers(BinAfterHeaderCount, 0, HeaderCount, []),
59 {ok, BodyCount, BinAfterBodyCount} = amf0:read_u16(BinAfterHeaders),
60 io:fwrite("There are ~p body(s)~n", [BodyCount]),
61 {ok, Bodies, BinAfterBodies} = read_bodies(BinAfterBodyCount, 0, BodyCount, []),
62 Msg = #action_message{version = Version, headers = Headers, bodies = Bodies},
63 {ok, Msg, BinAfterBodies}.
Something went wrong with that request. Please try again.