X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fhttp%2Fconnection.ex;h=ebacf7902e2c9b10523c7c25d80b70841ef5b841;hb=607e06c245e8a36d7b720d71b45e1fc962ede3ab;hp=e2d7afbbde19850e9ef64c8673c752d773614686;hpb=f604f9e47061b9d47c1bb62cc7aaf44fabdf69b3;p=akkoma
diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex
index e2d7afbbd..ebacf7902 100644
--- a/lib/pleroma/http/connection.ex
+++ b/lib/pleroma/http/connection.ex
@@ -1,11 +1,19 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.Connection do
@moduledoc """
Configure Tesla.Client with default and customized adapter options.
"""
+
+ alias Pleroma.Config
+ alias Pleroma.HTTP.AdapterHelper
+
+ require Logger
+
+ @defaults [pool: :federation]
+
@type ip_address :: ipv4_address() | ipv6_address()
@type ipv4_address :: {0..255, 0..255, 0..255, 0..255}
@type ipv6_address ::
@@ -13,13 +21,6 @@ defmodule Pleroma.HTTP.Connection do
@type proxy_type() :: :socks4 | :socks5
@type host() :: charlist() | ip_address()
- @defaults [pool: :federation]
-
- require Logger
-
- alias Pleroma.Config
- alias Pleroma.HTTP.Adapter
-
@doc """
Merge default connection & adapter options with received ones.
"""
@@ -29,12 +30,12 @@ defmodule Pleroma.HTTP.Connection do
@defaults
|> pool_timeout()
|> Keyword.merge(opts)
- |> adapter().options(uri)
+ |> adapter_helper().options(uri)
end
defp pool_timeout(opts) do
{config_key, default} =
- if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
+ if adapter() == Tesla.Adapter.Gun do
{:pools, Config.get([:pools, :default, :timeout])}
else
{:hackney_pools, 10_000}
@@ -46,13 +47,15 @@ defmodule Pleroma.HTTP.Connection do
end
@spec after_request(keyword()) :: :ok
- def after_request(opts), do: adapter().after_request(opts)
+ def after_request(opts), do: adapter_helper().after_request(opts)
- defp adapter do
- case Application.get_env(:tesla, :adapter) do
- Tesla.Adapter.Gun -> Adapter.Gun
- Tesla.Adapter.Hackney -> Adapter.Hackney
- _ -> Adapter
+ defp adapter, do: Application.get_env(:tesla, :adapter)
+
+ defp adapter_helper do
+ case adapter() do
+ Tesla.Adapter.Gun -> AdapterHelper.Gun
+ Tesla.Adapter.Hackney -> AdapterHelper.Hackney
+ _ -> AdapterHelper
end
end
@@ -70,16 +73,16 @@ defmodule Pleroma.HTTP.Connection do
{:ok, parse_host(host), port}
else
{_, _} ->
- Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
- {:error, :error_parsing_port_in_proxy}
+ Logger.warn("Parsing port failed #{inspect(proxy)}")
+ {:error, :invalid_proxy_port}
:error ->
- Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
- {:error, :error_parsing_port_in_proxy}
+ Logger.warn("Parsing port failed #{inspect(proxy)}")
+ {:error, :invalid_proxy_port}
_ ->
- Logger.warn("parsing proxy fail #{inspect(proxy)}")
- {:error, :error_parsing_proxy}
+ Logger.warn("Parsing proxy failed #{inspect(proxy)}")
+ {:error, :invalid_proxy}
end
end
@@ -88,8 +91,8 @@ defmodule Pleroma.HTTP.Connection do
{:ok, type, parse_host(host), port}
else
_ ->
- Logger.warn("parsing proxy fail #{inspect(proxy)}")
- {:error, :error_parsing_proxy}
+ Logger.warn("Parsing proxy failed #{inspect(proxy)}")
+ {:error, :invalid_proxy}
end
end
@@ -105,4 +108,17 @@ defmodule Pleroma.HTTP.Connection do
{:ok, ip} -> ip
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