From db12dd680f6539f199308f0783309ffefa5f3b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Thu, 27 Feb 2020 16:27:34 +0100 Subject: Use specific error when HTTP/2 receives HTTP/1 response Also fix a build issue introduced in cherry-pick. --- src/gun_http2.erl | 12 ++++++++++-- test/rfc7540_SUITE.erl | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/gun_http2.erl b/src/gun_http2.erl index 3b92286..dcc1c88 100644 --- a/src/gun_http2.erl +++ b/src/gun_http2.erl @@ -134,6 +134,9 @@ parse(Data0, State0=#http2_state{buffer=Buffer, parse_state=preface}) -> <<_:24,4:8,_/bits>> -> {state, State0#http2_state{buffer=Data}}; %% Not a SETTINGS frame, this is an invalid preface. + <<"HTTP/1",_/bits>> -> + terminate(State0, {connection_error, protocol_error, + 'Invalid connection preface received. Appears to be an HTTP/1 response? (RFC7540 3.5)'}); _ -> terminate(State0, {connection_error, protocol_error, 'Invalid connection preface received. (RFC7540 3.5)'}) @@ -141,8 +144,13 @@ parse(Data0, State0=#http2_state{buffer=Buffer, parse_state=preface}) -> %% Any error in the preface is converted to this specific error %% to make debugging the problem easier (it's the server's fault). _ -> - terminate(State0, {connection_error, protocol_error, - 'Invalid connection preface received. (RFC7540 3.5)'}) + Reason = case Data of + <<"HTTP/1",_/bits>> -> + 'Invalid connection preface received. Appears to be an HTTP/1 response? (RFC7540 3.5)'; + _ -> + 'Invalid connection preface received. (RFC7540 3.5)' + end, + terminate(State0, {connection_error, protocol_error, Reason}) end; parse(Data0, State0=#http2_state{buffer=Buffer, parse_state=PS}) -> Data = << Buffer/binary, Data0/binary >>, diff --git a/test/rfc7540_SUITE.erl b/test/rfc7540_SUITE.erl index 43d2e52..0031b19 100644 --- a/test/rfc7540_SUITE.erl +++ b/test/rfc7540_SUITE.erl @@ -118,7 +118,7 @@ prior_knowledge_preface_http1(_) -> {ok, http2} = gun:await_up(ConnPid), receive {gun_down, ConnPid, http2, {error, {connection_error, protocol_error, - 'Invalid connection preface received. (RFC7540 3.5)'}}, [], []} -> + 'Invalid connection preface received. Appears to be an HTTP/1 response? (RFC7540 3.5)'}}, [], []} -> gun:close(ConnPid); Msg -> error({unexpected_msg, Msg}) @@ -144,7 +144,7 @@ prior_knowledge_preface_http1_await(_) -> {ok, ConnPid} = gun:open("localhost", Port, #{protocols => [http2], retry => 0}), {ok, http2} = gun:await_up(ConnPid), {error, {shutdown, {error, {connection_error, protocol_error, - 'Invalid connection preface received. (RFC7540 3.5)'}}}} + 'Invalid connection preface received. Appears to be an HTTP/1 response? (RFC7540 3.5)'}}}} = gun:await(ConnPid, make_ref()), gun:close(ConnPid). -- cgit v1.2.3