Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Working on HTTP2 client #2

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/http2_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ handle_info({inet_async, _ListSock, Ref, {ok, CliSocket}},
ssl ->
{ok, AcceptSocket} = ssl:ssl_accept(CliSocket, SSLOptions),
%% TODO: Erlang 18 uses ALPN
{ok, _Upgrayedd} = ssl:negotiated_next_protocol(AcceptSocket),
{ok, _Upgrayedd} = ssl:negotiated_protocol(AcceptSocket),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this needs to be a switch based on erlang version 17/18

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. What is a sane way of doing that? Macro party?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably

AcceptSocket
end,
chatterbox_sup:start_socket(),
Expand Down
56 changes: 28 additions & 28 deletions src/http2c.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

%% API
-export([
start_link/0,
start_link/1,
send_binary/2,
send_frames/2,
send_unaltered_frames/2,
Expand All @@ -39,9 +39,9 @@
}).

%% Starts a server. Should probably take args eventually
-spec start_link() -> {ok, pid()} | ignore | {error, any()}.
start_link() ->
gen_server:start_link(?MODULE, [], []).
-spec start_link(any()) -> {ok, pid()} | ignore | {error, any()}.
start_link(Opts) ->
gen_server:start_link(?MODULE, [Opts], []).

%% Three API levels:
%% 1: lowest: Send a frame or set of frames
Expand Down Expand Up @@ -110,40 +110,40 @@ get_frames(Pid, StreamId) ->
{ok, #http2c_state{}, timeout()} |
ignore |
{stop, any()}.
init([]) ->
Host = "localhost",
{ok, Port} = application:get_env(chatterbox, port),
ClientOptions = [
binary,
{packet, raw},
{active, false}
],
%% TODO: Stealing from the server config here :/
{ok, SSLEnabled} = application:get_env(chatterbox, ssl),
{Transport, Options} = case SSLEnabled of
true ->
{ok, SSLOptions} = application:get_env(chatterbox, ssl_options),
{ssl, ClientOptions ++ SSLOptions ++ [{client_preferred_next_protocols, {client, [<<"h2">>]}}]};
false ->
{gen_tcp, ClientOptions}
end,
init([Options]) ->
Host = proplists:get_value(host, Options),
Port = proplists:get_value(port, Options),
ClientOptions = [binary,
{packet, raw},
{active, false}],
{Transport, Options1} =
case proplists:get_value(ssl, Options, false) of
false ->
{gen_tcp, ClientOptions};
true ->
SSLOptions = proplists:get_value(ssl_opts, Options, []),
{ssl, ClientOptions ++ SSLOptions ++
[{client_preferred_next_protocols, {client, [<<"h2">>]}}]}
end,
lager:debug("Options: ~p", [Options1]),
lager:debug("Transport: ~p", [Transport]),
{ok, Socket} = Transport:connect(Host, Port, Options),
{ok, Socket} = Transport:connect(Host, Port, Options1),

%% Send the preamble
Transport:send(Socket, <<?PREAMBLE>>),

%% Settings Handshake
{_SSH, ServerSettings} = http2_frame:read({Transport, Socket}),
http2_frame_settings:ack({Transport, Socket}),
ok = Transport:send(Socket, <<?PREAMBLE>>),

%% Send the Settings Handshake before anything else.
ClientSettings = #settings{},
http2_frame_settings:send({Transport, Socket}, #settings{}, ClientSettings),
{AH, _Ack} = http2_frame:read({Transport, Socket}),

%% Read and accept the settings from the server. @todo handle errors here
{_SSH, ServerSettings} = http2_frame:read({Transport, Socket}),
http2_frame_settings:ack({Transport, Socket}),
lager:debug("Ack: ~p", [_Ack]),
lager:debug("AH: ~p", [AH]),
Ack = ?IS_FLAG(AH#frame_header.flags, ?FLAG_ACK),
lager:debug("Ack: ~p", [Ack]),

case Transport of
ssl ->
ssl:setopts(Socket, [{active, true}]);
Expand Down
22 changes: 17 additions & 5 deletions test/header_continuation_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ end_per_testcase(_, Config) ->
ok.

basic_continuation(_Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),

%% build some headers
Headers = [
Expand Down Expand Up @@ -54,12 +57,15 @@ basic_continuation(_Config) ->
ct:pal("Resp: ~p", [Resp]),

?assertEqual(2, length(Resp)),

gen_server:stop(Client),
ok.


basic_continuation_end_stream_first(_Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),

%% build some headers
Headers = [
Expand Down Expand Up @@ -96,7 +102,10 @@ basic_continuation_end_stream_first(_Config) ->


bad_frame_wrong_type_between_continuations(_Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),

%% build some headers
Headers = [
Expand Down Expand Up @@ -138,7 +147,10 @@ bad_frame_wrong_type_between_continuations(_Config) ->
ok.

bad_frame_wrong_stream_between_continuations(_Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),

%% build some headers
Headers = [
Expand Down
15 changes: 12 additions & 3 deletions test/http2_frame_size_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ wrong_size_window_update(Config) ->
send_wrong_size(?WINDOW_UPDATE, Config).

send_wrong_size(Type, _Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),
http2c:send_binary(Client, <<10:24,Type:8,0:1,0:31,0:100>>),
timer:sleep(100),
Resp = http2c:get_frames(Client, 0),
Expand All @@ -46,7 +49,10 @@ send_wrong_size(Type, _Config) ->
ok.

frame_too_big(_Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),
Frames = [
{#frame_header{length=16392,type=?HEADERS,flags=?FLAG_END_HEADERS,stream_id=3}, #headers{block_fragment = <<1:131136>>}}
],
Expand All @@ -66,7 +72,10 @@ frame_too_big(_Config) ->
ok.

euc(_Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),

Headers1 = [
{<<":path">>, <<"/">>},
Expand Down
5 changes: 4 additions & 1 deletion test/protocol_errors_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ no_goaway_frame_on_non_zero(Config) ->


one_frame(Frame, _Config) ->
{ok, Client} = http2c:start_link(),
{ok, Port} = application:get_env(chatterbox, port),
{ok, Client} = http2c:start_link([{host, "127.0.0.1"},
{port, Port},
{ssl, true}]),
http2c:send_unaltered_frames(Client, [Frame]),

%% How do I get the response? Should be GOAWAY with PROTOCOL_ERROR
Expand Down
2 changes: 1 addition & 1 deletion test/starting_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ identifies_protocol(Config) ->
{ok, Socket} = ssl:connect("localhost", Port, Options),
ct:pal("Socket to me: ~p", [Socket]),

try ssl:negotiated_next_protocol(Socket) of
try ssl:negotiated_protocol(Socket) of
{ok, NextProtocol} ->
ct:pal("NextProtocol: ~p", [NextProtocol]),
<<"h2">> = NextProtocol,
Expand Down