|
| 1 | +defmodule AlgoraWeb.Plugs.CanonicalHostPlug do |
| 2 | + @moduledoc """ |
| 3 | + A Plug for ensuring that all requests are served by a single canonical host |
| 4 | + Adapted from https://github.com/remi/plug_canonical_host |
| 5 | + """ |
| 6 | + @behaviour Plug |
| 7 | + |
| 8 | + # Imports |
| 9 | + import Plug.Conn |
| 10 | + |
| 11 | + # Aliases |
| 12 | + alias Plug.Conn |
| 13 | + |
| 14 | + # Behaviours |
| 15 | + |
| 16 | + # Constants |
| 17 | + @location_header "location" |
| 18 | + @forwarded_port_header "x-forwarded-port" |
| 19 | + @forwarded_proto_header "x-forwarded-proto" |
| 20 | + @status_code 301 |
| 21 | + @html_template """ |
| 22 | + <!DOCTYPE html> |
| 23 | + <html lang="en-US"> |
| 24 | + <head><title>301 Moved Permanently</title></head> |
| 25 | + <body> |
| 26 | + <h1>Moved Permanently</h1> |
| 27 | + <p>The document has moved <a href="%s">here</a>.</p> |
| 28 | + </body> |
| 29 | + </html> |
| 30 | + """ |
| 31 | + |
| 32 | + # Types |
| 33 | + @type opts :: binary | tuple | atom | integer | float | [opts] | %{opts => opts} |
| 34 | + |
| 35 | + @doc """ |
| 36 | + Initialize this plug with a canonical host option. |
| 37 | + """ |
| 38 | + @spec init(opts) :: opts |
| 39 | + def init(opts) do |
| 40 | + [ |
| 41 | + canonical_host: Keyword.fetch!(opts, :canonical_host), |
| 42 | + path: Keyword.fetch!(opts, :path) |
| 43 | + ] |
| 44 | + end |
| 45 | + |
| 46 | + @doc """ |
| 47 | + Call the plug. |
| 48 | + """ |
| 49 | + @spec call(%Conn{}, opts) :: Conn.t() |
| 50 | + def call(%Conn{host: host} = conn, canonical_host: canonical_host, path: path) |
| 51 | + when is_nil(canonical_host) == false and canonical_host !== "" and host !== canonical_host do |
| 52 | + location = redirect_location(conn, canonical_host, path) |
| 53 | + |
| 54 | + conn |
| 55 | + |> put_resp_header(@location_header, location) |
| 56 | + |> send_resp(@status_code, String.replace(@html_template, "%s", location)) |
| 57 | + |> halt() |
| 58 | + end |
| 59 | + |
| 60 | + def call(conn, _), do: conn |
| 61 | + |
| 62 | + @spec redirect_location(%Conn{}, String.t(), String.t()) :: String.t() |
| 63 | + defp redirect_location(conn, canonical_host, path) do |
| 64 | + conn |
| 65 | + |> request_uri(path) |
| 66 | + |> URI.parse() |
| 67 | + |> sanitize_empty_query() |
| 68 | + |> Map.put(:host, canonical_host) |
| 69 | + |> Map.put(:path, path) |
| 70 | + |> URI.to_string() |
| 71 | + end |
| 72 | + |
| 73 | + @spec request_uri(%Conn{}, String.t()) :: String.t() |
| 74 | + defp request_uri(%Conn{host: host, query_string: query_string} = conn, path) do |
| 75 | + "#{canonical_scheme(conn)}://#{host}:#{canonical_port(conn)}#{path}?#{query_string}" |
| 76 | + end |
| 77 | + |
| 78 | + @spec canonical_port(%Conn{}) :: binary | integer |
| 79 | + defp canonical_port(%Conn{port: port} = conn) do |
| 80 | + case {get_req_header(conn, @forwarded_port_header), get_req_header(conn, @forwarded_proto_header)} do |
| 81 | + {[forwarded_port], _} -> forwarded_port |
| 82 | + {[], ["http"]} -> 80 |
| 83 | + {[], ["https"]} -> 443 |
| 84 | + {[], []} -> port |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + @spec canonical_scheme(%Conn{}) :: binary |
| 89 | + defp canonical_scheme(%Conn{scheme: scheme} = conn) do |
| 90 | + case get_req_header(conn, @forwarded_proto_header) do |
| 91 | + [forwarded_proto] -> forwarded_proto |
| 92 | + [] -> scheme |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + @spec sanitize_empty_query(%URI{}) :: %URI{} |
| 97 | + defp sanitize_empty_query(%URI{query: ""} = uri), do: Map.put(uri, :query, nil) |
| 98 | + defp sanitize_empty_query(uri), do: uri |
| 99 | +end |
0 commit comments