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

Add check for HTTP_PROXY env var #306

Merged
merged 1 commit into from
Jan 15, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -7,3 +7,4 @@ erl_crash.dump
mix.lock
/.idea
*.iml
/.elixir_ls
30 changes: 25 additions & 5 deletions lib/httpoison/base.ex
Original file line number Diff line number Diff line change
@@ -401,8 +401,6 @@ defmodule HTTPoison.Base do
recv_timeout = Keyword.get options, :recv_timeout
stream_to = Keyword.get options, :stream_to
async = Keyword.get options, :async
proxy = Keyword.get options, :proxy
proxy_auth = Keyword.get options, :proxy_auth
ssl = Keyword.get options, :ssl
follow_redirect = Keyword.get options, :follow_redirect
max_redirect = Keyword.get options, :max_redirect
@@ -411,8 +409,6 @@ defmodule HTTPoison.Base do

hn_options = if timeout, do: [{:connect_timeout, timeout} | hn_options], else: hn_options
hn_options = if recv_timeout, do: [{:recv_timeout, recv_timeout} | hn_options], else: hn_options
hn_options = if proxy, do: [{:proxy, proxy} | hn_options], else: hn_options
hn_options = if proxy_auth, do: [{:proxy_auth, proxy_auth} | hn_options], else: hn_options
hn_options = if ssl, do: [{:ssl_options, ssl} | hn_options], else: hn_options
hn_options = if follow_redirect, do: [{:follow_redirect, follow_redirect} | hn_options], else: hn_options
hn_options = if max_redirect, do: [{:max_redirect, max_redirect} | hn_options], else: hn_options
@@ -431,10 +427,34 @@ defmodule HTTPoison.Base do
hn_options
end

defp build_hackney_proxy_options(options, request_url) do
proxy =
if Keyword.has_key?(options, :proxy) do
Keyword.get(options, :proxy)
else
case URI.parse(request_url).scheme do
"http" -> System.get_env("HTTP_PROXY") || System.get_env("http_proxy")
"https" -> System.get_env("HTTPS_PROXY") || System.get_env("https_proxy")
_ -> nil
end
end

proxy_auth = Keyword.get(options, :proxy_auth)

hn_proxy_options = if proxy, do: [{:proxy, proxy}], else: []

hn_proxy_options =
if proxy_auth, do: [{:proxy_auth, proxy_auth} | hn_proxy_options], else: hn_proxy_options

hn_proxy_options
end


@doc false
@spec request(atom, atom, binary, body, headers, any, fun, fun, fun) :: {:ok, Response.t | AsyncResponse.t} | {:error, Error.t}
def request(module, method, request_url, request_body, request_headers, options, process_status_code, process_headers, process_response_body) do
hn_options = build_hackney_options(module, options)
hn_proxy_options = build_hackney_proxy_options(options, request_url)
hn_options = hn_proxy_options ++ build_hackney_options(module, options)

case do_request(method, request_url, request_headers, request_body, hn_options) do
{:ok, status_code, headers} -> response(process_status_code, process_headers, process_response_body, status_code, headers, "", request_url)
64 changes: 64 additions & 0 deletions test/httpoison_base_test.exs
Original file line number Diff line number Diff line change
@@ -146,6 +146,70 @@ defmodule HTTPoisonBaseTest do
assert validate :hackney
end

test "having http_proxy env variable set on http requests" do
expect(System, :get_env, [{["HTTP_PROXY"], "proxy"}])

expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", [proxy: "proxy"]], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body") ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate(:hackney)
end

test "having https_proxy env variable set on https requests" do
expect(System, :get_env, [{["HTTPS_PROXY"], "proxy"}])

expect(:hackney, :request, [
{[:post, "https://localhost", [], "body", [proxy: "proxy"]], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("https://localhost", "body") ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "https://localhost"
}

assert validate(:hackney)
end

test "having https_proxy env variable set on http requests" do
expect(System, :get_env, [
{["HTTPS_PROXY"], "proxy"},
{["HTTP_PROXY"], nil},
{["http_proxy"], nil}
])

expect(:hackney, :request, [
{[:post, "http://localhost", [], "body", []], {:ok, 200, "headers", :client}}
])

expect(:hackney, :body, 1, {:ok, "response"})

assert HTTPoison.post!("localhost", "body") ==
%HTTPoison.Response{
status_code: 200,
headers: "headers",
body: "response",
request_url: "http://localhost"
}

assert validate(:hackney)
end

test "passing ssl option" do
expect(:hackney, :request, [{[:post, "http://localhost", [], "body", [ssl_options: [certfile: "certs/client.crt"]]],
{:ok, 200, "headers", :client}}])