fix(stomp): fix unexpected_linefeed error if the packet truncated on headers

This commit is contained in:
JianBo He 2022-02-10 11:18:51 +08:00
parent 3fb5e19cc9
commit 289904bbcf
2 changed files with 31 additions and 0 deletions

View File

@ -123,6 +123,8 @@ parse(<<>>, Parser) ->
parse(Bytes, #{phase := body, length := Len, state := State}) -> parse(Bytes, #{phase := body, length := Len, state := State}) ->
parse(body, Bytes, State, Len); parse(body, Bytes, State, Len);
parse(<<?LF, Bytes/binary>>, #{phase := hdname, state := State}) ->
parse(body, Bytes, State, content_len(State));
parse(Bytes, #{phase := Phase, state := State}) when Phase =/= none -> parse(Bytes, #{phase := Phase, state := State}) when Phase =/= none ->
parse(Phase, Bytes, State); parse(Phase, Bytes, State);

View File

@ -359,6 +359,35 @@ t_1000_msg_send(_) ->
lists:foreach(fun(_) -> RecvFun() end, lists:seq(1, 1000)) lists:foreach(fun(_) -> RecvFun() end, lists:seq(1, 1000))
end). end).
t_sticky_packets_truncate_after_headers(_) ->
with_connection(fun(Sock) ->
gen_tcp:send(Sock, serialize(<<"CONNECT">>,
[{<<"accept-version">>, ?STOMP_VER},
{<<"host">>, <<"127.0.0.1:61613">>},
{<<"login">>, <<"guest">>},
{<<"passcode">>, <<"guest">>},
{<<"heart-beat">>, <<"0,0">>}])),
{ok, Data} = gen_tcp:recv(Sock, 0),
{ok, #stomp_frame{command = <<"CONNECTED">>,
headers = _,
body = _}, _} = parse(Data),
Topic = <<"/queue/foo">>,
emqx:subscribe(Topic),
gen_tcp:send(Sock, ["SEND\n",
"content-length:3\n",
"destination:/queue/foo\n"]),
timer:sleep(300),
gen_tcp:send(Sock, ["\nfoo",0]),
receive
{deliver, Topic, _Msg}->
ok
after 100 ->
?assert(false, "waiting message timeout")
end
end).
with_connection(DoFun) -> with_connection(DoFun) ->
{ok, Sock} = gen_tcp:connect({127, 0, 0, 1}, {ok, Sock} = gen_tcp:connect({127, 0, 0, 1},
61613, 61613,