Browse Source

Fix parsing of HTTP/2 PING frames

Fix for cases where the full frame is not received in one go.
Pablo Polvorin 8 years ago
parent
commit
b7ae1f5519
1 changed files with 10 additions and 1 deletions
  1. 10 1
      src/cow_http2.erl

+ 10 - 1
src/cow_http2.erl

@@ -196,7 +196,7 @@ parse(<< 8:24, 6:8, _:7, 0:1, _:1, 0:31, Opaque:64, Rest/bits >>) ->
 	{ok, {ping, Opaque}, Rest};
 parse(<< 8:24, 6:8, _:104, _/bits >>) ->
 	{connection_error, protocol_error, 'PING frames MUST NOT be associated with a stream. (RFC7540 6.7)'};
-parse(<< _:24, 6:8, _/bits >>) ->
+parse(<< Len:24, 6:8, _/bits >>) when Len =/= 8 ->
 	{connection_error, frame_size_error, 'PING frames MUST be 8 bytes wide. (RFC7540 6.7)'};
 %%
 %% GOAWAY frames.
@@ -233,6 +233,15 @@ parse(<< Len:24, 9:8, _:5, FlagEndHeaders:1, _:3, StreamID:31, HeaderBlockFragme
 parse(_) ->
 	more.
 
+-ifdef(TEST).
+parse_ping_test() ->
+	Ping = ping(1234567890),
+	_ = [more = parse(binary:part(Ping, 0, I)) || I <- lists:seq(1, byte_size(Ping) - 1)],
+	{ok, {ping, 1234567890}, <<>>} = parse(Ping),
+	{ok, {ping, 1234567890}, << 42 >>} = parse(<< Ping/binary, 42 >>),
+	ok.
+-endif.
+
 parse_fin(0) -> nofin;
 parse_fin(1) -> fin.