1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP.Connection do
7 Configure Tesla.Client with default and customized adapter options.
11 alias Pleroma.HTTP.AdapterHelper
15 @defaults [pool: :federation]
17 @type ip_address :: ipv4_address() | ipv6_address()
18 @type ipv4_address :: {0..255, 0..255, 0..255, 0..255}
20 {0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535}
21 @type proxy_type() :: :socks4 | :socks5
22 @type host() :: charlist() | ip_address()
25 Merge default connection & adapter options with received ones.
28 @spec options(URI.t(), keyword()) :: keyword()
29 def options(%URI{} = uri, opts \\ []) do
32 |> Keyword.merge(opts)
33 |> adapter_helper().options(uri)
36 defp pool_timeout(opts) do
37 {config_key, default} =
38 if adapter() == Tesla.Adapter.Gun do
39 {:pools, Config.get([:pools, :default, :timeout])}
41 {:hackney_pools, 10_000}
44 timeout = Config.get([config_key, opts[:pool], :timeout], default)
46 Keyword.merge(opts, timeout: timeout)
49 @spec after_request(keyword()) :: :ok
50 def after_request(opts), do: adapter_helper().after_request(opts)
52 defp adapter, do: Application.get_env(:tesla, :adapter)
54 defp adapter_helper do
56 Tesla.Adapter.Gun -> AdapterHelper.Gun
57 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
62 @spec parse_proxy(String.t() | tuple() | nil) ::
63 {:ok, host(), pos_integer()}
64 | {:ok, proxy_type(), host(), pos_integer()}
68 def parse_proxy(nil), do: nil
70 def parse_proxy(proxy) when is_binary(proxy) do
71 with [host, port] <- String.split(proxy, ":"),
72 {port, ""} <- Integer.parse(port) do
73 {:ok, parse_host(host), port}
76 Logger.warn("Parsing port failed #{inspect(proxy)}")
77 {:error, :invalid_proxy_port}
80 Logger.warn("Parsing port failed #{inspect(proxy)}")
81 {:error, :invalid_proxy_port}
84 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
85 {:error, :invalid_proxy}
89 def parse_proxy(proxy) when is_tuple(proxy) do
90 with {type, host, port} <- proxy do
91 {:ok, type, parse_host(host), port}
94 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
95 {:error, :invalid_proxy}
99 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | ip_address()
100 def parse_host(host) when is_list(host), do: host
101 def parse_host(host) when is_atom(host), do: to_charlist(host)
103 def parse_host(host) when is_binary(host) do
104 host = to_charlist(host)
106 case :inet.parse_address(host) do
107 {:error, :einval} -> host
112 @spec format_host(String.t()) :: charlist()
113 def format_host(host) do
114 host_charlist = to_charlist(host)
116 case :inet.parse_address(host_charlist) do
118 :idna.encode(host_charlist)