simplification of formatting host method

case for format_proxy method
This commit is contained in:
Alexander Strizhakov 2020-03-03 15:44:13 +03:00
parent ee8071f0d5
commit e605e79df9
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
5 changed files with 43 additions and 73 deletions

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Gun.Conn do defmodule Pleroma.Gun.Conn do
@ -131,7 +131,7 @@ defmodule Pleroma.Gun.Conn do
end end
defp do_open(%URI{host: host, port: port} = uri, opts) do defp do_open(%URI{host: host, port: port} = uri, opts) do
{_type, host} = Pleroma.HTTP.Adapter.domain_or_ip(host) host = Pleroma.HTTP.Connection.parse_host(host)
with {:ok, conn} <- API.open(host, port, opts), with {:ok, conn} <- API.open(host, port, opts),
{:ok, _} <- API.await_up(conn, opts[:await_up_timeout]) do {:ok, _} <- API.await_up(conn, opts[:await_up_timeout]) do
@ -149,7 +149,7 @@ defmodule Pleroma.Gun.Conn do
end end
defp destination_opts(%URI{host: host, port: port}) do defp destination_opts(%URI{host: host, port: port}) do
{_type, host} = Pleroma.HTTP.Adapter.domain_or_ip(host) host = Pleroma.HTTP.Connection.parse_host(host)
%{host: host, port: port} %{host: host, port: port}
end end

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.Adapter do defmodule Pleroma.HTTP.Adapter do
@ -8,7 +8,6 @@ defmodule Pleroma.HTTP.Adapter do
@type proxy :: @type proxy ::
{Connection.host(), pos_integer()} {Connection.host(), pos_integer()}
| {Connection.proxy_type(), pos_integer()} | {Connection.proxy_type(), pos_integer()}
@type host_type :: :domain | :ip
@callback options(keyword(), URI.t()) :: keyword() @callback options(keyword(), URI.t()) :: keyword()
@callback after_request(keyword()) :: :ok @callback after_request(keyword()) :: :ok
@ -29,9 +28,8 @@ defmodule Pleroma.HTTP.Adapter do
def format_proxy(nil), do: nil def format_proxy(nil), do: nil
def format_proxy(proxy_url) do def format_proxy(proxy_url) do
with {:ok, host, port} <- Connection.parse_proxy(proxy_url) do case Connection.parse_proxy(proxy_url) do
{host, port} {:ok, host, port} -> {host, port}
else
{:ok, type, host, port} -> {type, host, port} {:ok, type, host, port} -> {type, host, port}
_ -> nil _ -> nil
end end
@ -40,25 +38,4 @@ defmodule Pleroma.HTTP.Adapter do
@spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword() @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
def maybe_add_proxy(opts, nil), do: opts def maybe_add_proxy(opts, nil), do: opts
def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy) def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
@spec domain_or_fallback(String.t()) :: charlist()
def domain_or_fallback(host) do
case domain_or_ip(host) do
{:domain, domain} -> domain
{:ip, _ip} -> to_charlist(host)
end
end
@spec domain_or_ip(String.t()) :: {host_type(), Connection.host()}
def domain_or_ip(host) do
charlist = to_charlist(host)
case :inet.parse_address(charlist) do
{:error, :einval} ->
{:domain, :idna.encode(charlist)}
{:ok, ip} ->
{:ip, ip}
end
end
end end

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.Adapter.Gun do defmodule Pleroma.HTTP.Adapter.Gun do
@ -42,7 +42,7 @@ defmodule Pleroma.HTTP.Adapter.Gun do
end end
defp add_original(opts, %URI{host: host, port: port}) do defp add_original(opts, %URI{host: host, port: port}) do
formatted_host = Adapter.domain_or_fallback(host) formatted_host = format_host(host)
Keyword.put(opts, :original, "#{formatted_host}:#{port}") Keyword.put(opts, :original, "#{formatted_host}:#{port}")
end end
@ -57,8 +57,7 @@ defmodule Pleroma.HTTP.Adapter.Gun do
cacertfile: CAStore.file_path(), cacertfile: CAStore.file_path(),
depth: 20, depth: 20,
reuse_sessions: false, reuse_sessions: false,
verify_fun: verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: format_host(host)]},
{&:ssl_verify_hostname.verify_fun/3, [check_hostname: Adapter.domain_or_fallback(host)]},
log_level: :warning log_level: :warning
] ]
] ]
@ -139,4 +138,17 @@ defmodule Pleroma.HTTP.Adapter.Gun do
opts opts
end end
end end
@spec format_host(String.t()) :: charlist()
def format_host(host) do
host_charlist = to_charlist(host)
case :inet.parse_address(host_charlist) do
{:error, :einval} ->
:idna.encode(host_charlist)
{:ok, _ip} ->
host_charlist
end
end
end end

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.Adapter.GunTest do defmodule Pleroma.HTTP.Adapter.GunTest do
@ -264,4 +264,23 @@ defmodule Pleroma.HTTP.Adapter.GunTest do
} = Connections.get_state(:gun_connections) } = Connections.get_state(:gun_connections)
end end
end end
describe "format_host/1" do
test "with domain" do
assert Gun.format_host("example.com") == 'example.com'
end
test "with idna domain" do
assert Gun.format_host("ですexample.com") == 'xn--example-183fne.com'
end
test "with ipv4" do
assert Gun.format_host("127.0.0.1") == '127.0.0.1'
end
test "with ipv6" do
assert Gun.format_host("2a03:2880:f10c:83:face:b00c:0:25de") ==
'2a03:2880:f10c:83:face:b00c:0:25de'
end
end
end end

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server # Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.AdapterTest do defmodule Pleroma.HTTP.AdapterTest do
@ -7,44 +7,6 @@ defmodule Pleroma.HTTP.AdapterTest do
alias Pleroma.HTTP.Adapter alias Pleroma.HTTP.Adapter
describe "domain_or_ip/1" do
test "with domain" do
assert Adapter.domain_or_ip("example.com") == {:domain, 'example.com'}
end
test "with idna domain" do
assert Adapter.domain_or_ip("ですexample.com") == {:domain, 'xn--example-183fne.com'}
end
test "with ipv4" do
assert Adapter.domain_or_ip("127.0.0.1") == {:ip, {127, 0, 0, 1}}
end
test "with ipv6" do
assert Adapter.domain_or_ip("2a03:2880:f10c:83:face:b00c:0:25de") ==
{:ip, {10_755, 10_368, 61_708, 131, 64_206, 45_068, 0, 9_694}}
end
end
describe "domain_or_fallback/1" do
test "with domain" do
assert Adapter.domain_or_fallback("example.com") == 'example.com'
end
test "with idna domain" do
assert Adapter.domain_or_fallback("ですexample.com") == 'xn--example-183fne.com'
end
test "with ipv4" do
assert Adapter.domain_or_fallback("127.0.0.1") == '127.0.0.1'
end
test "with ipv6" do
assert Adapter.domain_or_fallback("2a03:2880:f10c:83:face:b00c:0:25de") ==
'2a03:2880:f10c:83:face:b00c:0:25de'
end
end
describe "format_proxy/1" do describe "format_proxy/1" do
test "with nil" do test "with nil" do
assert Adapter.format_proxy(nil) == nil assert Adapter.format_proxy(nil) == nil