Commit 5b837592 authored by Alexander Strizhakov's avatar Alexander Strizhakov

reverse proxy for mint adapter

parent c20e64c4
Pipeline #17035 passed with stages
in 4 minutes and 35 seconds
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
defmodule Pleroma.ReverseProxy.Client.Tesla do defmodule Pleroma.ReverseProxy.Client.Tesla do
@behaviour Pleroma.ReverseProxy.Client @behaviour Pleroma.ReverseProxy.Client
@adapters [Tesla.Adapter.Gun] @adapters [Tesla.Adapter.Gun, Tesla.Adapter.Mint]
def request(method, url, headers, body, opts \\ []) do def request(method, url, headers, body, opts \\ []) do
opts = Keyword.put(opts, :body_as, :chunks) opts = Keyword.put(opts, :body_as, :chunks)
...@@ -28,6 +28,24 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do ...@@ -28,6 +28,24 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
def stream_body(%{fin: true}), do: :done def stream_body(%{fin: true}), do: :done
def stream_body(%{body: body} = client) do
case body do
{:fin, response} ->
{:ok, response, Map.put(client, :fin, true)}
{:nofin, response} ->
client = Map.delete(client, :body)
case response do
nil ->
stream_body(client)
_binary ->
{:ok, response, client}
end
end
end
def stream_body(client) do def stream_body(client) do
case read_chunk!(client) do case read_chunk!(client) do
{:fin, body} -> {:ok, body, Map.put(client, :fin, true)} {:fin, body} -> {:ok, body, Map.put(client, :fin, true)}
...@@ -37,22 +55,27 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do ...@@ -37,22 +55,27 @@ defmodule Pleroma.ReverseProxy.Client.Tesla do
end end
defp read_chunk!(%{pid: pid, stream: stream, opts: opts}) do defp read_chunk!(%{pid: pid, stream: stream, opts: opts}) do
adapter = Application.get_env(:tesla, :adapter) adapter = check_adapter()
unless adapter in @adapters do
raise "#{adapter} doesn't support reading body in chunks"
end
adapter.read_chunk(pid, stream, opts) adapter.read_chunk(pid, stream, opts)
end end
defp read_chunk!(%{conn: conn, opts: opts}) do
adapter = check_adapter()
adapter.read_chunk(conn, opts)
end
def close(pid) do def close(pid) do
adapter = check_adapter()
adapter.close(pid)
end
defp check_adapter do
adapter = Application.get_env(:tesla, :adapter) adapter = Application.get_env(:tesla, :adapter)
unless adapter in @adapters do unless adapter in @adapters do
raise "#{adapter} doesn't support closing connection" raise "#{adapter} doesn't support reading body in chunks"
end end
adapter.close(pid) adapter
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment