total rebase
[anni] / patches / 2(fix_searches_p2).patch
1 From ed31bad2c481633520cc593912586b403c1ac6d4 Mon Sep 17 00:00:00 2001
2 From: Mint <>
3 Date: Wed, 31 May 2023 19:50:20 +0300
4 Subject: [PATCH] In-house redirect handler for mediaproxy with Hackney adapter
5
6 ---
7  lib/pleroma/reverse_proxy/client/hackney.ex | 32 ++++++++++++++++++++-
8  1 file changed, 31 insertions(+), 1 deletion(-)
9
10 diff --git a/lib/pleroma/reverse_proxy/client/hackney.ex b/lib/pleroma/reverse_proxy/client/hackney.ex
11 index d3e9869121..ca4a0c29f0 100644
12 --- a/lib/pleroma/reverse_proxy/client/hackney.ex
13 +++ b/lib/pleroma/reverse_proxy/client/hackney.ex
14 @@ -5,9 +5,39 @@
15  defmodule Pleroma.ReverseProxy.Client.Hackney do
16    @behaviour Pleroma.ReverseProxy.Client
17  
18 +  # redirect handler from Pleb, slightly modified to work with Hackney
19 +  # https://declin.eu/objects/d4f38e62-5429-4614-86d1-e8fc16e6bf33
20 +  # seven years without upstream fix!
21 +  # https://github.com/benoitc/hackney/issues/273
22 +  @redirect_statuses [301, 302, 303, 307, 308]
23 +  defp get_location(headers) do
24 +    location =
25 +      Enum.find(headers, fn {header, _location} ->
26 +        String.downcase(header) == "location"
27 +      end)
28 +
29 +    elem(location, 1)
30 +  end
31 +
32    @impl true
33    def request(method, url, headers, body, opts \\ []) do
34 -    :hackney.request(method, url, headers, body, opts)
35 +    if opts[:follow_redirect] != false do
36 +      {_state, req_opts} = Access.get_and_update(opts, :follow_redirect, fn a -> {a, false} end)
37 +      res = :hackney.request(method, url, headers, body, req_opts)
38 +
39 +      case res do
40 +        {:ok, code, reply_headers, _client} when code in @redirect_statuses ->
41 +          :hackney.request(method, get_location(reply_headers), headers, body, req_opts)
42 +
43 +        {:ok, code, reply_headers} when code in @redirect_statuses ->
44 +          :hackney.request(method, get_location(reply_headers), headers, body, req_opts)
45 +
46 +        _ ->
47 +          res
48 +      end
49 +    else
50 +      :hackney.request(method, url, headers, body, opts)
51 +    end
52    end
53  
54    @impl true
55 -- 
56 GitLab
57