Skip to content

Commit 51e8928

Browse files
authored
Merge pull request #38 from lpgauth/chore/upgrade-deps
Upgrade deps and replace cowboy test server with gen_tcp
2 parents 46f1f17 + 41c1372 commit 51e8928

5 files changed

Lines changed: 125 additions & 125 deletions

File tree

Makefile

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
CACHEGRIND=qcachegrind
21
REBAR3=$(shell which rebar3)
32
ifeq ($(REBAR3),)
43
REBAR3=./bin/rebar3
@@ -22,22 +21,10 @@ eunit:
2221
@echo "Running rebar3 eunit..."
2322
@$(REBAR3) do eunit -cv, cover -v
2423

25-
profile:
26-
@echo "Profiling..."
27-
@$(REBAR3) as test compile
28-
@erl +K true \
29-
-noshell \
30-
-pa _build/test/lib/*/ebin \
31-
-pa _build/test/lib/*/test \
32-
-eval 'buoy_profile:fprofx()' \
33-
-eval 'init:stop()'
34-
@_build/test/lib/fprofx/erlgrindx -p fprofx.analysis
35-
@$(CACHEGRIND) fprofx.cgrind
36-
3724
test: xref eunit dialyzer
3825

3926
xref:
4027
@echo "Running rebar3 xref..."
4128
@$(REBAR3) xref
4229

43-
.PHONY: clean compile dialyzer eunit profile test xref
30+
.PHONY: clean compile dialyzer eunit test xref

rebar.config

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{deps, [
2-
{foil, "0.1.4"},
3-
{shackle, "0.7.1"},
2+
{foil, "0.1.6"},
3+
{shackle, "0.7.3"},
44
{telemetry, "1.4.2"}
55
]}.
66

@@ -35,13 +35,6 @@
3535
warn_unused_import,
3636
warn_unused_vars
3737
]}
38-
]},
39-
{test, [
40-
{deps, [
41-
{cowboy, "2.12.0"},
42-
{fprofx,
43-
{git, "https://github.com/lpgauth/fprofx.git", {branch, "otp_19"}}}
44-
]}
4538
]}
4639
]}.
4740

rebar.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{"1.2.0",
2-
[{<<"foil">>,{pkg,<<"foil">>,<<"0.1.4">>},0},
3-
{<<"knot">>,{pkg,<<"knot">>,<<"0.1.0">>},1},
2+
[{<<"foil">>,{pkg,<<"foil">>,<<"0.1.6">>},0},
3+
{<<"knot">>,{pkg,<<"knot">>,<<"0.1.2">>},1},
44
{<<"metal">>,{pkg,<<"metal">>,<<"0.1.2">>},1},
5-
{<<"shackle">>,{pkg,<<"shackle">>,<<"0.7.1">>},0},
5+
{<<"shackle">>,{pkg,<<"shackle">>,<<"0.7.3">>},0},
66
{<<"telemetry">>,{pkg,<<"telemetry">>,<<"1.4.2">>},0}]}.
77
[
88
{pkg_hash,[
9-
{<<"foil">>, <<"1B786A2CC3E81496F98F799DF23AA24065253A94032AB79E5D60AED5423850AE">>},
10-
{<<"knot">>, <<"62CACC2CD9E2D50373EAFAC7C26A4286EF22C4590CBBF42FC81B4B0F497CC87D">>},
9+
{<<"foil">>, <<"31A9048E8EC6E8AC951ED3C4E4EB8F549465920C70F19E92CD9FC8A6867E7253">>},
10+
{<<"knot">>, <<"3B6C07CD24831D62752D879F031BCEDCFADB343738679EB1774A029D0A8B7D48">>},
1111
{<<"metal">>, <<"7501F14B075380F829498C4CE605643D6C7F0C55F60EBFCFBF694C33A558DF98">>},
12-
{<<"shackle">>, <<"0516B5890BFAC7153D10FABD8D73AB735685E84B6C22D0A8306567142741E63A">>},
12+
{<<"shackle">>, <<"DEDE0777BC297B212DE3DBE4377B391DA991FC011B4D5201960A5C4F11E83B2C">>},
1313
{<<"telemetry">>, <<"A0CB522801DFFB1C49FE6E30561BADFFC7B6D0E180DB1300DF759FAA22062855">>}]},
1414
{pkg_hash_ext,[
15-
{<<"foil">>, <<"4B0C51432A13740E5D74289C19A584A02B1BA02558AA9002665900EC61260E1B">>},
16-
{<<"knot">>, <<"5659015314767F69BD95D88B0AEDF6A65DCBEF3E9AFE49172D0429B18DB89584">>},
15+
{<<"foil">>, <<"8D34B12D712FFB30AD9E192F57885ABB1A61510CD3D9F6AA44470510A50B979E">>},
16+
{<<"knot">>, <<"42CB91A8024A8857D70A61B33E83D31370D5EB48681B366AE1203CB7C7CCFB38">>},
1717
{<<"metal">>, <<"CAC84E5CF2875B56E68874F3F0B3D598AF3EC30397EC2151D5C81C408961E6E4">>},
18-
{<<"shackle">>, <<"A9E14E3E253369F133226163C22854A20A5BC22DDCE5C61C87A51E587E5E67C8">>},
18+
{<<"shackle">>, <<"056944FEEB85ED0F3E1B7505FBC7174CDE0788993A19E9ADA85137A23CA56349">>},
1919
{<<"telemetry">>, <<"928F6495066506077862C0D1646609EED891A4326BEE3126BA54B60AF61FEBB1">>}]}
2020
].

test/buoy_http_server.erl

Lines changed: 113 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,124 @@
55
stop/0
66
]).
77

