Current section
Files
Jump to
Current section
Files
lib/phoenix/plug/save_raw_body.ex
if Code.ensure_loaded?(Plug) do
defmodule ExWeb3.Phoenix.Plug.SaveRawBody do
@moduledoc """
Phoenix decodes original body message in Endpoint,
so to get proper digest, we need to preserve original message,
before Plug.Parsers are called.
Put this plug just before Plug.Parsers in endpoint.ex of your
Phoenix app.
"""
import Plug.Conn
@behaviour Plug
@impl Plug
@spec init(opts :: Keyword.t()) :: Keyword.t()
def init(opts \\ []) do
unless Keyword.has_key?(opts, :match_req_path),
do: throw "Regular expression :match_req_path is required"
end
@impl Plug
@spec call(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()
def call(conn, opts) do
mask = Keyword.fetch!(opts, :match_req_path)
case String.match?(request_path, mask) do
false ->
conn
true ->
{:ok, body, _} = Plug.Conn.read_body(conn)
Plug.Conn.put_private(conn, :raw_body, body)
end
end
end
end