aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cowboy_http.erl19
-rw-r--r--src/cowboy_http2.erl39
-rw-r--r--src/cowboy_req.erl16
3 files changed, 49 insertions, 25 deletions
diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl
index f9ee5ac..d694cff 100644
--- a/src/cowboy_http.erl
+++ b/src/cowboy_http.erl
@@ -302,7 +302,7 @@ parse_request(<< $\s, _/bits >>, State, _) ->
''}); %% @todo
%% We limit the length of the Request-line to MaxLength to avoid endlessly
%% reading from the socket and eventually crashing.
-parse_request(Buffer, State=#state{opts=Opts}, EmptyLines) ->
+parse_request(Buffer, State=#state{opts=Opts, in_streamid=InStreamID}, EmptyLines) ->
MaxLength = maps:get(max_request_line_length, Opts, 8000),
MaxEmptyLines = maps:get(max_empty_lines, Opts, 5),
case match_eol(Buffer, 0) of
@@ -324,6 +324,10 @@ parse_request(Buffer, State=#state{opts=Opts}, EmptyLines) ->
parse_version(Rest, State, <<"OPTIONS">>, <<"*">>, <<>>);
% << "CONNECT ", Rest/bits >> ->
% parse_authority( %% @todo
+ %% Accept direct HTTP/2 only at the beginning of the connection.
+ << "PRI * HTTP/2.0\r\n", _/bits >> when InStreamID =:= 1 ->
+ %% @todo Might be worth throwing to get a clean stacktrace.
+ http2_upgrade(State, Buffer, undefined);
_ ->
parse_method(Buffer, State, <<>>,
maps:get(max_method_length, Opts, 32))
@@ -636,6 +640,19 @@ request(Buffer, State0=#state{ref=Ref, transport=Transport, in_streamid=StreamID
end,
{request, Req, State, Buffer}.
+%% HTTP/2 upgrade.
+
+http2_upgrade(State=#state{parent=Parent, ref=Ref, socket=Socket, transport=Transport,
+ opts=Opts, handler=Handler}, Buffer, Settings) ->
+ case Transport:secure() of
+ false ->
+ _ = cancel_request_timeout(State),
+ cowboy_http2:init(Parent, Ref, Socket, Transport, Opts, Handler, Buffer, Settings);
+ true ->
+ error_terminate(400, State, {connection_error, protocol_error,
+ 'Clients that support HTTP/2 over TLS MUST use ALPN. (RFC7540 3.4)'})
+ end.
+
%% Request body parsing.
parse_body(Buffer, State=#state{in_streamid=StreamID, in_state=
diff --git a/src/cowboy_http2.erl b/src/cowboy_http2.erl
index 42d8ba5..67efa61 100644
--- a/src/cowboy_http2.erl
+++ b/src/cowboy_http2.erl
@@ -15,6 +15,7 @@
-module(cowboy_http2).
-export([init/6]).
+-export([init/8]).
-export([system_continue/3]).
-export([system_terminate/4]).
@@ -79,8 +80,22 @@
-spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(), module()) -> ok.
init(Parent, Ref, Socket, Transport, Opts, Handler) ->
- before_loop(#state{parent=Parent, ref=Ref, socket=Socket,
- transport=Transport, opts=Opts, handler=Handler}, <<>>).
+ init(Parent, Ref, Socket, Transport, Opts, Handler, <<>>, undefined).
+
+-spec init(pid(), ranch:ref(), inet:socket(), module(), cowboy:opts(), module(),
+ binary(), binary() | undefined) -> ok.
+init(Parent, Ref, Socket, Transport, Opts, Handler, Buffer, SettingsPayload) ->
+ State = #state{parent=Parent, ref=Ref, socket=Socket,
+ transport=Transport, opts=Opts, handler=Handler},
+ preface(State),
+ case Buffer of
+ <<>> -> before_loop(State, Buffer);
+ _ -> parse(State, Buffer)
+ end.
+
+preface(#state{socket=Socket, transport=Transport, next_settings=Settings}) ->
+ %% We send next_settings and use defaults until we get a ack.
+ ok = Transport:send(Socket, cow_http2:settings(Settings)).
%% @todo Add the timeout for last time since we heard of connection.
before_loop(State, Buffer) ->
@@ -130,19 +145,26 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, children=Ch
terminate(State, {internal_error, timeout, 'No message or data received before timeout.'})
end.
-parse(State=#state{socket=Socket, transport=Transport, next_settings=Settings, parse_state=preface}, Data) ->
+parse(State=#state{socket=Socket, transport=Transport, parse_state=preface}, Data) ->
case Data of
<< "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", Rest/bits >> ->
- %% @todo To speed up connection we may be able to construct the frame when starting the listener.
- %% We send next_settings and use defaults until we get a ack.
- Transport:send(Socket, cow_http2:settings(Settings)),
parse(State#state{parse_state=settings}, Rest);
_ when byte_size(Data) >= 24 ->
Transport:close(Socket),
exit({shutdown, {connection_error, protocol_error,
'The connection preface was invalid. (RFC7540 3.5)'}});
_ ->
- before_loop(State, Data)
+ Len = byte_size(Data),
+ << Preface:Len/binary, _/bits >> = <<"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n">>,
+ case Data of
+ Preface ->
+ %% @todo OK we should have a timeout when waiting for the preface.
+ before_loop(State, Data);
+ _ ->
+ Transport:close(Socket),
+ exit({shutdown, {connection_error, protocol_error,
+ 'The connection preface was invalid. (RFC7540 3.5)'}})
+ end
end;
%% @todo Perhaps instead of just more we can have {more, Len} to avoid all the checks.
parse(State=#state{parse_state=ParseState}, Data) ->
@@ -209,9 +231,10 @@ frame(State, {priority, _StreamID, _IsExclusive, _DepStreamID, _Weight}) ->
frame(State, {rst_stream, StreamID, Reason}) ->
stream_reset(State, StreamID, {stream_error, Reason, 'Stream reset requested by client.'});
%% SETTINGS frame.
-frame(State, {settings, Settings}) ->
+frame(State=#state{socket=Socket, transport=Transport}, {settings, Settings}) ->
%% @todo Apply SETTINGS.
io:format("settings ~p~n", [Settings]),
+ Transport:send(Socket, cow_http2:settings_ack()),
State;
%% Ack for a previously sent SETTINGS frame.
frame(State=#state{next_settings=_NextSettings}, settings_ack) ->
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 2ea0dde..998c2fe 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -1187,20 +1187,4 @@ connection_to_atom_test_() ->
],
[{lists:flatten(io_lib:format("~p", [T])),
fun() -> R = connection_to_atom(T) end} || {T, R} <- Tests].
-
-merge_headers_test_() ->
- Tests = [
- {[{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
- [{<<"set-cookie">>,<<"foo=bar">>},{<<"content-length">>,<<"11">>}],
- [{<<"set-cookie">>,<<"foo=bar">>},
- {<<"content-length">>,<<"13">>},
- {<<"server">>,<<"Cowboy">>}]},
- {[{<<"content-length">>,<<"13">>},{<<"server">>,<<"Cowboy">>}],
- [{<<"set-cookie">>,<<"foo=bar">>},{<<"set-cookie">>,<<"bar=baz">>}],
- [{<<"set-cookie">>,<<"bar=baz">>},
- {<<"set-cookie">>,<<"foo=bar">>},
- {<<"content-length">>,<<"13">>},
- {<<"server">>,<<"Cowboy">>}]}
- ],
- [fun() -> Res = merge_headers(L,R) end || {L, R, Res} <- Tests].
-endif.