Source: https://h2corn.monicz.dev/api/config/

# Config

Use [`Config`](https://h2corn.monicz.dev/api/config/#h2corn.Config "Config") to configure the server and [`RequestPolicy`](https://h2corn.monicz.dev/api/config/#h2corn.RequestPolicy "RequestPolicy dataclass") for path-specific limits. Constructor options are keyword-only. The [Configuration reference](https://h2corn.monicz.dev/configuration/) lists their types, defaults, CLI flags, environment variables, TOML keys, and precedence.

## Config

Immutable server configuration.

Configuration can be loaded with [`Config.from_env`](https://h2corn.monicz.dev/api/config/#h2corn.Config.from_env "from_env classmethod"), [`Config.from_mapping`](https://h2corn.monicz.dev/api/config/#h2corn.Config.from_mapping "from_mapping classmethod"), or [`Config.from_toml`](https://h2corn.monicz.dev/api/config/#h2corn.Config.from_toml "from_toml classmethod").

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):

1. CLI flags
2. Environment variables
3. TOML file (via `--config` or `H2CORN_CONFIG`)
4. 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:

```pycon
>>> 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:

```pycon
>>> Config(certfile='server.pem')
Traceback (most recent call last):
...
ValueError: certfile and keyfile must be configured together
```

### from\_env`classmethod`

```python
from_env
```

Build a [`Config`](https://h2corn.monicz.dev/api/config/#h2corn.Config "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:

```pycon
>>> 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\_mapping`classmethod`

```python
from_mapping
```

Build a [`Config`](https://h2corn.monicz.dev/api/config/#h2corn.Config "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:

```pycon
>>> config = Config.from_mapping(
...     {'workers': '2', 'access_log': 'false'}
... )
>>> (config.workers, config.access_log)
(2, False)
```

### from\_toml`classmethod`

```python
from_toml
```

Build a [`Config`](https://h2corn.monicz.dev/api/config/#h2corn.Config "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`:

```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:

```python
from h2corn import Config

config = Config.from_toml('h2corn.toml')
```

## RequestPolicy`dataclass`

```python
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 `/*`. Queries are excluded from matching. Paths are compared case-sensitively and percent escapes are not decoded. `/service/*` matches `/service/` and its descendants, not `/service` itself. |
| `max_request_body_size` | `int` | Maximum request-body bytes as an exact integer from `0` through `18_446_744_073_709_551_615`. Use `0` to disable the cumulative size limit. Native gRPC requests are rejected with HTTP 415 when `Config.grpc` is disabled and exempt when enabled, unless a matching request policy sets a limit. Connect and gRPC-Web requests use this limit normally. |
| `timeout_request_body_idle` | `float` | Maximum seconds without request-body data. Use `0` to disable the idle timer. Native gRPC requests are rejected with HTTP 415 when `Config.grpc` is disabled and exempt when enabled, unless a matching request policy sets a timeout. Connect and gRPC-Web requests use this timer normally. |
| `h2_timeout_response_stall` | `float` | Maximum seconds response bytes may remain blocked by HTTP/2 peer flow control. Use `0` to disable the timer. |

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. |

### path`instance-attribute`

```python
path: str
```

### max\_request\_body\_size`instance-attribute`

```python
max_request_body_size: int
```

### timeout\_request\_body\_idle`instance-attribute`

```python
timeout_request_body_idle: float
```

### h2\_timeout\_response\_stall`instance-attribute`

```python
h2_timeout_response_stall: float
```

### \_\_reduce\_\_

```python
__reduce__
```

### \_\_post\_init\_\_

```python
__post_init__
```

## ProxyProtocolMode`module-attribute`

```python
ProxyProtocolMode = Literal['off', 'v1', 'v2']
```

## LifespanMode`module-attribute`

```python
LifespanMode = Literal['auto', 'on', 'off']
```

## CertReqsMode`module-attribute`

```python
CertReqsMode = Literal['none', 'optional', 'required']
```

## LoopImpl`module-attribute`

```python
LoopImpl = Literal['auto', 'asyncio', 'uvloop']
```

## ServerHeaderMode`module-attribute`

```python
ServerHeaderMode = Literal['off', 'on', 'full']
```

## LogFormat`module-attribute`

```python
LogFormat = Literal['text', 'json']
```
