HelPHP

Bridge
in package
uses Logs

Turns a WebSocket frame into a REST call: one message is one `new Restresponse($module, $action, $data, true)`, answered with the same envelope as `api/index.php`.

Three things this class owns, because nobody else can:

  1. The identity of the caller. A long-lived process serves several clients in a row, so the globals Restresponse writes ($_POST, $USER, $H_errors…) are snapshotted before a call and restored after it. The user agent and the client address are rebuilt from the handshake, and the identifier is pinned to the address the service itself resolved: a client cannot choose the address factor of its OID by sending hicut.
  2. What a client may call. Only the modules listed in the whitelist are reachable (Config_websocket::MODULES, 'connection' by default), and the keys that make HelPHP_module bypass the normal dispatch (action, quick_edit) are refused. Those branches echo and exit(); in a service that must keep serving, a refusal is the only safe answer.
  3. The life of the token. check_oid() purges tokens older than Config::TOKEN_MINUTE, and renews the one it just used. A connection idle for longer would die on its next message, so renew() keeps the token alive on the heartbeat and treats a vanished token as a revocation.

The answer always has the REST shape: {id, status, OID, data} with message instead of data from 400 up. id is the client's correlation number, echoed as is.

Tags
see

Router, Server, api/index.php in the instance

Table of Contents

Constants

RESERVED_KEYS  : mixed = array('action', 'quick_edit')
Keys that reach a branch of HelPHP_module::process_data() which echoes and exits: over HTTP they end the request, over a service they would end the service.

Methods

__construct()  : mixed
add_log_sink()  : void
Adds a destination. A sink that throws is reported and skipped: a logger must never be the reason a connection dies.
envelope()  : array<string|int, mixed>
Builds the answer envelope from what Restresponse left in `$data`.
handle()  : void
Handles one frame and answers on the connection.
has_log_sinks()  : bool
modules()  : array<int, string>
renew()  : bool
Keeps the OID of a connection alive on the heartbeat.

Constants

RESERVED_KEYS

Keys that reach a branch of HelPHP_module::process_data() which echoes and exits: over HTTP they end the request, over a service they would end the service.

public mixed RESERVED_KEYS = array('action', 'quick_edit')

Methods

__construct()

public __construct([array<string|int, mixed> $settings = array() ]) : mixed
Parameters
$settings : array<string|int, mixed> = array()

['modules'=>array<int,string>, 'log'=>callable]

add_log_sink()

Adds a destination. A sink that throws is reported and skipped: a logger must never be the reason a connection dies.

public add_log_sink(mixed $callback) : void
Parameters
$callback : mixed

envelope()

Builds the answer envelope from what Restresponse left in `$data`.

public static envelope(mixed $id, mixed $raw[, string|false $oid = false ]) : array<string|int, mixed>

Restresponse has two shapes: the REST commands write a complete envelope ({status, OID, data}, JSON encoded), while a module call leaves its raw output. Both end up as the same frame here.

Parameters
$id : mixed

Client correlation number, echoed as is.

$raw : mixed

Restresponse::$data.

$oid : string|false = false

OID known to Restresponse after the call.

Return values
array<string|int, mixed>

handle()

Handles one frame and answers on the connection.

public handle(Connection $connection, string $payload) : void
Parameters
$connection : Connection
$payload : string

Frame payload, a JSON object.

has_log_sinks()

public has_log_sinks() : bool
Return values
bool —

True when the lines go to an application sink rather than the instance log.

modules()

public modules() : array<int, string>
Return values
array<int, string> —

Modules currently reachable.

renew()

Keeps the OID of a connection alive on the heartbeat.

public renew(Connection $connection) : bool

The rule mirrors Restresponse::check_oid(), which stays the reference: the token, the hash of the user agent and the client address must still match, and the creation time is pushed forward so the purge never takes a live connection. A token that disappeared means the user logged out or was revoked: the caller closes the connection.

Parameters
$connection : Connection
Return values
bool —

False when the token is gone.

On this page

Search results