Skip to content

Commit

Permalink
fix request scheme which was always undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
tsloughter committed Feb 18, 2022
1 parent 3ec3522 commit cae3b5e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/elli_example_callback.erl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ handle('GET', [<<"shorthand">>], _Req) ->
handle('GET', [<<"ip">>], Req) ->
{<<"200 OK">>, elli_request:peer(Req)};

handle('GET', [<<"scheme">>], Req) ->
{<<"200 OK">>, elli_request:scheme(Req)};

handle('GET', [<<"304">>], _Req) ->
%% A "Not Modified" response is exactly like a normal response (so
%% Content-Length is included), but the body will not be sent.
Expand Down
1 change: 1 addition & 0 deletions src/elli_example_middleware.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
%%

handle(Req, _Args) ->
<<"http">> = elli_request:scheme(Req),
do_handle(elli_request:path(Req)).

do_handle([<<"middleware">>, <<"short-circuit">>]) ->
Expand Down
11 changes: 9 additions & 2 deletions src/elli_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ do_check_max_size_x2(_, _, _, _) -> ok.
mk_req(Method, PathTuple, Headers, ParsedHeaders, Body, V, Socket, {Mod, Args} = Callback) ->
case parse_path(PathTuple) of
{ok, {Scheme, Host, Port}, {Path, URL, URLArgs}} ->
#req{method = Method, scheme = Scheme, host = Host,
#req{method = Method, scheme = update_scheme(Socket, Scheme), host = Host,
port = Port, path = URL, args = URLArgs,
version = V, raw_path = Path, original_headers = Headers,
body = Body, pid = self(), socket = Socket,
Expand All @@ -625,7 +625,14 @@ mk_req(Method, PathTuple, Headers, ParsedHeaders, Body, V, Socket, {Mod, Args} =

mk_req(Method, Scheme, Host, Port, PathTuple, Headers, ParsedHeaders, Body, V, Socket, Callback) ->
Req = mk_req(Method, PathTuple, Headers, ParsedHeaders, Body, V, Socket, Callback),
Req#req{scheme = Scheme, host = Host, port = Port}.
Req#req{scheme = update_scheme(Socket, Scheme), host = Host, port = Port}.

update_scheme({plain, _}, undefined) ->
<<"http">>;
update_scheme({ssl, _}, undefined) ->
<<"https">>;
update_scheme(_, Scheme) ->
Scheme.

%%
%% HEADERS
Expand Down
9 changes: 8 additions & 1 deletion test/elli_ssl_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ elli_ssl_test_() ->
?_test(hello_world()),
?_test(chunked()),
?_test(sendfile()),
?_test(acceptor_leak_regression())
?_test(acceptor_leak_regression()),
?_test(check_scheme_parsing())
]}
]}.

Expand All @@ -33,6 +34,12 @@ hello_world() ->
?assertMatch(200, status(Response)),
?assertMatch({ok, 200, _, _}, Response).

check_scheme_parsing() ->
Response = hackney:get("https://localhost:3443/scheme",
[], <<>>, [insecure]),
?assertMatch(200, status(Response)),
?assertMatch(<<"https">>, body(Response)).

chunked() ->
Expected = <<"chunk10chunk9chunk8chunk7chunk6chunk5chunk4chunk3chunk2chunk1">>,

Expand Down
11 changes: 10 additions & 1 deletion test/elli_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ elli_test_() ->
?_test(get_pipeline()),
?_test(head()),
?_test(no_body()),
?_test(sends_continue())
?_test(sends_continue()),
?_test(check_scheme_parsing())
]}
]}.

Expand Down Expand Up @@ -108,12 +109,14 @@ accessors_test_() ->
RawPath = <<"/foo/bar">>,
Headers = [{<<"content-type">>, <<"application/x-www-form-urlencoded">>}],
Method = 'POST',
Scheme = <<"http">>,
Body = <<"name=knut%3D">>,
Name = <<"knut=">>,
Req1 = #req{raw_path = RawPath,
original_headers = Headers,
headers = Headers,
method = Method,
scheme = Scheme,
body = Body},
Args = [{<<"name">>, Name}],
Req2 = #req{original_headers = Headers, headers = Headers, args = Args, body = <<>>},
Expand All @@ -123,6 +126,7 @@ accessors_test_() ->
?_assertMatch(RawPath, elli_request:raw_path(Req1)),
?_assertMatch(Headers, elli_request:headers(Req1)),
?_assertMatch(Method, elli_request:method(Req1)),
?_assertMatch(Scheme, elli_request:scheme(Req1)),
?_assertMatch(Body, elli_request:body(Req1)),
?_assertMatch(Args, elli_request:post_args_decoded(Req1)),
?_assertMatch(undefined, elli_request:post_arg(<<"foo">>, Req1)),
Expand Down Expand Up @@ -595,6 +599,11 @@ sends_continue() ->
?assertMatch({ok, ExpectedResponse},
gen_tcp:recv(Socket, size(ExpectedResponse))).

check_scheme_parsing() ->
Response = hackney:get("http://localhost:3001/scheme"),
?assertMatch(200, status(Response)),
?assertMatch(<<"http">>, body(Response)).

%%% Slow client, sending only the specified byte size every millisecond

start_slow_client(Port, Url) ->
Expand Down

0 comments on commit cae3b5e

Please sign in to comment.