aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/http
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pleroma/http')
-rw-r--r--lib/pleroma/http/adapter_helper.ex4
-rw-r--r--lib/pleroma/http/adapter_helper/default.ex4
-rw-r--r--lib/pleroma/http/adapter_helper/gun.ex20
-rw-r--r--lib/pleroma/http/adapter_helper/hackney.ex18
-rw-r--r--lib/pleroma/http/ex_aws.ex4
-rw-r--r--lib/pleroma/http/http.ex110
-rw-r--r--lib/pleroma/http/request.ex2
-rw-r--r--lib/pleroma/http/request_builder.ex2
-rw-r--r--lib/pleroma/http/tzdata.ex6
-rw-r--r--lib/pleroma/http/web_push.ex12
10 files changed, 48 insertions, 134 deletions
diff --git a/lib/pleroma/http/adapter_helper.ex b/lib/pleroma/http/adapter_helper.ex
index d72297323..c667afd25 100644
--- a/lib/pleroma/http/adapter_helper.ex
+++ b/lib/pleroma/http/adapter_helper.ex
@@ -1,12 +1,12 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.AdapterHelper do
@moduledoc """
Configure Tesla.Client with default and customized adapter options.
"""
- @defaults [pool: :federation]
+ @defaults [pool: :federation, connect_timeout: 5_000, recv_timeout: 5_000]
@type proxy_type() :: :socks4 | :socks5
@type host() :: charlist() | :inet.ip_address()
diff --git a/lib/pleroma/http/adapter_helper/default.ex b/lib/pleroma/http/adapter_helper/default.ex
index e13441316..a1614b9c5 100644
--- a/lib/pleroma/http/adapter_helper/default.ex
+++ b/lib/pleroma/http/adapter_helper/default.ex
@@ -1,3 +1,7 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
defmodule Pleroma.HTTP.AdapterHelper.Default do
alias Pleroma.HTTP.AdapterHelper
diff --git a/lib/pleroma/http/adapter_helper/gun.ex b/lib/pleroma/http/adapter_helper/gun.ex
index 4a967d8f2..251539f34 100644
--- a/lib/pleroma/http/adapter_helper/gun.ex
+++ b/lib/pleroma/http/adapter_helper/gun.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.AdapterHelper.Gun do
@@ -11,12 +11,8 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
require Logger
@defaults [
- connect_timeout: 5_000,
- domain_lookup_timeout: 5_000,
- tls_handshake_timeout: 5_000,
retry: 1,
- retry_timeout: 1000,
- await_up_timeout: 5_000
+ retry_timeout: 1_000
]
@type pool() :: :federation | :upload | :media | :default
@@ -45,19 +41,21 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
end
defp put_timeout(opts) do
+ {recv_timeout, opts} = Keyword.pop(opts, :recv_timeout, pool_timeout(opts[:pool]))
# this is the timeout to receive a message from Gun
- Keyword.put_new(opts, :timeout, pool_timeout(opts[:pool]))
+ # `:timeout` key is used in Tesla
+ Keyword.put(opts, :timeout, recv_timeout)
end
@spec pool_timeout(pool()) :: non_neg_integer()
def pool_timeout(pool) do
- default = Config.get([:pools, :default, :timeout], 5_000)
+ default = Config.get([:pools, :default, :recv_timeout], 5_000)
- Config.get([:pools, pool, :timeout], default)
+ Config.get([:pools, pool, :recv_timeout], default)
end
- @prefix Pleroma.Gun.ConnectionPool
def limiter_setup do
+ prefix = Pleroma.Gun.ConnectionPool
wait = Config.get([:connections_pool, :connection_acquisition_wait])
retries = Config.get([:connections_pool, :connection_acquisition_retries])
@@ -68,7 +66,7 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
max_waiting = Keyword.get(opts, :max_waiting, 10)
result =
- ConcurrentLimiter.new(:"#{@prefix}.#{name}", max_running, max_waiting,
+ ConcurrentLimiter.new(:"#{prefix}.#{name}", max_running, max_waiting,
wait: wait,
max_retries: retries
)
diff --git a/lib/pleroma/http/adapter_helper/hackney.ex b/lib/pleroma/http/adapter_helper/hackney.ex
index 42e3acfec..fe3f91a72 100644
--- a/lib/pleroma/http/adapter_helper/hackney.ex
+++ b/lib/pleroma/http/adapter_helper/hackney.ex
@@ -1,12 +1,13 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
defmodule Pleroma.HTTP.AdapterHelper.Hackney do
@behaviour Pleroma.HTTP.AdapterHelper
@defaults [
- connect_timeout: 10_000,
- recv_timeout: 20_000,
follow_redirect: true,
- force_redirect: true,
- pool: :federation
+ force_redirect: true
]
@spec options(keyword(), URI.t()) :: keyword()
@@ -19,6 +20,7 @@ defmodule Pleroma.HTTP.AdapterHelper.Hackney do
|> Keyword.merge(config_opts)
|> Keyword.merge(connection_opts)
|> add_scheme_opts(uri)
+ |> maybe_add_with_body()
|> Pleroma.HTTP.AdapterHelper.maybe_add_proxy(proxy)
end
@@ -27,4 +29,12 @@ defmodule Pleroma.HTTP.AdapterHelper.Hackney do
end
defp add_scheme_opts(opts, _), do: opts
+
+ defp maybe_add_with_body(opts) do
+ if opts[:max_body] do
+ Keyword.put(opts, :with_body, true)
+ else
+ opts
+ end
+ end
end
diff --git a/lib/pleroma/http/ex_aws.ex b/lib/pleroma/http/ex_aws.ex
index c3f335c73..283590b18 100644
--- a/lib/pleroma/http/ex_aws.ex
+++ b/lib/pleroma/http/ex_aws.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.ExAws do
@@ -11,7 +11,7 @@ defmodule Pleroma.HTTP.ExAws do
@impl true
def request(method, url, body \\ "", headers \\ [], http_opts \\ []) do
- http_opts = Keyword.put_new(http_opts, :adapter, pool: :upload)
+ http_opts = Keyword.put_new(http_opts, :pool, :upload)
case HTTP.request(method, url, body, headers, http_opts) do
{:ok, env} ->
diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex
deleted file mode 100644
index 7bc73f4a0..000000000
--- a/lib/pleroma/http/http.ex
+++ /dev/null
@@ -1,110 +0,0 @@
-# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
-# SPDX-License-Identifier: AGPL-3.0-only
-
-defmodule Pleroma.HTTP do
- @moduledoc """
- Wrapper for `Tesla.request/2`.
- """
-
- alias Pleroma.HTTP.AdapterHelper
- alias Pleroma.HTTP.Request
- alias Pleroma.HTTP.RequestBuilder, as: Builder
- alias Tesla.Client
- alias Tesla.Env
-
- require Logger
-
- @type t :: __MODULE__
- @type method() :: :get | :post | :put | :delete | :head
-
- @doc """
- Performs GET request.
-
- See `Pleroma.HTTP.request/5`
- """
- @spec get(Request.url() | nil, Request.headers(), keyword()) ::
- nil | {:ok, Env.t()} | {:error, any()}
- def get(url, headers \\ [], options \\ [])
- def get(nil, _, _), do: nil
- def get(url, headers, options), do: request(:get, url, "", headers, options)
-
- @spec head(Request.url(), Request.headers(), keyword()) :: {:ok, Env.t()} | {:error, any()}
- def head(url, headers \\ [], options \\ []), do: request(:head, url, "", headers, options)
-
- @doc """
- Performs POST request.
-
- See `Pleroma.HTTP.request/5`
- """
- @spec post(Request.url(), String.t(), Request.headers(), keyword()) ::
- {:ok, Env.t()} | {:error, any()}
- def post(url, body, headers \\ [], options \\ []),
- do: request(:post, url, body, headers, options)
-
- @doc """
- Builds and performs http request.
-
- # Arguments:
- `method` - :get, :post, :put, :delete, :head
- `url` - full url
- `body` - request body
- `headers` - a keyworld list of headers, e.g. `[{"content-type", "text/plain"}]`
- `options` - custom, per-request middleware or adapter options
-
- # Returns:
- `{:ok, %Tesla.Env{}}` or `{:error, error}`
-
- """
- @spec request(method(), Request.url(), String.t(), Request.headers(), keyword()) ::
- {:ok, Env.t()} | {:error, any()}
- def request(method, url, body, headers, options) when is_binary(url) do
- uri = URI.parse(url)
- adapter_opts = AdapterHelper.options(uri, options[:adapter] || [])
-
- options = put_in(options[:adapter], adapter_opts)
- params = options[:params] || []
- request = build_request(method, headers, options, url, body, params)
-
- adapter = Application.get_env(:tesla, :adapter)
-
- client = Tesla.client(adapter_middlewares(adapter), adapter)
-
- maybe_limit(
- fn ->
- request(client, request)
- end,
- adapter,
- adapter_opts
- )
- end
-
- @spec request(Client.t(), keyword()) :: {:ok, Env.t()} | {:error, any()}
- def request(client, request), do: Tesla.request(client, request)
-
- defp build_request(method, headers, options, url, body, params) do
- Builder.new()
- |> Builder.method(method)
- |> Builder.headers(headers)
- |> Builder.opts(options)
- |> Builder.url(url)
- |> Builder.add_param(:body, :body, body)
- |> Builder.add_param(:query, :query, params)
- |> Builder.convert_to_keyword()
- end
-
- @prefix Pleroma.Gun.ConnectionPool
- defp maybe_limit(fun, Tesla.Adapter.Gun, opts) do
- ConcurrentLimiter.limit(:"#{@prefix}.#{opts[:pool] || :default}", fun)
- end
-
- defp maybe_limit(fun, _, _) do
- fun.()
- end
-
- defp adapter_middlewares(Tesla.Adapter.Gun) do
- [Tesla.Middleware.FollowRedirects, Pleroma.Tesla.Middleware.ConnectionPool]
- end
-
- defp adapter_middlewares(_), do: []
-end
diff --git a/lib/pleroma/http/request.ex b/lib/pleroma/http/request.ex
index 761bd6ccf..d906024de 100644
--- a/lib/pleroma/http/request.ex
+++ b/lib/pleroma/http/request.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.Request do
diff --git a/lib/pleroma/http/request_builder.ex b/lib/pleroma/http/request_builder.ex
index 8a44a001d..631c927af 100644
--- a/lib/pleroma/http/request_builder.ex
+++ b/lib/pleroma/http/request_builder.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.RequestBuilder do
diff --git a/lib/pleroma/http/tzdata.ex b/lib/pleroma/http/tzdata.ex
index 4539ac359..77e1b537e 100644
--- a/lib/pleroma/http/tzdata.ex
+++ b/lib/pleroma/http/tzdata.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.Tzdata do
@@ -11,7 +11,7 @@ defmodule Pleroma.HTTP.Tzdata do
@impl true
def get(url, headers, options) do
- options = Keyword.put_new(options, :adapter, pool: :default)
+ options = Keyword.put_new(options, :pool, :default)
with {:ok, %Tesla.Env{} = env} <- HTTP.get(url, headers, options) do
{:ok, {env.status, env.headers, env.body}}
@@ -20,7 +20,7 @@ defmodule Pleroma.HTTP.Tzdata do
@impl true
def head(url, headers, options) do
- options = Keyword.put_new(options, :adapter, pool: :default)
+ options = Keyword.put_new(options, :pool, :default)
with {:ok, %Tesla.Env{} = env} <- HTTP.head(url, headers, options) do
{:ok, {env.status, env.headers}}
diff --git a/lib/pleroma/http/web_push.ex b/lib/pleroma/http/web_push.ex
new file mode 100644
index 000000000..16bbe6e8c
--- /dev/null
+++ b/lib/pleroma/http/web_push.ex
@@ -0,0 +1,12 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.HTTP.WebPush do
+ @moduledoc false
+
+ def post(url, payload, headers, options \\ []) do
+ list_headers = Map.to_list(headers)
+ Pleroma.HTTP.post(url, payload, list_headers, options)
+ end
+end