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
10 alias Pleroma.Config
11 alias Pleroma.HTTP.AdapterHelper
12
13 require Logger
14
15 @defaults [pool: :federation]
16
17 @type ip_address :: ipv4_address() | ipv6_address()
18 @type ipv4_address :: {0..255, 0..255, 0..255, 0..255}
19 @type ipv6_address ::
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()
23
24 @doc """
25 Merge default connection & adapter options with received ones.
26 """
27
28 @spec options(URI.t(), keyword()) :: keyword()
29 def options(%URI{} = uri, opts \\ []) do
30 @defaults
31 |> pool_timeout()
32 |> Keyword.merge(opts)
33 |> adapter().options(uri)
34 end
35
36 defp pool_timeout(opts) do
37 {config_key, default} =
38 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
39 {:pools, Config.get([:pools, :default, :timeout])}
40 else
41 {:hackney_pools, 10_000}
42 end
43
44 timeout = Config.get([config_key, opts[:pool], :timeout], default)
45
46 Keyword.merge(opts, timeout: timeout)
47 end
48
49 @spec after_request(keyword()) :: :ok
50 def after_request(opts), do: adapter().after_request(opts)
51
52 defp adapter do
53 case Application.get_env(:tesla, :adapter) do
54 Tesla.Adapter.Gun -> AdapterHelper.Gun
55 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
56 _ -> AdapterHelper
57 end
58 end
59
60 @spec parse_proxy(String.t() | tuple() | nil) ::
61 {:ok, host(), pos_integer()}
62 | {:ok, proxy_type(), host(), pos_integer()}
63 | {:error, atom()}
64 | nil
65
66 def parse_proxy(nil), do: nil
67
68 def parse_proxy(proxy) when is_binary(proxy) do
69 with [host, port] <- String.split(proxy, ":"),
70 {port, ""} <- Integer.parse(port) do
71 {:ok, parse_host(host), port}
72 else
73 {_, _} ->
74 Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
75 {:error, :invalid_proxy_port}
76
77 :error ->
78 Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
79 {:error, :invalid_proxy_port}
80
81 _ ->
82 Logger.warn("parsing proxy fail #{inspect(proxy)}")
83 {:error, :invalid_proxy}
84 end
85 end
86
87 def parse_proxy(proxy) when is_tuple(proxy) do
88 with {type, host, port} <- proxy do
89 {:ok, type, parse_host(host), port}
90 else
91 _ ->
92 Logger.warn("parsing proxy fail #{inspect(proxy)}")
93 {:error, :invalid_proxy}
94 end
95 end
96
97 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | ip_address()
98 def parse_host(host) when is_list(host), do: host
99 def parse_host(host) when is_atom(host), do: to_charlist(host)
100
101 def parse_host(host) when is_binary(host) do
102 host = to_charlist(host)
103
104 case :inet.parse_address(host) do
105 {:error, :einval} -> host
106 {:ok, ip} -> ip
107 end
108 end
109 end