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