Skip to content

Commit

Permalink
make sure to always set the Host header.
Browse files Browse the repository at this point in the history
fix #100
  • Loading branch information
benoitc committed Apr 18, 2014
1 parent b44bfc2 commit 9b246c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
27 changes: 18 additions & 9 deletions src/hackney.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
_ ->
<<Path/binary, "?", Query/binary>>
end,
{Method, FinalPath, Headers, Body}.
{Method, FinalPath, Headers1, Body}.


maybe_proxy(Transport, Host, Port, Options)
Expand Down
13 changes: 2 additions & 11 deletions src/hackney_request.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9b246c1

Please sign in to comment.