Config¶
Use Config to configure the server and RequestPolicy for path-specific limits. Constructor options are keyword-only. The Configuration reference lists their types, defaults, CLI flags, environment variables, TOML keys, and precedence.
Config¶
Immutable server configuration.
Configuration can be loaded with Config.from_env, Config.from_mapping, or Config.from_toml.
Every option is also exposed as a CLI flag and an H2CORN_* environment variable. The full list and defaults are documented in the configuration reference of the project documentation.
Resolution order (highest precedence first):
- CLI flags
- Environment variables
- TOML file (via
--configorH2CORN_CONFIG) - Built-in defaults
Invalid combinations (for example, certfile without keyfile, or a Unix listener with TLS) raise ValueError at construction time.
Unix listeners (unix:PATH) and the user/group/umask options are POSIX-only.
Examples:
Configure an option without starting a server:
>>> config = Config(bind=('127.0.0.1:0',), access_log=False)
>>> (config.bind, config.access_log)
(('127.0.0.1:0',), False)
Invalid combinations fail during construction:
>>> Config(certfile='server.pem')
Traceback (most recent call last):
...
ValueError: certfile and keyfile must be configured together
from_envclassmethod¶
from_env
Build a Config from H2CORN_* environment variables.
Each option's environment variable name is its field name in upper case, prefixed with H2CORN_ (for example, H2CORN_BIND, H2CORN_WORKERS, H2CORN_PROXY_HEADERS). Variables that are not set fall through to defaults. Unknown H2CORN_* names raise ValueError; variables outside that prefix are ignored.
The convenience pair H2CORN_HOST / H2CORN_PORT is accepted only when H2CORN_BIND is unset.
Examples:
Pass an explicit mapping so the process environment is not involved:
>>> config = Config.from_env(
... {'H2CORN_BIND': '127.0.0.1:9000', 'H2CORN_WORKERS': '2'}
... )
>>> (config.bind, config.workers)
(('127.0.0.1:9000',), 2)
from_mappingclassmethod¶
from_mapping
Build a Config from a plain mapping (e.g. parsed JSON or TOML).
Keys must match either a field name on Config or the convenience keys host/port. Unknown keys raise ValueError. String values are coerced through the same parsing rules as the matching environment variable; other values are checked against the option's accepted types.
Examples:
String values use the same parsing rules as environment values:
>>> config = Config.from_mapping(
... {'workers': '2', 'access_log': 'false'}
... )
>>> (config.workers, config.access_log)
(2, False)
from_tomlclassmethod¶
from_toml
Build a Config from a TOML file.
The file uses top-level Config field names. Request policies use an array of tables. For example, save this as h2corn.toml:
bind = ["127.0.0.1:8000"]
workers = 4
proxy_headers = true
forwarded_allow_ips = ["127.0.0.1", "::1", "unix"]
forwarded_fields = ["for", "proto"]
[[request_policies]]
path = "/example.Chat/*"
max_request_body_size = 0
timeout_request_body_idle = 0
h2_timeout_response_stall = 60.0
Load it with:
from h2corn import Config
config = Config.from_toml('h2corn.toml')
RequestPolicydataclass¶
RequestPolicy
Complete body and HTTP/2 response-stall policy for matching request paths.
Attributes:
| Name | Type | Description |
|---|---|---|
path | str | An exact ASCII request path or slash-delimited subtree ending in |
max_request_body_size | int | Maximum request-body bytes as an exact integer from |
timeout_request_body_idle | float | Maximum seconds without request-body data. Use |
h2_timeout_response_stall | float | Maximum seconds response bytes may remain blocked by HTTP/2 peer flow control. Use |
A matching rule takes precedence over the native gRPC streaming defaults and replaces all three global values; it does not inherit individual values from Config.
Native gRPC admission is controlled by Config.grpc; when disabled, native gRPC requests receive HTTP 415 even if a path rule matches. When enabled, unmatched native gRPC requests have no request-body size limit or idle timeout, while the HTTP/2 response-stall timeout still follows Config.
Native gRPC is recognized from case-insensitive application/grpc and application/grpc+<valid non-empty subtype> media types; parameters are ignored. Connect and gRPC-Web media types remain ordinary HTTP.
Construction accepts exact integers or floats for timeout seconds and normalizes them to floats. Booleans, non-finite numbers, and negative values are rejected. Config validation also rejects durations the runtime's monotonic clock cannot represent.
Raises:
| Type | Description |
|---|---|
TypeError | A field has the wrong Python type. |
ValueError | The path or a numeric field is outside its accepted range. |
pathinstance-attribute¶
path: str
max_request_body_sizeinstance-attribute¶
max_request_body_size: int
timeout_request_body_idleinstance-attribute¶
timeout_request_body_idle: float
h2_timeout_response_stallinstance-attribute¶
h2_timeout_response_stall: float
__reduce__¶
__reduce__
__post_init__¶
__post_init__
ProxyProtocolModemodule-attribute¶
ProxyProtocolMode = Literal['off', 'v1', 'v2']
LifespanModemodule-attribute¶
LifespanMode = Literal['auto', 'on', 'off']
CertReqsModemodule-attribute¶
CertReqsMode = Literal['none', 'optional', 'required']
LoopImplmodule-attribute¶
LoopImpl = Literal['auto', 'asyncio', 'uvloop']
ServerHeaderModemodule-attribute¶
ServerHeaderMode = Literal['off', 'on', 'full']
LogFormatmodule-attribute¶
LogFormat = Literal['text', 'json']