Skip to content

Commit

Permalink
Merge pull request #233 from bjc/chunked-bosh
Browse files Browse the repository at this point in the history
BOSH support for chunked transfer encoding.
  • Loading branch information
nniclausse authored Apr 21, 2017
2 parents b6bcce8 + 2ab00e8 commit 5459e9c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/tsung/ts_bosh.erl
Original file line number Diff line number Diff line change
Expand Up @@ -457,11 +457,32 @@ read_body(Type, _Vsn, Hdrs, Socket) ->
% closed (AFAIK, this was common in versions before 1.1).
case proplists:get_value('Content-Length', Hdrs, undefined) of
undefined ->
throw({no_content_length, Hdrs});
case proplists:get_value('Transfer-Encoding', Hdrs, undefined) of
undefined ->
throw({no_content_length, Hdrs});
"chunked" ->
read_chunked_body(Type, Hdrs, Socket, [])
end;
ContentLength ->
read_length(Type, Hdrs, Socket, list_to_integer(ContentLength))
end.

read_chunked_body(Type, Hdrs, Socket, Acc) ->
socket_setopts(Type, Socket, [{packet, line}, binary]),
{ok, HexLen} = socket_recv(Type, Socket, 0),
socket_setopts(Type, Socket, [{packet, raw}]),

Len = binary_to_integer(binary_part(HexLen, 0, byte_size(HexLen) - 2), 16),
case Len of
0 ->
{ok, _} = socket_recv(Type, Socket, 2),
{iolist_to_binary(lists:reverse(Acc)), Hdrs};
_ ->
{ok, Data} = socket_recv(Type, Socket, Len),
{ok, _} = socket_recv(Type, Socket, 2),
read_chunked_body(Type, [], Socket, [Data|Acc])
end.

read_length(Type, Hdrs, Socket, Length) ->
case socket_recv(Type, Socket, Length) of
{ok, Data} ->
Expand Down

0 comments on commit 5459e9c

Please sign in to comment.