Merge branch 'fix/cache-control-headers' into 'develop'
Fix Cache Control headers on media See merge request pleroma/pleroma!2295
This commit is contained in:
commit
d88c8a9614
|
@ -90,8 +90,6 @@ server {
|
||||||
proxy_ignore_client_abort on;
|
proxy_ignore_client_abort on;
|
||||||
proxy_buffering on;
|
proxy_buffering on;
|
||||||
chunked_transfer_encoding on;
|
chunked_transfer_encoding on;
|
||||||
proxy_ignore_headers Cache-Control;
|
|
||||||
proxy_hide_header Cache-Control;
|
|
||||||
proxy_pass http://127.0.0.1:4000;
|
proxy_pass http://127.0.0.1:4000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,14 @@ defmodule Pleroma.Plugs.UploadedMedia do
|
||||||
# no slashes
|
# no slashes
|
||||||
@path "media"
|
@path "media"
|
||||||
|
|
||||||
|
@default_cache_control_header "public, max-age=1209600"
|
||||||
|
|
||||||
def init(_opts) do
|
def init(_opts) do
|
||||||
static_plug_opts =
|
static_plug_opts =
|
||||||
[]
|
[
|
||||||
|
headers: %{"cache-control" => @default_cache_control_header},
|
||||||
|
cache_control_for_etags: @default_cache_control_header
|
||||||
|
]
|
||||||
|> Keyword.put(:from, "__unconfigured_media_plug")
|
|> Keyword.put(:from, "__unconfigured_media_plug")
|
||||||
|> Keyword.put(:at, "/__unconfigured_media_plug")
|
|> Keyword.put(:at, "/__unconfigured_media_plug")
|
||||||
|> Plug.Static.init()
|
|> Plug.Static.init()
|
||||||
|
|
|
@ -7,7 +7,7 @@ defmodule Pleroma.ReverseProxy do
|
||||||
|
|
||||||
@keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since) ++
|
@keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since) ++
|
||||||
~w(if-unmodified-since if-none-match if-range range)
|
~w(if-unmodified-since if-none-match if-range range)
|
||||||
@resp_cache_headers ~w(etag date last-modified cache-control)
|
@resp_cache_headers ~w(etag date last-modified)
|
||||||
@keep_resp_headers @resp_cache_headers ++
|
@keep_resp_headers @resp_cache_headers ++
|
||||||
~w(content-type content-disposition content-encoding content-range) ++
|
~w(content-type content-disposition content-encoding content-range) ++
|
||||||
~w(accept-ranges vary)
|
~w(accept-ranges vary)
|
||||||
|
@ -34,9 +34,6 @@ defmodule Pleroma.ReverseProxy do
|
||||||
* request: `#{inspect(@keep_req_headers)}`
|
* request: `#{inspect(@keep_req_headers)}`
|
||||||
* response: `#{inspect(@keep_resp_headers)}`
|
* response: `#{inspect(@keep_resp_headers)}`
|
||||||
|
|
||||||
If no caching headers (`#{inspect(@resp_cache_headers)}`) are returned by upstream, `cache-control` will be
|
|
||||||
set to `#{inspect(@default_cache_control_header)}`.
|
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
||||||
* `redirect_on_failure` (default `false`). Redirects the client to the real remote URL if there's any HTTP
|
* `redirect_on_failure` (default `false`). Redirects the client to the real remote URL if there's any HTTP
|
||||||
|
@ -297,16 +294,17 @@ defp build_resp_headers(headers, opts) do
|
||||||
|
|
||||||
defp build_resp_cache_headers(headers, _opts) do
|
defp build_resp_cache_headers(headers, _opts) do
|
||||||
has_cache? = Enum.any?(headers, fn {k, _} -> k in @resp_cache_headers end)
|
has_cache? = Enum.any?(headers, fn {k, _} -> k in @resp_cache_headers end)
|
||||||
has_cache_control? = List.keymember?(headers, "cache-control", 0)
|
|
||||||
|
|
||||||
cond do
|
cond do
|
||||||
has_cache? && has_cache_control? ->
|
|
||||||
headers
|
|
||||||
|
|
||||||
has_cache? ->
|
has_cache? ->
|
||||||
# There's caching header present but no cache-control -- we need to explicitely override it
|
# There's caching header present but no cache-control -- we need to set our own
|
||||||
# to public as Plug defaults to "max-age=0, private, must-revalidate"
|
# as Plug defaults to "max-age=0, private, must-revalidate"
|
||||||
List.keystore(headers, "cache-control", 0, {"cache-control", "public"})
|
List.keystore(
|
||||||
|
headers,
|
||||||
|
"cache-control",
|
||||||
|
0,
|
||||||
|
{"cache-control", @default_cache_control_header}
|
||||||
|
)
|
||||||
|
|
||||||
true ->
|
true ->
|
||||||
List.keystore(
|
List.keystore(
|
||||||
|
|
|
@ -275,17 +275,6 @@ test "returns 400 on non GET, HEAD requests", %{conn: conn} do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "cache resp headers" do
|
describe "cache resp headers" do
|
||||||
test "returns headers", %{conn: conn} do
|
|
||||||
ClientMock
|
|
||||||
|> expect(:request, fn :get, "/cache/" <> ttl, _, _, _ ->
|
|
||||||
{:ok, 200, [{"cache-control", "public, max-age=" <> ttl}], %{}}
|
|
||||||
end)
|
|
||||||
|> expect(:stream_body, fn _ -> :done end)
|
|
||||||
|
|
||||||
conn = ReverseProxy.call(conn, "/cache/10")
|
|
||||||
assert {"cache-control", "public, max-age=10"} in conn.resp_headers
|
|
||||||
end
|
|
||||||
|
|
||||||
test "add cache-control", %{conn: conn} do
|
test "add cache-control", %{conn: conn} do
|
||||||
ClientMock
|
ClientMock
|
||||||
|> expect(:request, fn :get, "/cache", _, _, _ ->
|
|> expect(:request, fn :get, "/cache", _, _, _ ->
|
||||||
|
@ -294,7 +283,7 @@ test "add cache-control", %{conn: conn} do
|
||||||
|> expect(:stream_body, fn _ -> :done end)
|
|> expect(:stream_body, fn _ -> :done end)
|
||||||
|
|
||||||
conn = ReverseProxy.call(conn, "/cache")
|
conn = ReverseProxy.call(conn, "/cache")
|
||||||
assert {"cache-control", "public"} in conn.resp_headers
|
assert {"cache-control", "public, max-age=1209600"} in conn.resp_headers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue