Merge branch 'develop' into gun
[akkoma] / lib / pleroma / http / adapter_helper / gun.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.Gun do
6 @behaviour Pleroma.HTTP.AdapterHelper
7
8 alias Pleroma.HTTP.AdapterHelper
9 alias Pleroma.Pool.Connections
10
11 require Logger
12
13 @defaults [
14 connect_timeout: 5_000,
15 domain_lookup_timeout: 5_000,
16 tls_handshake_timeout: 5_000,
17 retry: 1,
18 retry_timeout: 1000,
19 await_up_timeout: 5_000
20 ]
21
22 @spec options(keyword(), URI.t()) :: keyword()
23 def options(connection_opts \\ [], %URI{} = uri) do
24 formatted_proxy =
25 Pleroma.Config.get([:http, :proxy_url], nil)
26 |> AdapterHelper.format_proxy()
27
28 config_opts = Pleroma.Config.get([:http, :adapter], [])
29
30 @defaults
31 |> Keyword.merge(config_opts)
32 |> add_scheme_opts(uri)
33 |> AdapterHelper.maybe_add_proxy(formatted_proxy)
34 |> maybe_get_conn(uri, connection_opts)
35 end
36
37 @spec after_request(keyword()) :: :ok
38 def after_request(opts) do
39 if opts[:conn] && opts[:body_as] != :chunks do
40 Connections.checkout(opts[:conn], self(), :gun_connections)
41 end
42
43 :ok
44 end
45
46 defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
47
48 defp add_scheme_opts(opts, %URI{scheme: "https", host: host}) do
49 adapter_opts = [
50 certificates_verification: true,
51 transport: :tls,
52 tls_opts: [
53 verify: :verify_peer,
54 cacertfile: CAStore.file_path(),
55 depth: 20,
56 reuse_sessions: false,
57 verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: format_host(host)]},
58 log_level: :warning
59 ]
60 ]
61
62 Keyword.merge(opts, adapter_opts)
63 end
64
65 defp maybe_get_conn(adapter_opts, uri, connection_opts) do
66 {receive_conn?, opts} =
67 adapter_opts
68 |> Keyword.merge(connection_opts)
69 |> Keyword.pop(:receive_conn, true)
70
71 if Connections.alive?(:gun_connections) and receive_conn? do
72 try_to_get_conn(uri, opts)
73 else
74 opts
75 end
76 end
77
78 defp try_to_get_conn(uri, opts) do
79 case Connections.checkin(uri, :gun_connections) do
80 nil ->
81 Logger.debug(
82 "Gun connections pool checkin was not successful. Trying to open conn for next request."
83 )
84
85 Task.start(fn -> Pleroma.Gun.Conn.open(uri, :gun_connections, opts) end)
86 opts
87
88 conn when is_pid(conn) ->
89 Logger.debug("received conn #{inspect(conn)} #{Connections.compose_uri_log(uri)}")
90
91 opts
92 |> Keyword.put(:conn, conn)
93 |> Keyword.put(:close_conn, false)
94 end
95 end
96
97 @spec format_host(String.t()) :: charlist()
98 def format_host(host) do
99 host_charlist = to_charlist(host)
100
101 case :inet.parse_address(host_charlist) do
102 {:error, :einval} ->
103 :idna.encode(host_charlist)
104
105 {:ok, _ip} ->
106 host_charlist
107 end
108 end
109 end