Skip to content

Commit

Permalink
drop OTP-21 support
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Feb 19, 2022
1 parent 7f6060e commit 57532cb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
otp_version: ['24', '23', '22', '21']
otp_version: ['24', '23', '22']
os: [ubuntu-latest]

steps:
Expand Down
13 changes: 7 additions & 6 deletions elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
]}},
{elvis_style, used_ignored_variable},
{elvis_style, no_behavior_info},
{elvis_style, state_record_and_type},
{elvis_style, atom_naming_convention,
#{ignore => [elli_http]}},
{elvis_style, state_record_and_type,
#{ignore => [elli]}},
{elvis_style, no_spec_with_records},
{elvis_style, dont_repeat_yourself},
{elvis_style, no_debug_call}
{elvis_style, no_debug_call},
{elvis_style, god_modules,
#{ignore => [elli_request]}}
],
ruleset => erl_files
},
#{dirs => ["."],
filter => "Makefile",
ruleset => makefiles
},
#{dirs => ["."],
filter => "rebar.config",
ruleset => rebar_config
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 @@

{project_plugins, [
{covertool, "2.0.3"},
{rebar3_lint, "v0.1.10"}
{rebar3_lint, "1.0.1"}
]}.

{provider_hooks, [{pre, [{eunit, lint}]}]}.
Expand Down
1 change: 1 addition & 0 deletions src/elli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-behaviour(gen_server).
-include("elli.hrl").
-include("elli_util.hrl").
-include_lib("kernel/include/logger.hrl").

