Pleroma/lib/pleroma/web/plugs/idempotency_plug.ex

87 lines
2.3 KiB
Elixir
Raw Normal View History

2019-06-26 13:36:42 +02:00
# Pleroma: A lightweight social networking server
2020-03-03 23:44:49 +01:00
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
2019-06-26 13:36:42 +02:00
# SPDX-License-Identifier: AGPL-3.0-only
2020-06-24 09:09:39 +02:00
defmodule Pleroma.Web.Plugs.IdempotencyPlug do
2019-06-26 13:36:42 +02:00
import Phoenix.Controller, only: [json: 2]
import Plug.Conn
@behaviour Plug
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
2019-06-26 13:36:42 +02:00
@impl true
def init(opts), do: opts
2019-06-26 14:19:07 +02:00
# Sending idempotency keys in `GET` and `DELETE` requests has no effect
# and should be avoided, as these requests are idempotent by definition.
2019-06-26 13:36:42 +02:00
@impl true
def call(%{method: method} = conn, _) when method in ["POST", "PUT", "PATCH"] do
case get_req_header(conn, "idempotency-key") do
[key] -> process_request(conn, key)
_ -> conn
end
end
def call(conn, _), do: conn
def process_request(conn, key) do
case @cachex.get(:idempotency_cache, key) do
2019-06-26 13:36:42 +02:00
{:ok, nil} ->
cache_resposnse(conn, key)
{:ok, record} ->
send_cached(conn, key, record)
2019-06-26 20:53:36 +02:00
{atom, message} when atom in [:ignore, :error] ->
render_error(conn, message)
2019-06-26 13:36:42 +02:00
end
end
defp cache_resposnse(conn, key) do
2019-06-26 20:53:36 +02:00
register_before_send(conn, fn conn ->
2019-06-26 13:36:42 +02:00
[request_id] = get_resp_header(conn, "x-request-id")
content_type = get_content_type(conn)
record = {request_id, content_type, conn.status, conn.resp_body}
{:ok, _} = @cachex.put(:idempotency_cache, key, record)
2019-06-26 13:36:42 +02:00
conn
|> put_resp_header("idempotency-key", key)
|> put_resp_header("x-original-request-id", request_id)
end)
end
defp send_cached(conn, key, record) do
{request_id, content_type, status, body} = record
conn
|> put_resp_header("idempotency-key", key)
|> put_resp_header("idempotent-replayed", "true")
|> put_resp_header("x-original-request-id", request_id)
|> put_resp_content_type(content_type)
|> send_resp(status, body)
|> halt()
end
defp render_error(conn, message) do
conn
|> put_status(:unprocessable_entity)
|> json(%{error: message})
|> halt()
end
defp get_content_type(conn) do
[content_type] = get_resp_header(conn, "content-type")
if String.contains?(content_type, ";") do
content_type
|> String.split(";")
|> hd()
else
content_type
end
end
end