Skip to content

Commit

Permalink
Renamed s/http2_/h2_
Browse files Browse the repository at this point in the history
  • Loading branch information
joedevivo committed Apr 28, 2016
1 parent 236ed94 commit b4cb254
Show file tree
Hide file tree
Showing 44 changed files with 522 additions and 550 deletions.
2 changes: 1 addition & 1 deletion src/chatterbox_ranch_protocol.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ start_link(Ref, Socket, Transport, Opts) ->
init(Ref, Socket, T, Opts) ->
ok = ranch:accept_ack(Ref),
Http2Settings = proplists:get_value(http2_settings, Opts, chatterbox:settings(server)),
http2_connection:become({transport(T), Socket}, Http2Settings).
h2_connection:become({transport(T), Socket}, Http2Settings).

transport(ranch_ssl) ->
ssl;
Expand Down
21 changes: 11 additions & 10 deletions src/chatterbox_static_content_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ handle(ConnPid, StreamId, Headers, _ReqBody) ->
ResponseHeaders = [
{<<":status">>,<<"403">>}
],
http2_connection:send_headers(ConnPid, StreamId, ResponseHeaders),
http2_connection:send_body(ConnPid, StreamId, <<"No soup for you!">>),
h2_connection:send_headers(ConnPid, StreamId, ResponseHeaders),
h2_connection:send_body(ConnPid, StreamId, <<"No soup for you!">>),
ok;
{true, false} ->
Ext = filename:extension(File),
Expand All @@ -83,10 +83,10 @@ handle(ConnPid, StreamId, Headers, _ReqBody) ->
{<<"content-type">>, MimeType}
],

http2_connection:send_headers(ConnPid, StreamId, ResponseHeaders),
h2_connection:send_headers(ConnPid, StreamId, ResponseHeaders),


case {MimeType, http2_connection:is_push(ConnPid)} of
case {MimeType, h2_connection:is_push(ConnPid)} of
{<<"text/html">>, true} ->
%% Search Data for resources to push
{ok, RE} = re:compile("<link rel=\"stylesheet\" href=\"([^\"]*)|<script src=\"([^\"]*)|src: '([^']*)"),
Expand All @@ -100,9 +100,10 @@ handle(ConnPid, StreamId, Headers, _ReqBody) ->

