HttpEtag.Conn (HttpEtag v0.1.0)

Copy Markdown View Source

Plug.Conn helpers. Requires the optional :plug dependency.

These are not a pipeline plug. They read and write headers only; they do not send 304, 412, or 400. Load the resource, mint a tag with HttpEtag.new!/1 (for example user.lock_version), then call if_match/2 or if_none_match/2. Repeated If-Match / If-None-Match field lines are combined with ", " (RFC 9110 §5.3).

if_match/2 and if_none_match/2 return the same RFC tuples as HttpEtag. This is not Plug.Static: that plug compares If-None-Match as raw field-line membership.

Examples

etag = HttpEtag.new!(user.lock_version)

case HttpEtag.Conn.if_none_match(conn, etag) do
  :ok ->
    conn |> HttpEtag.Conn.put_etag(etag) |> json(user)

  {:error, %{reason: :precondition_failed}} ->
    conn
    |> HttpEtag.Conn.put_etag(etag)
    |> Plug.Conn.send_resp(304, "")

  {:error, %{reason: :invalid_header}} ->
    Plug.Conn.send_resp(conn, 400, "")
end

See HttpEtag.if_match/2 and HttpEtag.if_none_match/2.

Summary

Functions

Returns the combined If-Match request field, or nil if it is absent.

Returns the combined If-None-Match request field, or nil if it is absent.

Evaluates If-Match on conn against current.

Evaluates If-None-Match on conn against current.

Sets the ETag response header from a parsed tag.

Functions

get_if_match(conn)

@spec get_if_match(Plug.Conn.t()) :: String.t() | nil

Returns the combined If-Match request field, or nil if it is absent.

Repeated field lines are joined with ", " (RFC 9110 §5.3).

get_if_none_match(conn)

@spec get_if_none_match(Plug.Conn.t()) :: String.t() | nil

Returns the combined If-None-Match request field, or nil if it is absent.

Repeated field lines are joined with ", " (RFC 9110 §5.3).

if_match(conn, current)

@spec if_match(Plug.Conn.t(), HttpEtag.t() | nil) ::
  :ok | {:error, HttpEtag.Error.t()}

Evaluates If-Match on conn against current.

See HttpEtag.if_match/2.

if_none_match(conn, current)

@spec if_none_match(Plug.Conn.t(), HttpEtag.t() | nil) ::
  :ok | {:error, HttpEtag.Error.t()}

Evaluates If-None-Match on conn against current.

See HttpEtag.if_none_match/2.

put_etag(conn, tag)

@spec put_etag(Plug.Conn.t(), HttpEtag.t()) :: Plug.Conn.t()

Sets the ETag response header from a parsed tag.