%% API
-export([start_link/0,
Expand Down
1 change: 1 addition & 0 deletions src/elli_example_callback.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
-behaviour(elli_handler).

-include_lib("kernel/include/file.hrl").
-include_lib("kernel/include/logger.hrl").

%%
%% ELLI REQUEST CALLBACK
Expand Down
104 changes: 29 additions & 75 deletions src/elli_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-module(elli_http).
-include("elli.hrl").
-include("elli_util.hrl").

-include_lib("kernel/include/logger.hrl").

%% API
-export([start_link/4]).
Expand Down Expand Up @@ -324,17 +324,17 @@ execute_callback(#req{callback = {Mod, Args}} = Req) ->
catch
throw:{ResponseCode, Headers, Body} when is_integer(ResponseCode) ->
{response, ResponseCode, Headers, Body};
?WITH_STACKTRACE(throw, Exc, Stacktrace)
throw:Exc:Stacktrace ->
handle_event(Mod, request_throw,
[Req, Exc, Stacktrace],
Args),
{response, 500, [], <<"Internal server error">>};
?WITH_STACKTRACE(error, Error, Stacktrace)
error:Error:Stacktrace ->
handle_event(Mod, request_error,
[Req, Error, Stacktrace],
Args),
{response, 500, [], <<"Internal server error">>};
?WITH_STACKTRACE(exit, Exit, Stacktrace)
exit:Exit:Stacktrace ->
handle_event(Mod, request_exit,
[Req, Exit, Stacktrace],
Args),
Expand Down Expand Up @@ -716,7 +716,6 @@ is_header_defined(Key, Headers) ->
get_header(Key, Headers) ->
get_header(Key, Headers, undefined).

-ifdef(OTP_RELEASE).
get_header(Key, Headers, Default) ->
CaseFoldedKey = string:casefold(Key),
case lists:search(fun({N, _}) -> string:equal(CaseFoldedKey, N, true) end, Headers) of
Expand All @@ -725,24 +724,6 @@ get_header(Key, Headers, Default) ->
false ->
Default
end.
-else.
get_header(Key, Headers, Default) ->
CaseFoldedKey = string:casefold(Key),
case search(fun({N, _}) -> string:equal(CaseFoldedKey, N, true) end, Headers) of
{value, {_, Value}} ->
Value;
false ->
Default
end.

search(Pred, [Hd|Tail]) ->
case Pred(Hd) of
true -> {value, Hd};
false -> search(Pred, Tail)
end;
search(Pred, []) when is_function(Pred, 1) ->
false.
-endif.

default_scheme_port(<<"http">>) ->
80;
Expand All @@ -755,58 +736,31 @@ default_scheme_port(_) ->
%% PATH HELPERS
%%

-ifdef(OTP_RELEASE).
-if(?OTP_RELEASE >= 22).
parse_path(SocketScheme, HostHeader, {abs_path, FullPath}) ->
case uri_string:parse(FullPath) of
URIMap when not is_map_key(host, URIMap) ,
SocketScheme =/= undefined ,
HostHeader =/= undefined ->
HostMap = uri_string:parse(
unicode:characters_to_binary([SocketScheme, "://", HostHeader])),
Host = maps:get(host, HostMap, HostHeader),
Scheme = maps:get(scheme, HostMap, SocketScheme),
Port = maps:get(port, HostMap, default_scheme_port(Scheme));
URIMap ->
Host = maps:get(host, URIMap, HostHeader),
Scheme = maps:get(scheme, URIMap, SocketScheme),
Port = maps:get(port, URIMap, default_scheme_port(Scheme))
end,
parse_path(SocketScheme, HostHeader, {abs_path, FullPath}) ->
case uri_string:parse(FullPath) of
URIMap when not is_map_key(host, URIMap) ,
SocketScheme =/= undefined ,
HostHeader =/= undefined ->
HostMap = uri_string:parse(
unicode:characters_to_binary([SocketScheme, "://", HostHeader])),
Host = maps:get(host, HostMap, HostHeader),
Scheme = maps:get(scheme, HostMap, SocketScheme),
Port = maps:get(port, HostMap, default_scheme_port(Scheme));
URIMap ->
Host = maps:get(host, URIMap, HostHeader),
Scheme = maps:get(scheme, URIMap, SocketScheme),
Port = maps:get(port, URIMap, default_scheme_port(Scheme))
end,

Path = maps:get(path, URIMap, <<>>),
Query = maps:get(query, URIMap, <<>>),

{ok, {Scheme, Host, Port},
{Path, split_path(Path), uri_string:dissect_query(Query)}};
parse_path(_Scheme, _HostHeader, {absoluteURI, Scheme, Host, Port, Path}) ->
setelement(2, parse_path(undefined, undefined, {abs_path, Path}), {Scheme, Host, Port});
parse_path(_, _, _) ->
{error, unsupported_uri}.
-else.
parse_path(_Scheme, _HostHeader, {abs_path, FullPath}) ->
Parsed = case binary:split(FullPath, [<<"?">>]) of
[URL] -> {FullPath, split_path(URL), []};
[URL, Args] -> {FullPath, split_path(URL), split_args(Args)}
end,
{ok, {undefined, undefined, undefined}, Parsed};
parse_path(Scheme, HostHeader, {absoluteURI, Scheme, Host, Port, Path}) ->
setelement(2, parse_path(Scheme, HostHeader, {abs_path, Path}), {Scheme, Host, Port});
parse_path(_, _, _) ->
{error, unsupported_uri}.
-endif.
-else.
%% same as else branch above. can drop this when only OTP 21+ is supported
parse_path(Scheme, HostHeader, {abs_path, FullPath}) ->
Parsed = case binary:split(FullPath, [<<"?">>]) of
[URL] -> {FullPath, split_path(URL), []};
[URL, Args] -> {FullPath, split_path(URL), split_args(Args)}
end,
{ok, {undefined, undefined, undefined}, Parsed};
parse_path(Scheme, HostHeader, {absoluteURI, Scheme, Host, Port, Path}) ->
setelement(2, parse_path(Scheme, HostHeader, {abs_path, Path}), {Scheme, Host, Port});
parse_path(_, _, _) ->
{error, unsupported_uri}.
-endif.
Path = maps:get(path, URIMap, <<>>),
Query = maps:get(query, URIMap, <<>>),

{ok, {Scheme, Host, Port},
{Path, split_path(Path), uri_string:dissect_query(Query)}};
parse_path(_Scheme, _HostHeader, {absoluteURI, Scheme, Host, Port, Path}) ->
setelement(2, parse_path(undefined, undefined, {abs_path, Path}), {Scheme, Host, Port});
parse_path(_, _, _) ->
{error, unsupported_uri}.

split_path(Path) ->
[P || P <- binary:split(Path, [<<"/">>], [global]),
Expand Down Expand Up @@ -841,7 +795,7 @@ handle_event(Mod, Name, EventArgs, ElliArgs) ->
try
Mod:handle_event(Name, EventArgs, ElliArgs)
catch
?WITH_STACKTRACE(EvClass, EvError, Stacktrace)
EvClass:EvError:Stacktrace ->
?LOG_ERROR("~p:handle_event/3 crashed ~p:~p~n~p",
[Mod, EvClass, EvError, Stacktrace])
end.
Expand Down
15 changes: 0 additions & 15 deletions src/elli_util.hrl
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@

-ifdef(OTP_RELEASE).
-include_lib("kernel/include/logger.hrl").
-else.
-define(LOG_ERROR(Str), error_logger:error_msg(Str)).
-define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)).
-define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)).
-endif.

-ifdef(OTP_RELEASE).
-define(WITH_STACKTRACE(T, R, S), T:R:S ->).
-else.
-define(WITH_STACKTRACE(T, R, S), T:R -> S = erlang:get_stacktrace(),).
-endif.

%% Bloody useful
-define(IF(Test,True,False), case Test of true -> True; false -> False end).
7 changes: 1 addition & 6 deletions test/elli_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
-define(VTB(T1, T2, LB, UB),
time_diff_to_micro_seconds(T1, T2) >= LB andalso
time_diff_to_micro_seconds(T1, T2) =< UB).
-ifdef(OTP_RELEASE).

-include_lib("kernel/include/logger.hrl").
-else.
-define(LOG_ERROR(Str), error_logger:error_msg(Str)).
-define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)).
-define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)).
-endif.

time_diff_to_micro_seconds(T1, T2) ->
erlang:convert_time_unit(
Expand Down

0 comments on commit 57532cb

Please sign in to comment.