8-
-export([
9-
init/2
10-
]).
8+
-define(PORT, 8080).
119

1210
%% public
1311
start() ->
14-
application:ensure_all_started(cowboy),
15-
Dispatch = cowboy_router:compile([{'_', [
16-
{"/:test", ?MODULE, []}]}]),
17-
{ok, _} = cowboy:start_clear(?MODULE, [{port, 8080}], #{
18-
env => #{dispatch => Dispatch},
19-
max_keepalive => infinity,
20-
request_timeout => infinity
21-
}).
12+
Self = self(),
13+
Pid = spawn(fun () -> init(Self) end),
14+
receive
15+
{Pid, started} ->
16+
{ok, Pid}
17+
after 5000 ->
18+
{error, timeout}
19+
end.
2220

2321
stop() ->
24-
cowboy:stop_listener(?MODULE).
25-
26-
%% cowboy callbacks
27-
init(Req, State) ->
28-
case cowboy_req:binding(test, Req) of
29-
<<"1">> ->
30-
reply(200, <<"Hello world!">>, Req, State);
31-
<<"2">> ->
32-
Body = [<<"Hello world!">> || _ <- lists:seq(1, 1000)],
33-
reply(200, Body, Req, State);
34-
<<"3">> ->
35-
{ok, Body, Req2} = cowboy_req:read_body(Req),
36-
reply(200, Body, Req2, State);
37-
<<"4">> ->
38-
Req2 = cowboy_req:stream_reply(200, Req),
39-
ok = cowboy_req:stream_body("Hello", nofin, Req2),
40-
ok = cowboy_req:stream_body(" world!", fin, Req2),
41-
{ok, Req2, State};
42-
<<"5">> ->
43-
Verb = cowboy_req:method(Req),
44-
reply(200, Verb, Req, State)
22+
case whereis(?MODULE) of
23+
undefined ->
24+
ok;
25+
Pid ->
26+
exit(Pid, kill),
27+
ok
4528
end.
4629

