Skip to content

Commit 75e8f2b

Browse files
committed
fix request scheme which was always undefined
1 parent 3ec3522 commit 75e8f2b

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/elli_example_callback.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ handle('GET', [<<"shorthand">>], _Req) ->
188188
handle('GET', [<<"ip">>], Req) ->
189189
{<<"200 OK">>, elli_request:peer(Req)};
190190

191+
handle('GET', [<<"scheme">>], Req) ->
192+
{<<"200 OK">>, elli_request:scheme(Req)};
193+
191194
handle('GET', [<<"304">>], _Req) ->
192195
%% A "Not Modified" response is exactly like a normal response (so
193196
%% Content-Length is included), but the body will not be sent.

src/elli_http.erl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ do_check_max_size_x2(_, _, _, _) -> ok.
610610
mk_req(Method, PathTuple, Headers, ParsedHeaders, Body, V, Socket, {Mod, Args} = Callback) ->
611611
case parse_path(PathTuple) of
612612
{ok, {Scheme, Host, Port}, {Path, URL, URLArgs}} ->
613-
#req{method = Method, scheme = Scheme, host = Host,
613+
#req{method = Method, scheme = update_scheme(Socket, Scheme), host = Host,
614614
port = Port, path = URL, args = URLArgs,
615615
version = V, raw_path = Path, original_headers = Headers,
616616
body = Body, pid = self(), socket = Socket,
@@ -625,7 +625,14 @@ mk_req(Method, PathTuple, Headers, ParsedHeaders, Body, V, Socket, {Mod, Args} =
625625

626626
mk_req(Method, Scheme, Host, Port, PathTuple, Headers, ParsedHeaders, Body, V, Socket, Callback) ->
627627
Req = mk_req(Method, PathTuple, Headers, ParsedHeaders, Body, V, Socket, Callback),
628-
Req#req{scheme = Scheme, host = Host, port = Port}.
628+
Req#req{scheme = update_scheme(Socket, Scheme), host = Host, port = Port}.
629+
630+
update_scheme({plain, _}, undefined) ->
631+
<<"http">>;
632+
update_scheme({ssl, _}, undefined) ->
633+
<<"https">>;
634+
update_scheme(_, Scheme) ->
635+
Scheme.
629636

630637
%%
631638
%% HEADERS

test/elli_ssl_tests.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ elli_ssl_test_() ->
1313
?_test(hello_world()),
1414
?_test(chunked()),
1515
?_test(sendfile()),
16-
?_test(acceptor_leak_regression())
16+
?_test(acceptor_leak_regression()),
17+
?_test(check_scheme_parsing())
1718
]}
1819
]}.
1920

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

37+
check_scheme_parsing() ->
38+
Response = hackney:get("https://localhost:3443/scheme",
39+
[], <<>>, [insecure]),
40+
?assertMatch(200, status(Response)),
41+
?assertMatch(<<"https">>, body(Response)).
42+
3643
chunked() ->
3744
Expected = <<"chunk10chunk9chunk8chunk7chunk6chunk5chunk4chunk3chunk2chunk1">>,
3845

test/elli_tests.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ elli_test_() ->
6363
?_test(get_pipeline()),
6464
?_test(head()),
6565
?_test(no_body()),
66-
?_test(sends_continue())
66+
?_test(sends_continue()),
67+
?_test(check_scheme_parsing())
6768
]}
6869
]}.
6970

@@ -108,12 +109,14 @@ accessors_test_() ->
108109
RawPath = <<"/foo/bar">>,
109110
Headers = [{<<"content-type">>, <<"application/x-www-form-urlencoded">>}],
110111
Method = 'POST',
112+
Scheme = <<"http">>,
111113
Body = <<"name=knut%3D">>,
112114
Name = <<"knut=">>,
113115
Req1 = #req{raw_path = RawPath,
114116
original_headers = Headers,
115117
headers = Headers,
116118
method = Method,
119+
scheme = Scheme,
117120
body = Body},
118121
Args = [{<<"name">>, Name}],
119122
Req2 = #req{original_headers = Headers, headers = Headers, args = Args, body = <<>>},
@@ -123,6 +126,7 @@ accessors_test_() ->
123126
?_assertMatch(RawPath, elli_request:raw_path(Req1)),
124127
?_assertMatch(Headers, elli_request:headers(Req1)),
125128
?_assertMatch(Method, elli_request:method(Req1)),
129+
?_assertMatch(Scheme, elli_request:scheme(Req1)),
126130
?_assertMatch(Body, elli_request:body(Req1)),
127131
?_assertMatch(Args, elli_request:post_args_decoded(Req1)),
128132
?_assertMatch(undefined, elli_request:post_arg(<<"foo">>, Req1)),
@@ -595,6 +599,11 @@ sends_continue() ->
595599
?assertMatch({ok, ExpectedResponse},
596600
gen_tcp:recv(Socket, size(ExpectedResponse))).
597601

602+
check_scheme_parsing() ->
603+
Response = hackney:get("http://localhost:3001/scheme"),
604+
?assertMatch(200, status(Response)),
605+
?assertMatch(<<"http">>, body(Response)).
606+
598607
%%% Slow client, sending only the specified byte size every millisecond
599608

600609
start_slow_client(Port, Url) ->

0 commit comments

Comments
 (0)