From 9b246c1445f310457cbf199dac80c9966b05c16e Mon Sep 17 00:00:00 2001 From: benoitc Date: Fri, 18 Apr 2014 14:33:13 +0200 Subject: [PATCH] make sure to always set the Host header. fix #100 --- src/hackney.erl | 27 ++++++++++++++++++--------- src/hackney_request.erl | 13 ++----------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/hackney.erl b/src/hackney.erl index 2d48dbc1..b5ea389c 100644 --- a/src/hackney.erl +++ b/src/hackney.erl @@ -539,20 +539,26 @@ stop_async(Ref) -> %% internal functions %% %% +%% +host_header(#hackney_url{netloc=Netloc}, Headers) -> + case proplists:get_value(<<"Host">>, Headers) of + undefined -> Headers ++ [{<<"Host">>, Netloc}]; + _ -> lists:keyreplace(<<"Host">>, 1, Headers, + {<<"Host">>, Netloc}) + end. + make_request(connect, #hackney_url{}=URL, Headers, Body, _, _) -> #hackney_url{host = Host, port = Port}= URL, + + %% place the correct host + Headers1 = host_header(URL, Headers), + Path = iolist_to_binary([Host, ":", integer_to_list(Port)]), - {connect, Path, Headers, Body}; + {connect, Path, Headers1, Body}; make_request(Method, #hackney_url{}=URL, Headers0, Body, Options, true) -> - #hackney_url{netloc = Netloc} = URL, - %% place the correct host - Headers1 = case proplists:get_value(<<"Host">>, Headers0) of - undefined -> Headers0 ++ [{<<"Host">>, Netloc}]; - _ -> lists:keyreplace(<<"Host">>, 1, Headers0, - {<<"Host">>, Netloc}) - end, + Headers1 = host_header(URL, Headers0), FinalPath = hackney_url:unparse_url(URL), Headers = case proplists:get_value(proxy_auth, Options) of @@ -568,13 +574,16 @@ make_request(Method, #hackney_url{}=URL, Headers, Body, _, _) -> #hackney_url{path = Path, qs = Query} = URL, + %% place the correct host + Headers1 = host_header(URL, Headers), + FinalPath = case Query of <<>> -> Path; _ -> <> end, - {Method, FinalPath, Headers, Body}. + {Method, FinalPath, Headers1, Body}. maybe_proxy(Transport, Host, Port, Options) diff --git a/src/hackney_request.erl b/src/hackney_request.erl index cc11da82..2a7baada 100644 --- a/src/hackney_request.erl +++ b/src/hackney_request.erl @@ -31,18 +31,9 @@ perform(Client0, {Method0, Path, Headers0, Body0}) -> Method = hackney_bstr:to_upper(hackney_bstr:to_binary(Method0)), - #client{netloc=Netloc, options=Options} = Client0, + #client{options=Options} = Client0, - %% set initial headers - %% don't override the host if it's alreay set (especially when - %% connecting to a proxy. - DefaultHeaders0 = case proplists:get_value(<<"Host">>, Headers0) of - undefined -> - [{<<"Host">>, Netloc}, - {<<"User-Agent">>, default_ua()}]; - _ -> - [{<<"User-Agent">>, default_ua()}] - end, + DefaultHeaders0 = [{<<"User-Agent">>, default_ua()}], %% basic authorization handling DefaultHeaders = case proplists:get_value(basic_auth, Options) of