4730
%% private
48-
reply(StatusCode, Body, Req, State) ->
49-
Req2 = cowboy_req:reply(StatusCode, #{
50-
<<"Content-Type">> => <<"text/plain">>,
51-
<<"Connection">> => <<"Keep-Alive">>
52-
}, Body, Req),
53-
{ok, Req2, State}.
31+
init(Parent) ->
32+
register(?MODULE, self()),
33+
{ok, LSocket} = gen_tcp:listen(?PORT, [
34+
binary,
35+
{active, false},
36+
{backlog, 128},
37+
{packet, http_bin},
38+
{reuseaddr, true}
39+
]),
40+
Parent ! {self(), started},
41+
accept(LSocket).
42+
43+
accept(LSocket) ->
44+
{ok, Socket} = gen_tcp:accept(LSocket),
45+
Pid = spawn_link(fun () ->
46+
receive go -> connection(Socket) end
47+
end),
48+
ok = gen_tcp:controlling_process(Socket, Pid),
49+
Pid ! go,
50+
accept(LSocket).
51+
52+
connection(Socket) ->
53+
case gen_tcp:recv(Socket, 0) of
54+
{ok, {http_request, Method, {abs_path, Path}, _Version}} ->
55+
ContentLength = headers(Socket, 0),
56+
Body = body(Socket, ContentLength),
57+
respond(Socket, Method, Path, Body),
58+
connection(Socket);
59+
{ok, _} ->
60+
gen_tcp:close(Socket);
61+
{error, _} ->
62+
gen_tcp:close(Socket)
63+
end.
64+
65+
headers(Socket, ContentLength) ->
66+
case gen_tcp:recv(Socket, 0) of
67+
{ok, {http_header, _, 'Content-Length', _, Value}} ->
68+
headers(Socket, binary_to_integer(Value));
69+
{ok, {http_header, _, _, _, _}} ->
70+
headers(Socket, ContentLength);
71+
{ok, http_eoh} ->
72+
ContentLength
73+
end.
74+
75+
body(_Socket, 0) ->
76+
<<>>;
77+
body(Socket, ContentLength) ->
78+
ok = inet:setopts(Socket, [{packet, raw}]),
79+
{ok, Body} = gen_tcp:recv(Socket, ContentLength),
80+
ok = inet:setopts(Socket, [{packet, http_bin}]),
81+
Body.
82+
83+
respond(Socket, Method, <<"/1">>, _Body) ->
84+
reply(Socket, Method, <<"Hello world!">>);
85+
respond(Socket, Method, <<"/2">>, _Body) ->
86+
reply(Socket, Method, binary:copy(<<"Hello world!">>, 1000));
87+
respond(Socket, Method, <<"/3">>, Body) ->
88+
reply(Socket, Method, Body);
89+
respond(Socket, Method, <<"/4">>, _Body) ->
90+
chunked_reply(Socket, Method, [<<"Hello">>, <<" world!">>]);
91+
respond(Socket, Method, <<"/5">>, _Body) ->
92+
reply(Socket, Method, method(Method)).
93+
94+
method(Method) when is_atom(Method) ->
95+
atom_to_binary(Method, utf8);
96+
method(Method) when is_binary(Method) ->
97+
Method.
98+
99+
reply(Socket, Method, Body) ->
100+
Headers = [
101+
<<"HTTP/1.1 200 OK\r\n">>,
102+
<<"Connection: Keep-Alive\r\n">>,
103+
<<"Content-Type: text/plain\r\n">>,
104+
<<"Content-Length: ">>, integer_to_binary(iolist_size(Body)),
105+
<<"\r\n\r\n">>
106+
],
107+
case Method of
108+
'HEAD' ->
109+
ok = gen_tcp:send(Socket, Headers);
110+
_ ->
111+
ok = gen_tcp:send(Socket, [Headers, Body])
112+
end.
113+
114+
chunked_reply(Socket, Method, Chunks) ->
115+
Headers = [
116+
<<"HTTP/1.1 200 OK\r\n">>,
117+
<<"Connection: Keep-Alive\r\n">>,
118+
<<"Content-Type: text/plain\r\n">>,
119+
<<"Transfer-Encoding: chunked\r\n\r\n">>
120+
],
121+
case Method of
122+
'HEAD' ->
123+
ok = gen_tcp:send(Socket, Headers);
124+
_ ->
125+
Encoded = [[integer_to_binary(byte_size(Chunk), 16), <<"\r\n">>,
126+
Chunk, <<"\r\n">>] || Chunk <- Chunks],
127+
ok = gen_tcp:send(Socket, [Headers, Encoded, <<"0\r\n\r\n">>])
128+
end.

test/buoy_profile.erl

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)