CGI¶
CGI script execution support for running external programs.
Configuration¶
CGIConfig¶
Configuration options for CGI script execution.
CGIConfig
dataclass
¶
CGIConfig(
timeout: float = 30.0,
max_header_size: int = 8192,
streaming: bool = False,
check_execute_permission: bool = True,
inherit_environment: bool = False,
app_state_keys: list[str] = list(),
)
Configuration for CGI script execution.
Attributes:
| Name | Type | Description |
|---|---|---|
timeout |
float
|
Maximum execution time in seconds. |
max_header_size |
int
|
Maximum size of the status line in bytes. |
streaming |
bool
|
Enable streaming mode for large responses. |
check_execute_permission |
bool
|
Whether to verify execute permission. |
inherit_environment |
bool
|
Whether to inherit parent environment variables. |
Handlers¶
CGIHandler¶
Execute CGI scripts from a directory.
CGIHandler
¶
Execute CGI scripts from a directory.
This handler executes scripts located in a specified directory, with proper environment variable setup and security validation.
Example
from xitzin.cgi import CGIHandler, CGIConfig
config = CGIConfig(timeout=30) handler = CGIHandler("/srv/gemini/cgi-bin", config=config) app.mount("/cgi-bin", handler)
Requests to /cgi-bin/hello.py execute /srv/gemini/cgi-bin/hello.py¶
Create a CGI directory handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_dir
|
Path | str
|
Directory containing CGI scripts. |
required |
config
|
CGIConfig | None
|
CGI execution configuration. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If script_dir doesn't exist or isn't a directory. |
Source code in src/xitzin/cgi.py
__call__
async
¶
Handle a request by executing the appropriate CGI script.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The Gemini request. |
required |
path_info
|
str
|
Path after the mount prefix (e.g., "/script.py"). |
required |
Returns:
| Type | Description |
|---|---|
GeminiResponse
|
GeminiResponse from the CGI script. |
Raises:
| Type | Description |
|---|---|
NotFound
|
If the script doesn't exist. |
CGIError
|
If execution fails. |
BadRequest
|
If path validation fails. |
Source code in src/xitzin/cgi.py
CGIScript¶
Execute a single CGI script.
CGIScript
¶
CGIScript(
script_path: Path | str,
*,
timeout: float = 30.0,
check_execute_permission: bool = True,
inherit_environment: bool = False,
app_state_keys: list[str] | None = None,
)
Execute a single CGI script.
This handler executes a specific CGI script for all requests, useful for mounting a single script at a specific path.
Example
from xitzin.cgi import CGIScript
handler = CGIScript("/srv/scripts/calculator.py", timeout=10) app.mount("/calculator", handler)
All requests to /calculator execute /srv/scripts/calculator.py¶
Create a single-script CGI handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script_path
|
Path | str
|
Path to the CGI script. |
required |
timeout
|
float
|
Maximum execution time in seconds. |
30.0
|
check_execute_permission
|
bool
|
Whether to verify execute permission. |
True
|
inherit_environment
|
bool
|
Whether to inherit parent environment. |
False
|
app_state_keys
|
list[str] | None
|
App state keys to pass as XITZIN_* variables. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If script doesn't exist. |
Source code in src/xitzin/cgi.py
__call__
async
¶
Execute the CGI script for this request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The Gemini request. |
required |
path_info
|
str
|
Additional path info (usually empty for single scripts). |
''
|
Returns:
| Type | Description |
|---|---|
GeminiResponse
|
GeminiResponse from the CGI script. |
Raises:
| Type | Description |
|---|---|
CGIError
|
If execution fails. |
Source code in src/xitzin/cgi.py
Helper Functions¶
build_cgi_env¶
Build CGI environment variables from a request.
build_cgi_env
¶
build_cgi_env(
request: Request,
script_name: str,
path_info: str,
*,
app_state_vars: dict[str, str] | None = None,
inherit_environment: bool = True,
) -> dict[str, str]
Build CGI environment variables from a request.
This follows the Gemini CGI conventions established by Jetforce and other Gemini servers, based on RFC 3875.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The Gemini request. |
required |
script_name
|
str
|
Name/path of the CGI script. |
required |
path_info
|
str
|
Additional path info after the script name. |
required |
app_state_vars
|
dict[str, str] | None
|
Application state variables to pass as XITZIN_*. |
None
|
inherit_environment
|
bool
|
Whether to inherit current environment. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of environment variables for the CGI script. |
Source code in src/xitzin/cgi.py
parse_cgi_output¶
Parse CGI script output into a response.
parse_cgi_output
¶
Parse CGI script output into a structured response.
The expected format is
Or for backwards compatibility
\r\n [body] # Assumes status 20
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stdout
|
bytes
|
The script's standard output. |
required |
stderr
|
bytes | None
|
The script's standard error (for error messages). |
None
|
Returns:
| Type | Description |
|---|---|
CGIResponse
|
Parsed CGI response. |
Raises:
| Type | Description |
|---|---|
CGIError
|
If the output format is invalid. |