SCGI¶
SCGI (Simple Common Gateway Interface) support for proxying requests to persistent backend processes.
Configuration¶
SCGIConfig¶
Configuration options for SCGI backend communication.
SCGIConfig
dataclass
¶
SCGIConfig(
timeout: float = 30.0,
max_response_size: int | None = 1048576,
buffer_size: int = 8192,
inherit_environment: bool = False,
app_state_keys: list[str] = list(),
)
Configuration for SCGI backend communication.
Attributes:
| Name | Type | Description |
|---|---|---|
timeout |
float
|
Maximum time to wait for SCGI response in seconds. |
max_response_size |
int | None
|
Maximum response size in bytes (None = unlimited). |
buffer_size |
int
|
Read buffer size for streaming responses. |
inherit_environment |
bool
|
Whether to inherit parent environment variables. |
app_state_keys |
list[str]
|
App state keys to pass as XITZIN_* env vars. |
Handlers¶
SCGIHandler¶
Proxy requests to an SCGI backend via TCP socket.
SCGIHandler
¶
Proxy requests to an SCGI backend server via TCP socket.
This handler forwards requests to an SCGI application server (like Python's flup, or custom SCGI servers) over a TCP connection.
Example
from xitzin.scgi import SCGIHandler, SCGIConfig
config = SCGIConfig(timeout=30) handler = SCGIHandler("127.0.0.1", 4000, config=config) app.mount("/dynamic", handler)
Requests to /dynamic/* are forwarded to 127.0.0.1:4000¶
Create an SCGI TCP handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
host
|
str
|
SCGI server hostname or IP. |
required |
port
|
int
|
SCGI server port. |
required |
config
|
SCGIConfig | None
|
SCGI communication configuration. |
None
|
Source code in src/xitzin/scgi.py
__call__
async
¶
Forward request to SCGI backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The Gemini request. |
required |
path_info
|
str
|
Path after the mount prefix. |
required |
Returns:
| Type | Description |
|---|---|
GeminiResponse
|
GeminiResponse from the SCGI backend. |
Raises:
| Type | Description |
|---|---|
ProxyError
|
If connection or communication fails. |
Source code in src/xitzin/scgi.py
SCGIApp¶
Proxy requests to an SCGI backend via Unix socket.
SCGIApp
¶
Proxy requests to an SCGI backend server via Unix socket.
This handler forwards requests to an SCGI application server over a Unix domain socket (more efficient for local communication).
Example
from xitzin.scgi import SCGIApp, SCGIConfig
config = SCGIConfig(timeout=30) handler = SCGIApp("/tmp/scgi.sock", config=config) app.mount("/dynamic", handler)
Requests to /dynamic/* are forwarded to /tmp/scgi.sock¶
Create an SCGI Unix socket handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
socket_path
|
Path | str
|
Path to the Unix socket. |
required |
config
|
SCGIConfig | None
|
SCGI communication configuration. |
None
|
Source code in src/xitzin/scgi.py
__call__
async
¶
Forward request to SCGI backend via Unix socket.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The Gemini request. |
required |
path_info
|
str
|
Path after the mount prefix. |
required |
Returns:
| Type | Description |
|---|---|
GeminiResponse
|
GeminiResponse from the SCGI backend. |
Raises:
| Type | Description |
|---|---|
ProxyError
|
If connection or communication fails. |
Source code in src/xitzin/scgi.py
Helper Functions¶
encode_netstring¶
Encode data as a netstring for SCGI protocol.
encode_netstring
¶
Encode data as a netstring.
Netstring format:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
Bytes to encode. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Netstring-encoded bytes. |
Example
encode_netstring(b"hello") b'5:hello,'
Source code in src/xitzin/scgi.py
encode_scgi_headers¶
Encode CGI environment as SCGI headers.
encode_scgi_headers
¶
Encode CGI environment as SCGI headers.
SCGI format is a netstring containing null-separated key-value pairs:
The CONTENT_LENGTH header must come first per SCGI spec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
dict[str, str]
|
CGI environment dictionary. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
Netstring-encoded headers ready for SCGI transmission. |
Example
env = {"CONTENT_LENGTH": "0", "SCGI": "1", "PATH_INFO": "/test"} encode_scgi_headers(env) # Returns netstring with headers