NewStreams =
lists:foldl(fun(R, Acc) ->
NewStreamId = http2_connection:new_stream(ConnPid),
PHeaders = generate_push_promise_headers(Headers, <<$/,R/binary>>),
http2_connection:send_promise(ConnPid, StreamId, NewStreamId, PHeaders),
NewStreamId = h2_connection:new_stream(ConnPid),
PHeaders = generate_push_promise_headers(Headers, <<$/,R/binary>>
),
h2_connection:send_promise(ConnPid, StreamId, NewStreamId, PHeaders),
[{NewStreamId, PHeaders}|Acc]
end,
[],
Expand All @@ -117,14 +118,14 @@ handle(ConnPid, StreamId, Headers, _ReqBody) ->
_ ->
ok
end,
http2_connection:send_body(ConnPid, StreamId, Data),
h2_connection:send_body(ConnPid, StreamId, Data),
ok;
{false, false} ->
ResponseHeaders = [
{<<":status">>,<<"404">>}
],
http2_connection:send_headers(ConnPid, StreamId, ResponseHeaders),
http2_connection:send_body(ConnPid, StreamId, <<"No soup for you!">>),
h2_connection:send_headers(ConnPid, StreamId, ResponseHeaders),
h2_connection:send_body(ConnPid, StreamId, <<"No soup for you!">>),
ok
end,
ok.
Expand Down
17 changes: 9 additions & 8 deletions src/chatterbox_static_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-include("http2.hrl").

-behaviour(http2_stream).
-behaviour(h2_stream).

-export([
init/2,
Expand Down Expand Up @@ -99,7 +99,7 @@ on_request_end_stream(State=#cb_static{connection_pid=ConnPid,
{<<"content-type">>, MimeType}
],

case {MimeType, http2_connection:is_push(ConnPid)} of
case {MimeType, h2_connection:is_push(ConnPid)} of
{<<"text/html">>, true} ->
%% Search Data for resources to push
{ok, RE} = re:compile("<link rel=\"stylesheet\" href=\"([^\"]*)|<script src=\"([^\"]*)|src: '([^']*)"),
Expand All @@ -113,9 +113,10 @@ on_request_end_stream(State=#cb_static{connection_pid=ConnPid,
NewStreams =
lists:foldl(
fun(R, Acc) ->
NewStreamId = http2_connection:new_stream(ConnPid),
PHeaders = generate_push_promise_headers(Headers, <<$/,R/binary>>),
http2_connection:send_promise(ConnPid, StreamId, NewStreamId, PHeaders),
NewStreamId = h2_connection:new_stream(ConnPid),
PHeaders = generate_push_promise_headers(Headers, <<$/,R/binary>>
),
h2_connection:send_promise(ConnPid, StreamId, NewStreamId, PHeaders),
[{NewStreamId, PHeaders}|Acc]
end,
[],
Expand Down Expand Up @@ -149,11 +150,11 @@ on_request_end_stream(State=#cb_static{connection_pid=ConnPid,

case {Method, HeadersToSend, BodyToSend} of
{<<"HEAD">>, _, _} ->
http2_connection:send_headers(ConnPid, StreamId, HeadersToSend, [{send_end_stream, true}]);
h2_connection:send_headers(ConnPid, StreamId, HeadersToSend, [{send_end_stream, true}]);
%%{<<"GET">>, _, _} ->
_ ->
http2_connection:send_headers(ConnPid, StreamId, HeadersToSend),
http2_connection:send_body(ConnPid, StreamId, BodyToSend)
h2_connection:send_headers(ConnPid, StreamId, HeadersToSend),
h2_connection:send_body(ConnPid, StreamId, BodyToSend)
end,

{ok, State}.
Expand Down
4 changes: 2 additions & 2 deletions src/chatterbox_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ init([]) ->
{ok, ListenSocket} = gen_tcp:listen(Port, Options),
Restart = {simple_one_for_one, 60, 3600},
Children = [{socket,
{http2_connection, start_server_link, [{Transport, ListenSocket}, SSLOptions, Http2Settings]},
temporary, 1000, worker, [http2_socket]}],
{h2_connection, start_server_link, [{Transport, ListenSocket}, SSLOptions, Http2Settings]},
temporary, 1000, worker, [h2_connection]}],
{ok, {Restart, Children}}.

start_socket() ->
Expand Down
25 changes: 12 additions & 13 deletions src/http2_client.erl → src/h2_client.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-module(http2_client).

-module(h2_client).
-include("http2.hrl").

%% Today's the day! We need to turn this gen_server into a gen_fsm
Expand Down Expand Up @@ -106,33 +105,33 @@ start_link(Transport, Host, Port, SSLOptions) ->
http -> gen_tcp;
https -> ssl
end,
http2_connection:start_client_link(NewT, Host, Port, SSLOptions, chatterbox:settings(client)).
h2_connection:start_client_link(NewT, Host, Port, SSLOptions, chatterbox:settings(client)).

start_ssl_upgrade_link(Host, Port, InitialMessage, SSLOptions) ->
http2_connection:start_ssl_upgrade_link(Host, Port, InitialMessage, SSLOptions, chatterbox:settings(client)).
h2_connection:start_ssl_upgrade_link(Host, Port, InitialMessage, SSLOptions, chatterbox:settings(client)).

-spec stop(pid()) -> ok.
stop(Pid) ->
http2_connection:stop(Pid).
h2_connection:stop(Pid).

sync_request(CliPid, Headers, Body) ->
StreamId = http2_connection:new_stream(CliPid),
http2_connection:send_headers(CliPid, StreamId, Headers),
http2_connection:send_body(CliPid,StreamId,Body),
StreamId = h2_connection:new_stream(CliPid),
h2_connection:send_headers(CliPid, StreamId, Headers),
h2_connection:send_body(CliPid,StreamId,Body),
receive
{'END_STREAM', StreamId} ->
http2_connection:get_response(CliPid, StreamId)
h2_connection:get_response(CliPid, StreamId)
after 5000 ->
{error, timeout}
end.
send_request(CliPid, Headers, Body) ->
StreamId = http2_connection:new_stream(CliPid),
http2_connection:send_headers(CliPid, StreamId, Headers),
http2_connection:send_body(CliPid,StreamId,Body),
StreamId = h2_connection:new_stream(CliPid),
h2_connection:send_headers(CliPid, StreamId, Headers),
h2_connection:send_body(CliPid,StreamId,Body),
{ok, StreamId}.

-spec get_response(pid(), stream_id()) ->
{ok, {hpack:header(), iodata()}}
| {error, term()}.
get_response(CliPid, StreamId) ->
http2_connection:get_response(CliPid, StreamId).
h2_connection:get_response(CliPid, StreamId).
Loading

0 comments on commit b4cb254

Please sign in to comment.