Execution Context

Salata is context-aware. Each of the four binaries sets an ExecutionContext before invoking salata-core, and this context flows through the entire processing pipeline. The context is defined as a Rust enum:

#![allow(unused)]
fn main() {
enum ExecutionContext {
    Cli,
    Cgi,
    FastCgi,
    Server,
}
}

Which Binary Sets Which Context

BinaryContext
salata (CLI)Cli
salata-cgiCgi
salata-fastcgiFastCgi
salata-serverServer

The context is set once at startup and does not change during the lifetime of the process.

Why Context Matters

The primary effect of the execution context is PHP binary selection. PHP has multiple Server API (SAPI) interfaces, and using the wrong one produces incorrect behavior. Salata mirrors PHP's own model:

ContextPHP Binary UsedConfig Field
Cliphp (CLI SAPI)cli_path
Cgiphp-cgi (CGI SAPI)cgi_path
FastCgiphp-fpm via socket or TCPfastcgi_socket / fastcgi_host
Serverphp-fpm via socket or TCPfastcgi_socket / fastcgi_host

Why Different PHP Binaries

PHP's SAPIs differ in how they handle input, output, and HTTP semantics:

  • php (CLI) -- reads from stdin, writes to stdout, has no awareness of HTTP headers. This is the correct choice when Salata is invoked from the command line and the output is written to a file or piped to another program.

  • php-cgi -- implements the CGI protocol. It reads request metadata from environment variables (REQUEST_METHOD, QUERY_STRING, etc.) and can emit HTTP headers in its output. This is necessary when Salata runs behind a web server as a CGI program.

  • php-fpm -- a persistent FastCGI process manager. It maintains a pool of PHP worker processes and communicates over Unix sockets or TCP. This is the efficient choice for persistent server contexts where spawning a new process per request would be wasteful.

Context in the Pipeline

The execution context is passed as a parameter through the processing pipeline. When salata-core encounters a <php> block, it checks the current context to determine which binary to invoke:

salata-cli sets context = Cli
  → salata-core receives context
    → parser extracts <php> block
      → runtime module checks context
        → context is Cli → use cli_path ("/usr/bin/php")
        → context is Cgi → use cgi_path ("/usr/bin/php-cgi")
        → context is FastCgi → connect to fastcgi_socket or fastcgi_host
        → context is Server → connect to fastcgi_socket or fastcgi_host

Effect on Other Runtimes

For runtimes other than PHP, the execution context currently has no effect on behavior. Python, Ruby, JavaScript, TypeScript, and Shell all use the same binary regardless of context. The context is still available to these runtimes in case future features need it.

Request Data

In CGI, FastCGI, and Server contexts, HTTP request data is made available to runtimes through standard CGI environment variables:

VariableDescription
REQUEST_METHODHTTP method (GET, POST, etc.)
QUERY_STRINGURL query parameters
CONTENT_TYPERequest body MIME type
CONTENT_LENGTHRequest body size in bytes
HTTP_HOSTHost header value
HTTP_COOKIECookie header value
REMOTE_ADDRClient IP address
REQUEST_URIFull request URI
PATH_INFOExtra path information
SERVER_NAMEServer hostname
SERVER_PORTServer port number
HTTP_AUTHORIZATIONAuthorization header value

In the CLI context, these variables are not set (there is no HTTP request).