Merge branch 'develop' into gun
[akkoma] / lib / pleroma / http / connection.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.Connection do
6 @moduledoc """
7 Configure Tesla.Client with default and customized adapter options.
8 """
9 @type ip_address :: ipv4_address() | ipv6_address()
10 @type ipv4_address :: {0..255, 0..255, 0..255, 0..255}
11 @type ipv6_address ::
12 {0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535}
13 @type proxy_type() :: :socks4 | :socks5
14 @type host() :: charlist() | ip_address()
15
16 @defaults [pool: :federation]
17
18 require Logger
19
20 alias Pleroma.Config
21 alias Pleroma.HTTP.AdapterHelper
22
23 @doc """
24 Merge default connection & adapter options with received ones.
25 """
26
27 @spec options(URI.t(), keyword()) :: keyword()
28 def options(%URI{} = uri, opts \\ []) do
29 @defaults
30 |> pool_timeout()
31 |> Keyword.merge(opts)
32 |> adapter().options(uri)
33 end
34
35 defp pool_timeout(opts) do
36 {config_key, default} =
37 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
38 {:pools, Config.get([:pools, :default, :timeout])}
39 else
40 {:hackney_pools, 10_000}
41 end
42
43 timeout = Config.get([config_key, opts[:pool], :timeout], default)
44
45 Keyword.merge(opts, timeout: timeout)
46 end
47
48 @spec after_request(keyword()) :: :ok
49 def after_request(opts), do: adapter().after_request(opts)
50
51 defp adapter do
52 case Application.get_env(:tesla, :adapter) do
53 Tesla.Adapter.Gun -> AdapterHelper.Gun
54 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
55 _ -> AdapterHelper
56 end
57 end
58
59 @spec parse_proxy(String.t() | tuple() | nil) ::
60 {:ok, host(), pos_integer()}
61 | {:ok, proxy_type(), host(), pos_integer()}
62 | {:error, atom()}
63 | nil
64
65 def parse_proxy(nil), do: nil
66
67 def parse_proxy(proxy) when is_binary(proxy) do
68 with [host, port] <- String.split(proxy, ":"),
69 {port, ""} <- Integer.parse(port) do
70 {:ok, parse_host(host), port}
71 else
72 {_, _} ->
73 Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
74 {:error, :invalid_proxy_port}
75
76 :error ->
77 Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
78 {:error, :invalid_proxy_port}
79
80 _ ->
81 Logger.warn("parsing proxy fail #{inspect(proxy)}")
82 {:error, :invalid_proxy}
83 end
84 end
85
86 def parse_proxy(proxy) when is_tuple(proxy) do
87 with {type, host, port} <- proxy do
88 {:ok, type, parse_host(host), port}
89 else
90 _ ->
91 Logger.warn("parsing proxy fail #{inspect(proxy)}")
92 {:error, :invalid_proxy}
93 end
94 end
95
96 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | ip_address()
97 def parse_host(host) when is_list(host), do: host
98 def parse_host(host) when is_atom(host), do: to_charlist(host)
99
100 def parse_host(host) when is_binary(host) do
101 host = to_charlist(host)
102
103 case :inet.parse_address(host) do
104 {:error, :einval} -> host
105 {:ok, ip} -> ip
106 end
107 end
108 end