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

Fix/parse qs amps #522

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/hackney.erl
Expand Up @@ -228,6 +228,8 @@ request(Method, URL, Headers, Body) ->
%% <li>`{proxy, proxy_options()}': to connect via a proxy.</li>
%% <li>`insecure': to perform "insecure" SSL connections and
%% transfers without checking the certificate</li>
%% <li>`{checkout_timeout, infinity | integer()}': timeout used when
%% checking out a socket from the pool, in milliseconds. Default is 8000</li>
%% <li>`{connect_timeout, infinity | integer()}': timeout used when
%% establishing a connection, in milliseconds. Default is 8000</li>
%% <li>`{recv_timeout, infinity | integer()}': timeout used when
Expand Down
6 changes: 3 additions & 3 deletions src/hackney_pool.erl
Expand Up @@ -67,9 +67,9 @@ checkout(Host0, Port, Transport, #client{options=Opts}=Client) ->
RequestRef = Client#client.request_ref,
Name = proplists:get_value(pool, Opts, default),
Pool = find_pool(Name, Opts),
ConnectTimeout = proplists:get_value(connect_timeout, Opts, 8000),
CheckoutTimeout = proplists:get_value(checkout_timeout, Opts, 8000),
case catch gen_server:call(Pool, {checkout, {Host, Port, Transport},
Pid, RequestRef}, ConnectTimeout) of
Pid, RequestRef}, CheckoutTimeout) of
{ok, Socket, Owner} ->
CheckinReference = {Host, Port, Transport},
{ok, {Name, RequestRef, CheckinReference, Owner, Transport}, Socket};
Expand All @@ -82,7 +82,7 @@ checkout(Host0, Port, Transport, #client{options=Opts}=Client) ->
{'EXIT', {timeout, _}} ->
% socket will still checkout so to avoid deadlock we send in a cancellation
gen_server:cast(Pool, {checkout_cancel, {Host, Port, Transport}, RequestRef}),
{error, connect_timeout}
{error, checkout_timeout}
end.

%% @doc release a socket in the pool
Expand Down
4 changes: 2 additions & 2 deletions src/hackney_url.erl
Expand Up @@ -328,8 +328,8 @@ tohexl(C) when C < 16 -> $a + C - 10.
parse_qs(<<>>) ->
[];
parse_qs(Bin) ->
Tokens = binary:split(Bin, <<"&">>, [trim, global]),
[case binary:split(Token, <<"=">>, [trim]) of
Tokens = binary:split(Bin, <<"&">>, [trim_all, global]),
[case binary:split(Token, <<"=">>, [trim_all]) of
[T] ->
{urldecode(T), true};
[Name, Value] ->
Expand Down
18 changes: 16 additions & 2 deletions test/hackney_pool_tests.erl
Expand Up @@ -8,7 +8,8 @@ dummy_test() ->

multipart_test_() ->
{setup, fun start/0, fun stop/1,
[queue_timeout()]}.
[{timeout, 120, queue_timeout()},
{timeout, 120, checkout_timeout()}]}.

start() ->
error_logger:tty(false),
Expand Down Expand Up @@ -39,7 +40,20 @@ queue_timeout() ->
ok = hackney:finish_send_body(Ref),
{ok, _Status, _Headers, Ref} = hackney:start_response(Ref),
ok = hackney:skip_body(Ref),
{ok, _} = hackney:request(post, URL, Headers, stream, Opts)
{ok, Ref2} = hackney:request(post, URL, Headers, stream, Opts),
hackney:close(Ref2)
end
end.

checkout_timeout() ->
fun() ->
URL = <<"http://localhost:8123/pool">>,
Headers = [],
Opts = [{max_body, 2048}, {pool, pool_test}, {connect_timeout, 1000}, {checkout_timeout, 100}],
case hackney:request(post, URL, Headers, stream, Opts) of
{ok, Ref} ->
{error, Error} = hackney:request(post, URL, Headers, stream, Opts),
hackney:close(Ref),
?assertEqual(Error, checkout_timeout)
end
end.
4 changes: 3 additions & 1 deletion test/hackney_url_tests.erl
Expand Up @@ -249,7 +249,9 @@ parse_qs_test_() ->
Tests = [
{<<"a=b">>, [{<<"a">>,<<"b">>}]},
{<<"a=b&c=d">>, [{<<"a">>,<<"b">>}, {<<"c">>, <<"d">>}]},
{<<"a=b&c">>, [{<<"a">>,<<"b">>}, {<<"c">>, true}]}
{<<"&a=b&&c=d&">>, [{<<"a">>,<<"b">>}, {<<"c">>, <<"d">>}]},
{<<"a=b&c">>, [{<<"a">>,<<"b">>}, {<<"c">>, true}]},
{<<"&a=b&c&&">>, [{<<"a">>,<<"b">>}, {<<"c">>, true}]}
],
[{V, fun() -> R = hackney_url:parse_qs(V) end} || {V, R} <- Tests].

Expand Down