http: fix mediaproxy
[akkoma] / lib / pleroma / http / http.ex
1 defmodule Pleroma.HTTP do
2 require HTTPoison
3
4 def request(method, url, body \\ "", headers \\ [], options \\ []) do
5 options =
6 process_request_options(options)
7 |> process_sni_options(url)
8
9 HTTPoison.request(method, url, body, headers, options)
10 end
11
12 defp process_sni_options(options, url) do
13 uri = URI.parse(url)
14 host = uri.host |> to_charlist()
15
16 case uri.scheme do
17 "https" -> options ++ [ssl: [server_name_indication: host]]
18 _ -> options
19 end
20 end
21
22 def process_request_options(options) do
23 config = Application.get_env(:pleroma, :http, [])
24 proxy = Keyword.get(config, :proxy_url, nil)
25 options = options ++ [hackney: [pool: :default]]
26
27 case proxy do
28 nil -> options
29 _ -> options ++ [proxy: proxy]
30 end
31 end
32
33 def get(url, headers \\ [], options \\ []), do: request(:get, url, "", headers, options)
34
35 def post(url, body, headers \\ [], options \\ []),
36 do: request(:post, url, body, headers, options)
37 end