2020-02-11 00:12:57 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-02-11 00:12:57 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.ReverseProxy.Client.Tesla do
|
2020-03-13 00:37:57 -06:00
|
|
|
@behaviour Pleroma.ReverseProxy.Client
|
|
|
|
|
2020-07-26 10:18:21 -06:00
|
|
|
alias Pleroma.Gun.ConnectionPool
|
|
|
|
|
2020-02-11 00:12:57 -07:00
|
|
|
@type headers() :: [{String.t(), String.t()}]
|
|
|
|
@type status() :: pos_integer()
|
|
|
|
|
|
|
|
@spec request(atom(), String.t(), headers(), String.t(), keyword()) ::
|
|
|
|
{:ok, status(), headers}
|
|
|
|
| {:ok, status(), headers, map()}
|
|
|
|
| {:error, atom() | String.t()}
|
|
|
|
| no_return()
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def request(method, url, headers, body, opts \\ []) do
|
2020-03-03 02:53:37 -07:00
|
|
|
check_adapter()
|
2020-02-11 00:12:57 -07:00
|
|
|
|
2020-03-13 00:37:57 -06:00
|
|
|
opts = Keyword.put(opts, :body_as, :chunks)
|
2020-03-03 04:56:49 -07:00
|
|
|
|
|
|
|
with {:ok, response} <-
|
2020-02-11 00:12:57 -07:00
|
|
|
Pleroma.HTTP.request(
|
|
|
|
method,
|
|
|
|
url,
|
|
|
|
body,
|
|
|
|
headers,
|
2020-09-10 11:09:44 -06:00
|
|
|
opts
|
2020-02-11 00:12:57 -07:00
|
|
|
) do
|
|
|
|
if is_map(response.body) and method != :head do
|
|
|
|
{:ok, response.status, response.headers, response.body}
|
|
|
|
else
|
2020-07-26 10:18:21 -06:00
|
|
|
conn_pid = response.opts[:adapter][:conn]
|
|
|
|
ConnectionPool.release_conn(conn_pid)
|
2020-02-11 00:12:57 -07:00
|
|
|
{:ok, response.status, response.headers}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, error} -> {:error, error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
2020-03-13 00:37:57 -06:00
|
|
|
@spec stream_body(map()) ::
|
|
|
|
{:ok, binary(), map()} | {:error, atom() | String.t()} | :done | no_return()
|
2020-07-26 11:44:26 -06:00
|
|
|
def stream_body(%{pid: pid, fin: true}) do
|
|
|
|
ConnectionPool.release_conn(pid)
|
2020-02-11 00:12:57 -07:00
|
|
|
:done
|
|
|
|
end
|
|
|
|
|
|
|
|
def stream_body(client) do
|
|
|
|
case read_chunk!(client) do
|
|
|
|
{:fin, body} ->
|
|
|
|
{:ok, body, Map.put(client, :fin, true)}
|
|
|
|
|
|
|
|
{:nofin, part} ->
|
|
|
|
{:ok, part, client}
|
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
{:error, error}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp read_chunk!(%{pid: pid, stream: stream, opts: opts}) do
|
|
|
|
adapter = check_adapter()
|
|
|
|
adapter.read_chunk(pid, stream, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
@spec close(map) :: :ok | no_return()
|
|
|
|
def close(%{pid: pid}) do
|
2020-07-26 10:18:21 -06:00
|
|
|
ConnectionPool.release_conn(pid)
|
2020-02-11 00:12:57 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
defp check_adapter do
|
|
|
|
adapter = Application.get_env(:tesla, :adapter)
|
|
|
|
|
|
|
|
unless adapter == Tesla.Adapter.Gun do
|
|
|
|
raise "#{adapter} doesn't support reading body in chunks"
|
|
|
|
end
|
|
|
|
|
|
|
|
adapter
|
|
|
|
end
|
|
|
|
end
|