In-house redirect handler for mediaproxy with Hackney adapter
Also ensure we always pass an absolute URL to Hackney when parsing a redirect response (cherry picked from commit 00ac6bce8d244eec7e2460358296619e5cacba6b)
This commit is contained in:
parent
f2ad6100f2
commit
71a4b8d0f2
2 changed files with 44 additions and 1 deletions
1
changelog.d/hackney-mediaproxy.change
Normal file
1
changelog.d/hackney-mediaproxy.change
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Use a custom redirect handler to ensure MediaProxy redirects are followed with Hackney
|
||||||
|
|
@ -5,6 +5,20 @@
|
||||||
defmodule Pleroma.ReverseProxy.Client.Hackney do
|
defmodule Pleroma.ReverseProxy.Client.Hackney do
|
||||||
@behaviour Pleroma.ReverseProxy.Client
|
@behaviour Pleroma.ReverseProxy.Client
|
||||||
|
|
||||||
|
# redirect handler from Pleb, slightly modified to work with Hackney
|
||||||
|
# https://declin.eu/objects/d4f38e62-5429-4614-86d1-e8fc16e6bf33
|
||||||
|
# https://github.com/benoitc/hackney/issues/273
|
||||||
|
@redirect_statuses [301, 302, 303, 307, 308]
|
||||||
|
defp absolute_redirect_url(original_url, resp_headers) do
|
||||||
|
location =
|
||||||
|
Enum.find(resp_headers, fn {header, _location} ->
|
||||||
|
String.downcase(header) == "location"
|
||||||
|
end)
|
||||||
|
|
||||||
|
URI.merge(original_url, elem(location, 1))
|
||||||
|
|> URI.to_string()
|
||||||
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def request(method, url, headers, body, opts \\ []) do
|
def request(method, url, headers, body, opts \\ []) do
|
||||||
opts =
|
opts =
|
||||||
|
|
@ -12,7 +26,35 @@ defmodule Pleroma.ReverseProxy.Client.Hackney do
|
||||||
path
|
path
|
||||||
end)
|
end)
|
||||||
|
|
||||||
:hackney.request(method, url, headers, body, opts)
|
if opts[:follow_redirect] != false do
|
||||||
|
{_state, req_opts} = Access.get_and_update(opts, :follow_redirect, fn a -> {a, false} end)
|
||||||
|
res = :hackney.request(method, url, headers, body, req_opts)
|
||||||
|
|
||||||
|
case res do
|
||||||
|
{:ok, code, resp_headers, _client} when code in @redirect_statuses ->
|
||||||
|
:hackney.request(
|
||||||
|
method,
|
||||||
|
absolute_redirect_url(url, resp_headers),
|
||||||
|
headers,
|
||||||
|
body,
|
||||||
|
req_opts
|
||||||
|
)
|
||||||
|
|
||||||
|
{:ok, code, resp_headers} when code in @redirect_statuses ->
|
||||||
|
:hackney.request(
|
||||||
|
method,
|
||||||
|
absolute_redirect_url(url, resp_headers),
|
||||||
|
headers,
|
||||||
|
body,
|
||||||
|
req_opts
|
||||||
|
)
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
res
|
||||||
|
end
|
||||||
|
else
|
||||||
|
:hackney.request(method, url, headers, body, opts)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue