improvements and fixes for http requests
[akkoma] / lib / pleroma / http / adapter_helper.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.AdapterHelper do
6 @moduledoc """
7 Configure Tesla.Client with default and customized adapter options.
8 """
9 @defaults [pool: :federation]
10
11 @type proxy_type() :: :socks4 | :socks5
12 @type host() :: charlist() | :inet.ip_address()
13 @type pool() :: :federation | :upload | :media | :default
14
15 alias Pleroma.Config
16 alias Pleroma.HTTP.AdapterHelper
17 require Logger
18
19 @type proxy ::
20 {Connection.host(), pos_integer()}
21 | {Connection.proxy_type(), Connection.host(), pos_integer()}
22
23 @callback options(keyword(), URI.t()) :: keyword()
24 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
25
26 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
27 def format_proxy(nil), do: nil
28
29 def format_proxy(proxy_url) do
30 case parse_proxy(proxy_url) do
31 {:ok, host, port} -> {host, port}
32 {:ok, type, host, port} -> {type, host, port}
33 _ -> nil
34 end
35 end
36
37 @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
38 def maybe_add_proxy(opts, nil), do: opts
39 def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
40
41 @doc """
42 Merge default connection & adapter options with received ones.
43 """
44
45 @spec options(URI.t(), keyword()) :: keyword()
46 def options(%URI{} = uri, opts \\ []) do
47 @defaults
48 |> Keyword.merge(opts)
49 |> put_timeout()
50 |> adapter_helper().options(uri)
51 end
52
53 @spec pool_timeout(pool()) :: non_neg_integer()
54 def pool_timeout(pool) do
55 {config_key, default} =
56 if adapter() == Tesla.Adapter.Gun do
57 {:pools, Config.get([:pools, :default, :timeout], 5_000)}
58 else
59 {:hackney_pools, 10_000}
60 end
61
62 Config.get([config_key, pool, :timeout], default)
63 end
64
65 # For Hackney, this is the time a connection can stay idle in the pool.
66 # For Gun, this is the timeout to receive a message from Gun.
67 defp put_timeout(opts) do
68 Keyword.put_new(opts, :timeout, pool_timeout(opts[:pool]))
69 end
70
71 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
72 defp adapter, do: Application.get_env(:tesla, :adapter)
73
74 defp adapter_helper do
75 case adapter() do
76 Tesla.Adapter.Gun -> AdapterHelper.Gun
77 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
78 _ -> AdapterHelper.Default
79 end
80 end
81
82 @spec parse_proxy(String.t() | tuple() | nil) ::
83 {:ok, host(), pos_integer()}
84 | {:ok, proxy_type(), host(), pos_integer()}
85 | {:error, atom()}
86 | nil
87
88 def parse_proxy(nil), do: nil
89
90 def parse_proxy(proxy) when is_binary(proxy) do
91 with [host, port] <- String.split(proxy, ":"),
92 {port, ""} <- Integer.parse(port) do
93 {:ok, parse_host(host), port}
94 else
95 {_, _} ->
96 Logger.warn("Parsing port failed #{inspect(proxy)}")
97 {:error, :invalid_proxy_port}
98
99 :error ->
100 Logger.warn("Parsing port failed #{inspect(proxy)}")
101 {:error, :invalid_proxy_port}
102
103 _ ->
104 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
105 {:error, :invalid_proxy}
106 end
107 end
108
109 def parse_proxy(proxy) when is_tuple(proxy) do
110 with {type, host, port} <- proxy do
111 {:ok, type, parse_host(host), port}
112 else
113 _ ->
114 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
115 {:error, :invalid_proxy}
116 end
117 end
118
119 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
120 def parse_host(host) when is_list(host), do: host
121 def parse_host(host) when is_atom(host), do: to_charlist(host)
122
123 def parse_host(host) when is_binary(host) do
124 host = to_charlist(host)
125
126 case :inet.parse_address(host) do
127 {:error, :einval} -> host
128 {:ok, ip} -> ip
129 end
130 end
131
132 @spec format_host(String.t()) :: charlist()
133 def format_host(host) do
134 host_charlist = to_charlist(host)
135
136 case :inet.parse_address(host_charlist) do
137 {:error, :einval} ->
138 :idna.encode(host_charlist)
139
140 {:ok, _ip} ->
141 host_charlist
142 end
143 end
144 end