The response.headers and response.status GUCs a function, a trigger or a
pre_request function sets, as PostgREST's Response.hs handles them.
Every read, write and call statement selects both GUCs after its main CTE, in the same
statement and transaction (response_headers, response_status, NULL when unset or
empty). decode/2 validates the raw values; status/2 picks the status to send (the GUC status
replaces the generated one) and apply/2 overlays the headers on the response: a generated
header is dropped when a GUC header has the same name (case-insensitively), and every GUC
header is then added in order, so repeated names (two Set-Cookies) are all kept.
A statement that fails is never decoded: its GUCs are rolled back with it. A malformed GUC
is a 500 (PGRST111, PGRST112) and rolls the transaction back like any other error.
Differences from PostgREST
Only response.status values in 200..999 are accepted. PostgREST may send an unfinished informational response or a malformed status line outside this range. Header names and values are validated to prevent response splitting.
Summary
Functions
Overlays the decoded GUC headers on a response conn that is not sent yet: generated
headers a GUC header names are dropped (names compare case-insensitively), and every GUC
header is appended in order, so repeated names are all kept. The status is the caller's to
send, through status/2.
The SQL select list of the two columns, response_headers then response_status: appended
to the select of every read, write and call statement.
Decodes the raw GUC values. nil is no override. The status is checked first, as
PostgREST does (overrideStatusHeaders), so two malformed GUCs answer PGRST112.
The decoded value for "no override": no header and no status.
The status to send: the GUC status when one is set, else default.
Types
@type decoded() :: %{headers: [{String.t(), String.t()}], status: pos_integer() | nil}
Functions
@spec apply(Plug.Conn.t(), decoded()) :: Plug.Conn.t()
Overlays the decoded GUC headers on a response conn that is not sent yet: generated
headers a GUC header names are dropped (names compare case-insensitively), and every GUC
header is appended in order, so repeated names are all kept. The status is the caller's to
send, through status/2.
@spec columns() :: String.t()
The SQL select list of the two columns, response_headers then response_status: appended
to the select of every read, write and call statement.
@spec decode(String.t() | nil, String.t() | nil) :: {:ok, decoded()} | {:error, PgRest.Error.t()}
Decodes the raw GUC values. nil is no override. The status is checked first, as
PostgREST does (overrideStatusHeaders), so two malformed GUCs answer PGRST112.
Headers are also rejected (PGRST111) when a name is empty or not an HTTP token, or a
name or value holds a CR, LF, NUL or other control character: apply/2 writes them to the
response without Plug's validation, so a value built from request data must not be able to
split the response. PostgREST has no such check (whether Warp rejects such headers itself is unverified).
@spec none() :: decoded()
The decoded value for "no override": no header and no status.
@spec status(decoded(), pos_integer()) :: pos_integer()
The status to send: the GUC status when one is set, else default.