Favicon Plug: Halt Plug pipeline when favicon not found
This commit is contained in:
parent
d0db1f00c3
commit
5f321b0b5b
2 changed files with 21 additions and 21 deletions
|
|
@ -11,7 +11,9 @@ defmodule Pleroma.Web.Plugs.FaviconPlug do
|
||||||
Serves default or custom favicon.png with cacheable cache-control.
|
Serves default or custom favicon.png with cacheable cache-control.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Plug.Conn, only: [put_resp_header: 3]
|
import Plug.Conn, only: [put_resp_header: 3, send_resp: 3, halt: 1]
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
def init(opts) do
|
def init(opts) do
|
||||||
opts
|
opts
|
||||||
|
|
@ -25,7 +27,12 @@ defmodule Pleroma.Web.Plugs.FaviconPlug do
|
||||||
call_static(conn, opts, dir)
|
call_static(conn, opts, dir)
|
||||||
|
|
||||||
:error ->
|
:error ->
|
||||||
conn # Let the request keep going to a 404
|
# Favicon should always be available and this should never occur.
|
||||||
|
# If it does, halt the pipeline before having unintended side-effects.
|
||||||
|
Logger.error("No favicon.png found! Is the default favicon deleted?")
|
||||||
|
conn
|
||||||
|
|> send_resp(404, "Not found")
|
||||||
|
|> halt()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -54,7 +61,6 @@ defmodule Pleroma.Web.Plugs.FaviconPlug do
|
||||||
|> Map.put(:content_types, false)
|
|> Map.put(:content_types, false)
|
||||||
|
|
||||||
conn = set_content_type(conn)
|
conn = set_content_type(conn)
|
||||||
|
|
||||||
Plug.Static.call(conn, opts)
|
Plug.Static.call(conn, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,20 +5,21 @@
|
||||||
defmodule Pleroma.Web.Plugs.FaviconPlugTest do
|
defmodule Pleroma.Web.Plugs.FaviconPlugTest do
|
||||||
use Pleroma.Web.ConnCase
|
use Pleroma.Web.ConnCase
|
||||||
|
|
||||||
# import Mox
|
|
||||||
|
|
||||||
# alias Pleroma.UnstubbedConfigMock, as: ConfigMock
|
|
||||||
|
|
||||||
@dir "test/tmp/favicon_static"
|
@dir "test/tmp/favicon_static"
|
||||||
|
|
||||||
|
setup do
|
||||||
|
Pleroma.Backports.mkdir_p!(@dir)
|
||||||
|
|
||||||
|
on_exit(fn -> File.rm_rf!(@dir) end)
|
||||||
|
end
|
||||||
|
|
||||||
describe "default favicon" do
|
describe "default favicon" do
|
||||||
test "returns favicon", %{conn: conn} do
|
test "returns favicon", %{conn: conn} do
|
||||||
conn = get(conn, "/favicon.png")
|
conn = get(conn, "/favicon.png")
|
||||||
etag = get_resp_header(conn, "etag")
|
body_size = byte_size(conn.resp_body)
|
||||||
|
|
||||||
# etag changes when serving a different file
|
|
||||||
assert conn.status == 200
|
assert conn.status == 200
|
||||||
assert etag == ["\"72487CE\""]
|
assert body_size == 1583
|
||||||
assert response_content_type(conn, :png)
|
assert response_content_type(conn, :png)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -33,28 +34,21 @@ defmodule Pleroma.Web.Plugs.FaviconPlugTest do
|
||||||
|
|
||||||
describe "custom favicon" do
|
describe "custom favicon" do
|
||||||
setup do
|
setup do
|
||||||
Pleroma.Backports.mkdir_p!(@dir)
|
|
||||||
favicon_path = Path.join(@dir, "favicon.png")
|
favicon_path = Path.join(@dir, "favicon.png")
|
||||||
|
donor_image = "test/fixtures/image.png"
|
||||||
|
|
||||||
# Favicon plug should always return image/png
|
|
||||||
donor_image = "test/fixtures/image.gif"
|
|
||||||
image_stat = File.stat!(donor_image)
|
|
||||||
|
|
||||||
# Preserve stat since that's what etag is based on
|
|
||||||
File.cp!(donor_image, favicon_path)
|
File.cp!(donor_image, favicon_path)
|
||||||
File.write_stat!(favicon_path, image_stat)
|
|
||||||
|
|
||||||
clear_config([:instance, :static_dir], @dir)
|
clear_config([:instance, :static_dir], @dir)
|
||||||
|
|
||||||
on_exit( fn -> File.rm_rf!(@dir) end)
|
on_exit(fn -> File.rm!(favicon_path) end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "returns favicon", %{conn: conn} do
|
test "returns favicon", %{conn: conn} do
|
||||||
conn = get(conn, "/favicon.png")
|
conn = get(conn, "/favicon.png")
|
||||||
etag = get_resp_header(conn, "etag")
|
body_size = byte_size(conn.resp_body)
|
||||||
|
|
||||||
assert conn.status == 200
|
assert conn.status == 200
|
||||||
assert etag == ["\"215A3A4\""]
|
assert body_size == 104426
|
||||||
assert response_content_type(conn, :png)
|
assert response_content_type(conn, :png)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue