moving some logic to tesla adapter

- checking original inside gun adapter
- flushing streams on max_body error
This commit is contained in:
Alexander Strizhakov 2020-03-04 18:13:24 +03:00
parent 22d52f5691
commit d6bebd4f9c
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
6 changed files with 6 additions and 34 deletions

View file

@ -26,7 +26,6 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
@defaults @defaults
|> Keyword.merge(Pleroma.Config.get([:http, :adapter], [])) |> Keyword.merge(Pleroma.Config.get([:http, :adapter], []))
|> add_original(uri)
|> add_scheme_opts(uri) |> add_scheme_opts(uri)
|> AdapterHelper.maybe_add_proxy(AdapterHelper.format_proxy(proxy)) |> AdapterHelper.maybe_add_proxy(AdapterHelper.format_proxy(proxy))
|> maybe_get_conn(uri, connection_opts) |> maybe_get_conn(uri, connection_opts)
@ -42,17 +41,12 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
:ok :ok
end end
defp add_original(opts, %URI{host: host, port: port}) do
formatted_host = format_host(host)
Keyword.put(opts, :original, "#{formatted_host}:#{port}")
end
defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
defp add_scheme_opts(opts, %URI{scheme: "https", host: host, port: port}) do defp add_scheme_opts(opts, %URI{scheme: "https", host: host}) do
adapter_opts = [ adapter_opts = [
certificates_verification: true, certificates_verification: true,
transport: :tls,
tls_opts: [ tls_opts: [
verify: :verify_peer, verify: :verify_peer,
cacertfile: CAStore.file_path(), cacertfile: CAStore.file_path(),
@ -63,13 +57,6 @@ defmodule Pleroma.HTTP.AdapterHelper.Gun do
] ]
] ]
adapter_opts =
if port != 443 do
Keyword.put(adapter_opts, :transport, :tls)
else
adapter_opts
end
Keyword.merge(opts, adapter_opts) Keyword.merge(opts, adapter_opts)
end end

View file

@ -28,12 +28,7 @@ defmodule Pleroma.Pool.Request do
end end
@impl true @impl true
def handle_info({:gun_data, _conn, stream, _, _}, state) do def handle_info({:gun_data, _conn, _stream, _, _}, state) do
# in some cases if we reuse conn and got {:error, :body_too_large}
# gun continues to send messages to this process,
# so we flush messages for this request
:ok = :gun.flush(stream)
{:noreply, state} {:noreply, state}
end end
@ -49,8 +44,7 @@ defmodule Pleroma.Pool.Request do
end end
@impl true @impl true
def handle_info({:gun_error, _conn, stream, _error}, state) do def handle_info({:gun_error, _conn, _stream, _error}, state) do
:ok = :gun.flush(stream)
{:noreply, state} {:noreply, state}
end end

View file

@ -122,7 +122,7 @@ defmodule Pleroma.Mixfile do
# {:tesla, "~> 1.3", override: true}, # {:tesla, "~> 1.3", override: true},
{:tesla, {:tesla,
git: "https://git.pleroma.social/pleroma/elixir-libraries/tesla.git", git: "https://git.pleroma.social/pleroma/elixir-libraries/tesla.git",
ref: "922cc3db13b421763edbea76246b8ea61c38c6fa", ref: "67436cf003d40370e944462649193706bb22ca35",
override: true}, override: true},
{:castore, "~> 0.1"}, {:castore, "~> 0.1"},
{:cowlib, "~> 2.8", override: true}, {:cowlib, "~> 2.8", override: true},

View file

@ -102,7 +102,7 @@
"swoosh": {:hex, :swoosh, "0.23.5", "bfd9404bbf5069b1be2ffd317923ce57e58b332e25dbca2a35dedd7820dfee5a", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "e3928e1d2889a308aaf3e42755809ac21cffd77cb58eef01cbfdab4ce2fd1e21"}, "swoosh": {:hex, :swoosh, "0.23.5", "bfd9404bbf5069b1be2ffd317923ce57e58b332e25dbca2a35dedd7820dfee5a", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "e3928e1d2889a308aaf3e42755809ac21cffd77cb58eef01cbfdab4ce2fd1e21"},
"syslog": {:hex, :syslog, "1.0.6", "995970c9aa7feb380ac493302138e308d6e04fd57da95b439a6df5bb3bf75076", [:rebar3], [], "hexpm", "769ddfabd0d2a16f3f9c17eb7509951e0ca4f68363fb26f2ee51a8ec4a49881a"}, "syslog": {:hex, :syslog, "1.0.6", "995970c9aa7feb380ac493302138e308d6e04fd57da95b439a6df5bb3bf75076", [:rebar3], [], "hexpm", "769ddfabd0d2a16f3f9c17eb7509951e0ca4f68363fb26f2ee51a8ec4a49881a"},
"telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"},
"tesla": {:git, "https://git.pleroma.social/pleroma/elixir-libraries/tesla.git", "922cc3db13b421763edbea76246b8ea61c38c6fa", [ref: "922cc3db13b421763edbea76246b8ea61c38c6fa"]}, "tesla": {:git, "https://git.pleroma.social/pleroma/elixir-libraries/tesla.git", "67436cf003d40370e944462649193706bb22ca35", [ref: "67436cf003d40370e944462649193706bb22ca35"]},
"timex": {:hex, :timex, "3.6.1", "efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "f354efb2400dd7a80fd9eb6c8419068c4f632da4ac47f3d8822d6e33f08bc852"}, "timex": {:hex, :timex, "3.6.1", "efdf56d0e67a6b956cc57774353b0329c8ab7726766a11547e529357ffdc1d56", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5 or ~> 1.0.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "f354efb2400dd7a80fd9eb6c8419068c4f632da4ac47f3d8822d6e33f08bc852"},
"trailing_format_plug": {:hex, :trailing_format_plug, "0.0.7", "64b877f912cf7273bed03379936df39894149e35137ac9509117e59866e10e45", [:mix], [{:plug, "> 0.12.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "bd4fde4c15f3e993a999e019d64347489b91b7a9096af68b2bdadd192afa693f"}, "trailing_format_plug": {:hex, :trailing_format_plug, "0.0.7", "64b877f912cf7273bed03379936df39894149e35137ac9509117e59866e10e45", [:mix], [{:plug, "> 0.12.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "bd4fde4c15f3e993a999e019d64347489b91b7a9096af68b2bdadd192afa693f"},
"tzdata": {:hex, :tzdata, "0.5.22", "f2ba9105117ee0360eae2eca389783ef7db36d533899b2e84559404dbc77ebb8", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "cd66c8a1e6a9e121d1f538b01bef459334bb4029a1ffb4eeeb5e4eae0337e7b6"}, "tzdata": {:hex, :tzdata, "0.5.22", "f2ba9105117ee0360eae2eca389783ef7db36d533899b2e84559404dbc77ebb8", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "cd66c8a1e6a9e121d1f538b01bef459334bb4029a1ffb4eeeb5e4eae0337e7b6"},
@ -112,4 +112,3 @@
"web_push_encryption": {:hex, :web_push_encryption, "0.2.3", "a0ceab85a805a30852f143d22d71c434046fbdbafbc7292e7887cec500826a80", [:mix], [{:httpoison, "~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:poison, "~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "9315c8f37c108835cf3f8e9157d7a9b8f420a34f402d1b1620a31aed5b93ecdf"}, "web_push_encryption": {:hex, :web_push_encryption, "0.2.3", "a0ceab85a805a30852f143d22d71c434046fbdbafbc7292e7887cec500826a80", [:mix], [{:httpoison, "~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jose, "~> 1.8", [hex: :jose, repo: "hexpm", optional: false]}, {:poison, "~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "9315c8f37c108835cf3f8e9157d7a9b8f420a34f402d1b1620a31aed5b93ecdf"},
"websocket_client": {:git, "https://github.com/jeremyong/websocket_client.git", "9a6f65d05ebf2725d62fb19262b21f1805a59fbf", []}, "websocket_client": {:git, "https://github.com/jeremyong/websocket_client.git", "9a6f65d05ebf2725d62fb19262b21f1805a59fbf", []},
} }

View file

@ -35,8 +35,6 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
{&:ssl_verify_hostname.verify_fun/3, [check_hostname: 'example.com']} {&:ssl_verify_hostname.verify_fun/3, [check_hostname: 'example.com']}
assert File.exists?(tls_opts[:cacertfile]) assert File.exists?(tls_opts[:cacertfile])
assert opts[:original] == "example.com:443"
end end
test "https ipv4 with default port" do test "https ipv4 with default port" do
@ -46,8 +44,6 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
assert opts[:tls_opts][:verify_fun] == assert opts[:tls_opts][:verify_fun] ==
{&:ssl_verify_hostname.verify_fun/3, [check_hostname: '127.0.0.1']} {&:ssl_verify_hostname.verify_fun/3, [check_hostname: '127.0.0.1']}
assert opts[:original] == "127.0.0.1:443"
end end
test "https ipv6 with default port" do test "https ipv6 with default port" do
@ -58,8 +54,6 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
assert opts[:tls_opts][:verify_fun] == assert opts[:tls_opts][:verify_fun] ==
{&:ssl_verify_hostname.verify_fun/3, {&:ssl_verify_hostname.verify_fun/3,
[check_hostname: '2a03:2880:f10c:83:face:b00c:0:25de']} [check_hostname: '2a03:2880:f10c:83:face:b00c:0:25de']}
assert opts[:original] == "2a03:2880:f10c:83:face:b00c:0:25de:443"
end end
test "https url with non standart port" do test "https url with non standart port" do
@ -129,7 +123,6 @@ defmodule Pleroma.HTTP.AdapterHelper.GunTest do
assert tls_opts[:depth] == 20 assert tls_opts[:depth] == 20
assert tls_opts[:reuse_sessions] == false assert tls_opts[:reuse_sessions] == false
assert opts[:original] == "some-domain.com:443"
assert opts[:close_conn] == false assert opts[:close_conn] == false
assert is_pid(opts[:conn]) assert is_pid(opts[:conn])
end end

View file

@ -134,7 +134,6 @@ defmodule Pleroma.HTTP.ConnectionTest do
assert tls_opts[:depth] == 20 assert tls_opts[:depth] == 20
assert tls_opts[:reuse_sessions] == false assert tls_opts[:reuse_sessions] == false
assert opts[:original] == "some-domain.com:443"
assert opts[:close_conn] == false assert opts[:close_conn] == false
assert is_pid(opts[:conn]) assert is_pid(opts[:conn])
end end