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: using {active, 100} instead of recv #88

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.rebar3
doc/*.html
!doc/tpl.html
_build
Expand Down
17 changes: 4 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,9 @@ language: erlang
install: true
script: make travis
otp_release:
- 18.0
- 18.1
- 18.2
- 18.2.1
- 18.3
- 19.0
- 19.1
- 19.2
- 19.3
- 20.0
- 20.1
- 20.2
- 21.0
- 21.1
- 21.2
- 20.3
- 21.3
- 22.3
- 23.0.2
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v3.3.0

* Do not use x-forwarded-for for peer #75
* Handle arguments with no value in (post|get)_arg_decoded #82
* Fix compile-time warnings on missing record info. from aleppo #81

## v3.2.0

* Quell warnings on OTP-21: https://github.com/elli-lib/elli/pull/61
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{rebar3_lint, "v0.1.10"}
]}.

{provider_hooks, [{pre, [{eunit, lint}]}]}.
%% {provider_hooks, [{pre, [{eunit, lint}]}]}.
{dialyzer, [{plt_extra_apps, [ssl]}]}.

{cover_enabled, true}.
Expand Down
2 changes: 1 addition & 1 deletion src/elli.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, elli,
[
{description, "Erlang web server for HTTP APIs"},
{vsn, "3.2.0"},
{vsn, "3.3.0"},
{modules, [
elli,
elli_example_callback,
Expand Down
113 changes: 90 additions & 23 deletions src/elli_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ keepalive_loop(Socket, NumRequests, Buffer, Options, Callback) ->
Callback :: elli_handler:callback(),
ConnToken :: {'keep_alive' | 'close', binary()}.
handle_request(S, PrevB, Opts, {Mod, Args} = Callback) ->
elli_tcp:setopts(S, [{active, 100}]),
{Method, RawPath, V, B0} = get_request(S, PrevB, Opts, Callback),
t(headers_start),
{RequestHeaders, B1} = get_headers(S, V, B0, Opts, Callback),
Expand Down Expand Up @@ -404,7 +405,7 @@ send_chunk(Socket, Data) ->

%% @doc Retrieve the request line.
get_request(Socket, <<>>, Options, Callback) ->
NewBuffer = recv_request(Socket, <<>>, Options, Callback),
NewBuffer = recv_request(Socket, <<>>, request_timeout(Options), Options, Callback),
get_request(Socket, NewBuffer, Options, Callback);
get_request(Socket, Buffer, Options, Callback) ->
t(request_start),
Expand All @@ -413,7 +414,7 @@ get_request(Socket, Buffer, Options, Callback) ->
get_request_(Socket, Buffer, Options, {Mod, Args} = Callback) ->
case erlang:decode_packet(http_bin, Buffer, []) of
{more, _} ->
NewBuffer = recv_request(Socket, Buffer, Options, Callback),
NewBuffer = recv_request(Socket, Buffer, request_timeout(Options), Options, Callback),
get_request_(Socket, NewBuffer, Options, Callback);
{ok, {http_request, Method, RawPath, Version}, Rest} ->
{Method, RawPath, Version, Rest};
Expand All @@ -427,19 +428,49 @@ get_request_(Socket, Buffer, Options, {Mod, Args} = Callback) ->
exit(normal)
end.

recv_request(Socket, Buffer, Options, {Mod, Args} = _Callback) ->
case elli_tcp:recv(Socket, 0, request_timeout(Options)) of
{ok, Data} ->
recv_request(Socket, Buffer, Timeout, Options, {Mod, Args} = Callback) ->
receive
{tcp, _Socket, Data} ->
<<Buffer/binary, Data/binary>>;
{error, timeout} ->
handle_event(Mod, request_timeout, [], Args),
{tcp_passive, _Socket} ->
elli_tcp:setopts(Socket, [{active, 100}]),
recv_request(Socket, Buffer, Timeout, Options, Callback);
{tcp_closed, _Socket} ->
handle_event(Mod, request_closed, [], Args),
elli_tcp:close(Socket),
exit(normal);
{error, Closed} when Closed =:= closed orelse
Closed =:= enotconn ->
handle_event(Mod, request_closed, [], Args),
{tcp_error, _Socket, timeout} ->
handle_event(Mod, request_timeout, [], Args),
elli_tcp:close(Socket),
exit(normal)
%% TODO: Handle other error reasons?
%% {tcp_error, Socket, _} ->
%% handle_event(Mod, request_closed, [], Args),
%% elli_tcp:close(Socket),
%% exit(normal)
after
Timeout ->
handle_event(Mod, request_timeout, [], Args),
elli_tcp:close(Socket),
exit(normal)
end.

recv(Socket, Timeout) ->
receive
{tcp, _Socket, Data} ->
{ok, Data};
{tcp_passive, _Socket} ->
elli_tcp:setopts(Socket, [{active, 100}]),
recv(Socket, Timeout);
{tcp_closed, _Socket} ->
{error, closed};
{tcp_error, _Socket, timeout} ->
{error, timeout};
{tcp_error, _Socket, Reason} ->
{error, Reason}
after
Timeout ->
{error, timeout}
end.

-spec get_headers(Socket, V, Buffer, Opts, Callback) -> Headers when
Expand Down Expand Up @@ -470,12 +501,12 @@ get_headers(Socket, Buffer, Headers, Count, Opts, {Mod, Args} = Callback) ->
{ok, {http_error, _}, Rest} ->
get_headers(Socket, Rest, Headers, Count, Opts, Callback);
{more, _} ->
case elli_tcp:recv(Socket, 0, header_timeout(Opts)) of
case recv(Socket, header_timeout(Opts)) of
{ok, Data} ->
get_headers(Socket, <<Buffer/binary, Data/binary>>,
Headers, Count, Opts, Callback);
{error, Closed} when Closed =:= closed orelse
Closed =:= enotconn ->
{error, Reason} when Reason =:= closed orelse
Reason =:= enotconn ->
handle_event(Mod, client_closed, [receiving_headers], Args),
elli_tcp:close(Socket),
exit(normal);
Expand Down Expand Up @@ -532,20 +563,56 @@ get_body(Socket, Headers, Buffer, Opts, Callback) ->
Result
end.

do_get_body(Socket, Buffer, Opts, N, {Mod, Args}) ->
case elli_tcp:recv(Socket, N, body_timeout(Opts)) of
{ok, Data} ->
{<<Buffer/binary, Data/binary>>, <<>>};
{error, Closed} when Closed =:= closed orelse Closed =:= enotconn ->
handle_event(Mod, client_closed, [receiving_body], Args),
ok = elli_tcp:close(Socket),
do_get_body(Socket, Buffer, Opts, N, Callback) ->
{Body, Rest} = do_get_body_(Socket, <<>>, Opts, N, Callback),
{<<Buffer/binary, Body/binary>>, Rest}.

do_get_body_(Socket, Buffer, Opts, N, {Mod, Args} = Callback) ->
Timeout = body_timeout(Opts),
receive
{tcp, _Socket, <<Data:N/binary, Rest/binary>>} ->
Body = <<Buffer/binary, Data/binary>>,
{Body, Rest};
{tcp, _Socket, Data} ->
Body = <<Buffer/binary, Data/binary>>,
do_get_body_(Socket, Body, Opts, N - byte_size(Body), Callback);
{tcp_passive, _Socket} ->
elli_tcp:setopts(Socket, [{active, 100}]),
do_get_body_(Socket, Buffer, Opts, N, Callback);
{tcp_closed, _Socket} ->
handle_event(Mod, request_closed, [], Args),
elli_tcp:close(Socket),
exit(normal);
{error, timeout} ->
handle_event(Mod, client_timeout, [receiving_body], Args),
ok = elli_tcp:close(Socket),
{tcp_error, _Socket, timeout} ->
handle_event(Mod, request_timeout, [], Args),
elli_tcp:close(Socket),
exit(normal)
%% TODO: Handle other error reasons?
%% {tcp_error, Socket, _} ->
%% handle_event(Mod, request_closed, [], Args),
%% elli_tcp:close(Socket),
%% exit(normal)
after
Timeout ->
handle_event(Mod, request_timeout, [], Args),
elli_tcp:close(Socket),
exit(normal)
end.


%% case elli_tcp:recv(Socket, N, ) of
%% {ok, Data} ->
%% {<<Buffer/binary, Data/binary>>, <<>>};
%% {error, Closed} when Closed =:= closed orelse Closed =:= enotconn ->
%% handle_event(Mod, client_closed, [receiving_body], Args),
%% ok = elli_tcp:close(Socket),
%% exit(normal);
%% {error, timeout} ->
%% handle_event(Mod, client_timeout, [receiving_body], Args),
%% ok = elli_tcp:close(Socket),
%% exit(normal)
%% end.

ensure_binary(Bin) when is_binary(Bin) -> Bin;
ensure_binary(Atom) when is_atom(Atom) -> atom_to_binary(Atom, latin1).

Expand Down