Metadata-Version: 2.4
Name: mini-aiohttp
Version: 1.4.1
Summary: A lightweight asyncio HTTP toolkit with HTTP/1.1, optional HTTP/2 and HTTP/3, WebSockets, and proxy support
License-Expression: MIT
Project-URL: Homepage, https://github.com/meenaserver123280-dotcom/mini-aiohttp
Project-URL: Repository, https://github.com/meenaserver123280-dotcom/mini-aiohttp
Project-URL: Changelog, https://github.com/meenaserver123280-dotcom/mini-aiohttp/blob/main/CHANGELOG.md
Keywords: asyncio,http,client,server,websocket,http2,http3,proxy,asgi
Classifier: Framework :: AsyncIO
Classifier: Typing :: Typed
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: http2
Requires-Dist: h2>=4.1.0; extra == "http2"
Provides-Extra: http3
Requires-Dist: aioquic>=1.2.0; extra == "http3"
Provides-Extra: brotli
Requires-Dist: brotli>=1.0.9; extra == "brotli"
Provides-Extra: orjson
Requires-Dist: orjson>=3.9; extra == "orjson"
Provides-Extra: certifi
Requires-Dist: certifi>=2023.7.22; extra == "certifi"
Provides-Extra: speed
Requires-Dist: orjson>=3.9; extra == "speed"
Requires-Dist: uvloop>=0.17; sys_platform != "win32" and extra == "speed"
Provides-Extra: prometheus
Requires-Dist: prometheus-client>=0.17; extra == "prometheus"
Provides-Extra: jwt
Requires-Dist: pyjwt>=2.8; extra == "jwt"
Provides-Extra: pydantic
Requires-Dist: pydantic>=2.0; extra == "pydantic"
Provides-Extra: otel
Requires-Dist: opentelemetry-api>=1.20; extra == "otel"
Provides-Extra: asyncpg
Requires-Dist: asyncpg>=0.29; extra == "asyncpg"
Provides-Extra: redis
Requires-Dist: redis>=5.0; extra == "redis"
Provides-Extra: sqlalchemy
Requires-Dist: sqlalchemy[asyncio]>=2.0; extra == "sqlalchemy"
Provides-Extra: mongo
Requires-Dist: motor>=3.3; extra == "mongo"
Provides-Extra: database
Requires-Dist: asyncpg>=0.29; extra == "database"
Requires-Dist: redis>=5.0; extra == "database"
Requires-Dist: sqlalchemy[asyncio]>=2.0; extra == "database"
Requires-Dist: motor>=3.3; extra == "database"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: pytest-asyncio<1.0,>=0.21; extra == "test"
Requires-Dist: pytest-timeout>=2.2; extra == "test"
Requires-Dist: pytest-cov>=4; extra == "test"
Requires-Dist: hypothesis>=6; extra == "test"
Provides-Extra: dev
Requires-Dist: mini-aiohttp[test]; extra == "dev"
Requires-Dist: ruff>=0.5; extra == "dev"
Requires-Dist: mypy>=1.10; extra == "dev"
Requires-Dist: pre-commit>=3.7; extra == "dev"
Provides-Extra: all
Requires-Dist: h2>=4.1.0; extra == "all"
Requires-Dist: aioquic>=1.2.0; extra == "all"
Requires-Dist: brotli>=1.0.9; extra == "all"
Requires-Dist: orjson>=3.9; extra == "all"
Requires-Dist: certifi>=2023.7.22; extra == "all"
Requires-Dist: uvloop>=0.17; sys_platform != "win32" and extra == "all"
Requires-Dist: prometheus-client>=0.17; extra == "all"
Requires-Dist: pyjwt>=2.8; extra == "all"
Requires-Dist: pydantic>=2.0; extra == "all"
Requires-Dist: opentelemetry-api>=1.20; extra == "all"
Dynamic: license-file

# ⚡ mini-aiohttp

**mini-aiohttp v1.4.1** is a lightweight, asynchronous HTTP/1.1 client and server framework built with Python and asyncio, with optional HTTP/2, HTTP/3, WebSocket and ASGI support.

Designed for simplicity, performance, flexibility and security. The core has no required dependencies and keeps pure-Python fallbacks. Prebuilt wheels include an optional PyO3 extension that accelerates HTTP request-head parsing and buffered response serialization.

[![Python](https://img.shields.io/badge/Python-3.11%2B-blue)](https://www.python.org/)
[![Version](https://img.shields.io/badge/Version-1.4.1-green)](https://pypi.org/project/mini-aiohttp/)
[![License](https://img.shields.io/badge/License-MIT-yellow)](https://opensource.org/license/mit)

---

## 📑 Contents

- [Features](#-features)
- [Installation](#-installation)
- [Quick Start](#-quick-start)
- [Guides](#-guides)
  - [Route-level middleware and rate limits](#route-level-middleware-and-rate-limits)
  - [Background tasks, worker queue and graceful shutdown](#background-tasks-worker-queue-and-graceful-shutdown)
  - [Telegram webhook](#telegram-webhook)
  - [Database adapters](#database-adapters)
  - [Authentication and CSRF](#authentication-and-csrf)
  - [Running behind Cloudflare](#running-behind-cloudflare)
- [API Reference](#-api-reference)
- [Streaming Responses](#-streaming-responses)
- [Server Statistics](#-server-statistics)
- [Testing](#-testing)
- [Compatibility](#-compatibility)
- [Protocol Limitations](#-protocol-limitations)
- [License](#-license)

---

## ✨ Features

### HTTP Client
- Asynchronous HTTP/1.1 requests.
- Connection pooling and keep-alive.
- Per-host concurrency limits.
- Automatic redirect handling.
- Bounded retries for idempotent methods, and a `RetryPolicy` helper for 429 and 502/503/504.
- Cookie management, including Max-Age expiry.
- Automatic decoding of `gzip`, `deflate` and (with an extra) `br` responses.
- Streaming response downloads.
- Async streaming uploads.
- Multipart form and file uploads.
- HTTP and SOCKS5/SOCKS5h proxies, including HTTPS CONNECT/tunnelling, and a Tor/Orbot preset.
- DNS caching.
- Configurable connection and request timeouts.
- TLS context customization, and optional certifi CA bundle.
- Request and response hooks.
- Response size limits.
- JSON parsing on responses (`response.json()`).
- Standalone WebSocket client (`ws://` / `wss://`), with permessage-deflate compression (RFC 7692).
- OpenAI-compatible chat helper for Groq, Gemini, OpenRouter and Ollama (`mini_aiohttp.ai`).
- Automatic HTTPS client: ALPN selects HTTP/2 when available, otherwise HTTP/1.1; Alt-Svc enables HTTP/3 on later requests (`AutoClientSession`).
- Optional HTTP/2 client and server adapters (`pip install mini-aiohttp[http2]`).
- Optional HTTP/3 client and server adapters (`pip install mini-aiohttp[http3]`).
- Telegram Bot API polling (`TelegramPoller`) and webhooks (`TelegramWebhook`).

### HTTP Server
- Asynchronous HTTP server with optional Rust-accelerated request parsing and response serialization (pure-Python fallbacks supported).
- Routing with path parameters.
- Route-level middleware and per-route rate limiting (`route_middleware()`, `route_limit()`).
- Global middleware, including a traffic recorder for inspecting recent requests.
- WebSocket endpoints on the HTTP port (`Server.websocket`).
- Webhook receiver with secret token check, update deduplication and background dispatch (`WebhookRouter`).
- ASGI 3 adapter (`ASGIApp`) for uvicorn, hypercorn and other ASGI servers.
- Gzip response compression middleware (`gzip_middleware`).
- Background tasks, bounded worker queues and graceful shutdown (`BackgroundTasks`, `TaskQueue`, `serve()`).
- HTTP/1.1 persistent connections, chunked request bodies and chunked streaming responses.
- HTTPS support using `SSLContext`.
- Static file serving with HTTP `Range` (single and multi-range, 206/416), `ETag`, `Last-Modified` and conditional GET (`StaticFiles`).
- Signed-cookie sessions (`SessionMiddleware`).
- Form body parsing with size limits (`parse_urlencoded`).
- Minimal template engine with HTML escaping (`Template`, `TemplateLoader`).
- Path traversal protection.
- Connection limits and runtime server statistics.

### Security
- HTTP header validation and request framing validation.
- Content-Length and Transfer-Encoding conflict protection.
- CSRF protection with signed tokens (`csrf_protect`).
- HTTP Basic authentication (`basic_auth`) and API key authentication (`api_key_auth`).
- CORS middleware and security headers.
- Token-bucket rate limiting per client IP, and a Redis-backed limiter for multiple servers.
- Real client IP behind Cloudflare, read only from trusted peers (`cloudflare_client_ip`).
- IP/CIDR policies.
- Secure cookie helpers and tamper-evident signed session cookies.
- JWT bearer authentication middleware (extra).
- JSON request validation, plain or with pydantic models (extra).
- Bounded request and response parsing, fuzz-tested with hypothesis.

### Database adapters
- PostgreSQL (asyncpg), Redis, SQLAlchemy 2.x, MongoDB (motor) and Firebase Realtime Database (REST, no extra needed).
- Drivers are optional extras. The core still has no dependencies.

### Observability (extras)
- Prometheus metrics middleware and `/metrics` handler.
- OpenTelemetry server spans.

---

## 📦 Installation

Core (no required dependencies):

```bash
pip install mini-aiohttp
```

Requires Python 3.11 or newer.

Prebuilt wheels are published for Linux x86_64, macOS (x86_64 and arm64) and Windows x86_64, on Python 3.11, 3.12 and 3.13. These wheels include the native Rust extension (`mini_aiohttp._native`). On any other platform, pip builds from the source distribution. If no Rust toolchain is available, the build still succeeds and the package uses its pure-Python parser and response serializer. Nothing in the API changes between the two.

### Optional extras

| Extra | What it adds |
|---|---|
| `http2` | HTTP/2 client and server (`h2`) |
| `http3` | HTTP/3 client and server (`aioquic`) |
| `brotli` | `br` Content-Encoding support |
| `orjson` | Faster `Response.json` / `Request.json` |
| `certifi` | Mozilla CA bundle for HTTPS |
| `speed` | `orjson` + `uvloop` (use with `use_uvloop()`) |
| `prometheus` | Prometheus metrics middleware |
| `jwt` | JWT bearer auth middleware |
| `pydantic` | Pydantic-validated JSON bodies |
| `otel` | OpenTelemetry tracing middleware |
| `asyncpg` | PostgreSQL driver for `PostgresPool` |
| `redis` | Redis client for `RedisClient` |
| `sqlalchemy` | SQLAlchemy 2.x async engine for `SQLAlchemyEngine` |
| `mongo` | motor driver for `MongoDatabase` |
| `database` | asyncpg, redis, sqlalchemy and motor together |
| `all` | Every runtime extra above |

```bash
pip install "mini-aiohttp[http2,http3]"
pip install "mini-aiohttp[brotli,orjson,certifi]"
pip install "mini-aiohttp[prometheus,jwt,pydantic,otel]"
pip install "mini-aiohttp[asyncpg,redis]"
pip install "mini-aiohttp[database]"
pip install "mini-aiohttp[all]"
```

Install a specific version:

```bash
pip install mini-aiohttp==1.4.1
```

Upgrade an existing installation:

```bash
pip install --upgrade mini-aiohttp
```

Verify installation:

```bash
pip show mini-aiohttp
python -c "import mini_aiohttp; print(mini_aiohttp.__version__)"
```

---

## 🚀 Quick Start

### Create an HTTP Server

```python
import asyncio
from mini_aiohttp import Server, Response, gzip_middleware

app = Server()
app.middleware_(gzip_middleware(min_size=1024))

@app.route("/")
async def home(req):
    return Response.json({
        "message": "Welcome to mini-aiohttp",
        "version": "1.4.1"
    })

@app.route("/hello/{name}")
async def hello(req):
    return Response.json({
        "hello": req.params["name"]
    })

async def main():
    await app.start("127.0.0.1", 8080)

    try:
        await asyncio.Event().wait()
    finally:
        await app.close()

asyncio.run(main())
```

### WebSocket on the same port

```python
from mini_aiohttp import Server

app = Server()

@app.websocket("/ws/{room}")
async def chat(ws, req):
    await ws.send(f"joined {req.params['room']}")
    while True:
        msg = await ws.recv()
        if msg is None:
            break
        await ws.send(msg)
```

Clients connect to `ws://127.0.0.1:8080/ws/lobby`. The HTTP routes and the WebSocket route share one port.

### Run under uvicorn (ASGI)

```python
# myapp.py
from mini_aiohttp import ASGIApp, Response, Server

server = Server()

@server.route("/hello/{name}")
async def hello(req):
    return Response.json({"hello": req.params["name"]})

asgi_app = ASGIApp(server)
```

```bash
pip install uvicorn
uvicorn myapp:asgi_app --host 127.0.0.1 --port 8000
```

### HTTP Client

```python
import asyncio
from mini_aiohttp import ClientSession

async def main():
    async with ClientSession() as session:
        response = await session.get("https://example.com")
        print("Status:", response.status)
        print("Body:", response.text())

asyncio.run(main())
```

### Faster event loop (optional)

```python
import asyncio
from mini_aiohttp import use_uvloop

use_uvloop()  # needs the 'speed' extra; returns False if uvloop is missing
asyncio.run(main())
```

### Static files with Range and ETag

```python
from mini_aiohttp import Server, StaticFiles

app = Server()
app.middleware_(StaticFiles("/static", "public"))
# GET /static/video.mp4 with "Range: bytes=0-1023" returns 206 with Content-Range.
# GET /static/app.css with "If-None-Match: <etag>" returns 304 with no body.
```

### Sessions and forms

```python
import os
from mini_aiohttp import Response, Server, SessionMiddleware
from mini_aiohttp.forms import FormError, parse_urlencoded

app = Server()
app.middleware_(SessionMiddleware(secret=os.environ["SESSION_SECRET"]))  # 32+ bytes

@app.route("/login", method="POST")
async def login(req):
    try:
        fields = parse_urlencoded(req)
    except FormError as exc:
        return Response(exc.status, str(exc))
    req.session["user"] = fields.get("user", [""])[0]
    return Response(303, b"", {"Location": "/"})

@app.route("/me")
async def me(req):
    return Response.json({"user": req.session.get("user")})
```

### Templates

```python
from mini_aiohttp import Server, TemplateLoader, html_response

loader = TemplateLoader("templates")  # templates/home.html
app = Server()

@app.route("/")
async def home(req):
    return html_response(loader.render("home.html", title="Hi", user={"name": "Ada"}))
```

### Prometheus metrics (optional)

```python
from mini_aiohttp.extras.metrics import prometheus_middleware, metrics_handler

app.middleware_(prometheus_middleware())
app.route("/metrics")(metrics_handler())
```

### JWT auth (optional)

```python
from mini_aiohttp import Response
from mini_aiohttp.extras.jwt_auth import jwt_auth

app.middleware_(jwt_auth(secret="change-me", audience="my-api", exempt=("/health",)))

@app.route("/me")
async def me(req):
    return Response.json(req.user)   # decoded claims
```

### Pydantic-validated JSON (optional)

```python
from pydantic import BaseModel
from mini_aiohttp import Response
from mini_aiohttp.extras.pydantic_json import validated_json

class User(BaseModel):
    name: str
    age: int

@app.route("/users", method="POST")
@validated_json(User)
async def create(req):
    return Response.json({"ok": True, "name": req.model.name})
```

### Rate limiting across servers (optional)

```python
import redis.asyncio as redis
from mini_aiohttp.extras.redis_limiter import RedisRateLimiter

limiter = RedisRateLimiter(redis.Redis(), rate=20)  # 20 requests/second per client
app.middleware_(limiter.middleware())
```

---

## 📘 Guides

### Route-level middleware and rate limits

Global middleware runs for every request. Route-level middleware runs only for the handler it decorates. Global middleware still runs first.

```python
from mini_aiohttp import RateLimiter, Response, Server, route_limit, route_middleware

app = Server()

login_limiter = RateLimiter(rate=0.2, burst=5)   # 5 quick tries, then 1 every 5 seconds

@app.route("/login", method="POST")
@route_limit(login_limiter)
async def login(req):
    return Response.json({"ok": True})

async def require_admin(req, call_next):
    if req.headers.get("x-role") != "admin":
        return Response(403, "admins only")
    return await call_next(req)

async def audit_log(req, call_next):
    resp = await call_next(req)
    print("audit:", req.method, req.path, resp.status)
    return resp

@app.route("/admin")
@route_middleware(require_admin, audit_log)   # runs in the order listed
async def admin(req):
    return Response.json({"secret": 42})
```

- Each `route_limit` has its own bucket, so one route's traffic does not use up another route's limit.
- Clients are keyed by `req.client`. See [Running behind Cloudflare](#running-behind-cloudflare) for proxies.
- A handler may return a `Response`, a `str`, or `bytes`. Route middleware always receives a `Response` from `call_next`.

### Background tasks, worker queue and graceful shutdown

```python
import asyncio
from mini_aiohttp import BackgroundTasks, Response, Server, TaskQueue, serve

app = Server()
background = BackgroundTasks()

async def refresh_cache():
    while True:
        await asyncio.sleep(60)
        ...   # refresh something

background.spawn(refresh_cache(), name="cache")

async def send_email(job):
    ...   # slow work: send an email, call an API, and so on

queue = TaskQueue(send_email, workers=4, maxsize=1000, tasks=background)

@app.route("/signup", method="POST")
async def signup(req):
    await queue.put({"to": req.json()["email"]})   # returns at once
    return Response.json({"queued": True}, status=202)

async def main():
    # Blocks until SIGINT or SIGTERM, then runs the shutdown steps in order.
    await serve(
        app, "0.0.0.0", 8080,
        grace=10,                       # seconds allowed for in-flight work
        background=background,
        shutdown=[queue.close],         # extra hooks run after background work finishes
    )

asyncio.run(main())
```

**What `serve()` does on shutdown:**
1. Stops accepting new connections.
2. Lets in-flight requests finish, for up to `grace` seconds, then closes the rest.
3. Waits for tracked background tasks, for up to `grace` seconds, then cancels what is still running.
4. Runs the `shutdown` hooks in the order you list them. A hook that raises is logged, and the rest still run.

**Other details:**
- `BackgroundTasks.spawn(coro, name=None)` returns the `asyncio.Task`. A failed task is logged. After `close()`, `spawn` raises `RuntimeError`.
- `TaskQueue.put(job)` waits when the queue is full. `put_nowait(job)` raises `asyncio.QueueFull` instead. `qsize()` shows how many jobs are waiting. `join()` waits until every job is handled.
- A job that raises is logged, and the worker keeps going.
- Workers start on the first `put`, so you can create the queue before the event loop runs.
- Each `serve()` call runs its own shutdown steps. For tests, pass `stop_event=asyncio.Event()` and set it instead of sending a signal. Pass `signals=()` to skip signal handlers.
- On Windows, signal handlers are not installed. Use `stop_event` to stop the server.

### Telegram webhook

`TelegramWebhook` handles the whole webhook flow: it sets and removes the webhook with the Bot API, checks Telegram's secret token on every request, and runs your handler in the background.

```python
import os
from mini_aiohttp import ClientSession, Server, TelegramWebhook, serve, update_message

TOKEN = os.environ["BOT_TOKEN"]
SECRET = os.environ["TG_SECRET"]        # 1-256 characters: A-Z, a-z, 0-9, _ or -

app = Server()
session = ClientSession()

async def on_update(update: dict) -> None:
    message = update_message(update)     # message, edited_message, channel_post or edited_channel_post
    if message and message.get("text"):
        ...   # reply, store, and so on

bot = TelegramWebhook(session, TOKEN, on_update, secret_token=SECRET)
bot.attach(app, "/telegram")             # POST /telegram

async def main():
    await bot.set_webhook("https://example.com/telegram", allowed_updates=["message"])
    print(await bot.get_webhook_info())
    await serve(app, "0.0.0.0", 8080, shutdown=[bot.close, session.close])

asyncio.run(main())
```

- `set_webhook(url, *, allowed_updates=None, drop_pending=False, max_connections=None)`: the URL must start with `https://`. The secret token is sent to Telegram.
- `delete_webhook(*, drop_pending=False)` removes the webhook.
- `get_webhook_info()` returns Telegram's status dict (pending count, last error and so on).
- Requests with a wrong or missing secret get 403. Valid updates get 200 at once. Duplicate `update_id`s are skipped.
- `close()` waits for running handlers to finish.
- Telegram API errors raise `TelegramError`, with Telegram's message in `.description`. The bot token never appears in logs.
- Use either `TelegramWebhook` or `TelegramPoller`, not both. Telegram does not send `getUpdates` results while a webhook is set.

### Database adapters

Each adapter opens a client in `open()`, gives it to your handlers, and closes it in `close()`. Every adapter is also an async context manager. Pass `db.close` to `serve(..., shutdown=[...])` so the connections close on shutdown.

Drivers are imported only when `open()` runs. The core library stays dependency-free.

| Adapter | Extra | Use in handlers |
|---|---|---|
| `PostgresPool(dsn, **options)` | `asyncpg` | `db.pool.acquire()` |
| `RedisClient(url, **options)` | `redis` | `db.client` (`redis.asyncio`) |
| `SQLAlchemyEngine(url, **options)` | `sqlalchemy` | `db.session()` as an async context manager |
| `MongoDatabase(uri, database, **options)` | `mongo` | `db.db["collection"]` (motor) |
| `FirebaseRTDB(session, database_url, *, auth_token=None)` | none | `get`, `put`, `patch`, `push`, `delete` (REST) |

#### PostgreSQL with asyncpg

```bash
pip install "mini-aiohttp[asyncpg]"
```

```python
import asyncio
import os
from mini_aiohttp import Response, Server, serve
from mini_aiohttp.db import PostgresPool

db = PostgresPool(os.environ["DATABASE_URL"], min_size=1, max_size=10)
app = Server()

@app.route("/users/{user_id}")
async def get_user(req):
    async with db.pool.acquire() as conn:
        row = await conn.fetchrow(
            "SELECT id, name FROM users WHERE id = $1", int(req.params["user_id"])
        )
    if row is None:
        return Response(404, "not found")
    return Response.json({"id": row["id"], "name": row["name"]})

async def main():
    await db.open()
    await serve(app, "127.0.0.1", 8080, grace=10, shutdown=[db.close])

asyncio.run(main())
```

All keyword options (`min_size`, `max_size`, `command_timeout`, and so on) go straight to `asyncpg.create_pool`.

#### Redis as a cache

```bash
pip install "mini-aiohttp[redis]"
```

```python
import asyncio
import os
from mini_aiohttp import Response, Server, serve
from mini_aiohttp.db import RedisClient

cache = RedisClient(os.environ.get("REDIS_URL", "redis://localhost:6379/0"))
app = Server()

@app.route("/price/{symbol}")
async def price(req):
    key = f"price:{req.params['symbol'].upper()}"
    cached = await cache.client.get(key)
    if cached is not None:
        return Response(200, cached.decode("utf-8"))
    value = "42.00"                          # replace with a slow lookup
    await cache.client.set(key, value, ex=60)  # expire after 60 seconds
    return Response(200, value)

async def main():
    await cache.open()
    await serve(app, "127.0.0.1", 8080, shutdown=[cache.close])

asyncio.run(main())
```

#### SQLAlchemy 2.x

```bash
pip install "mini-aiohttp[sqlalchemy]" asyncpg
```

```python
import asyncio
import os
from sqlalchemy import text
from mini_aiohttp import Response, Server, serve
from mini_aiohttp.db import SQLAlchemyEngine

db = SQLAlchemyEngine(os.environ["DATABASE_URL"], pool_size=10)   # postgresql+asyncpg://...
app = Server()

@app.route("/users/{user_id}")
async def get_user(req):
    async with db.session() as session:
        result = await session.execute(
            text("SELECT name FROM users WHERE id = :id"),
            {"id": int(req.params["user_id"])},
        )
        row = result.first()
    if row is None:
        return Response(404, "not found")
    return Response.json({"name": row[0]})

async def main():
    await db.open()
    await serve(app, "127.0.0.1", 8080, shutdown=[db.close])

asyncio.run(main())
```

`session()` sets `expire_on_commit=False`, so objects stay readable after `commit()`. Call `db.session()` once per request, and let the `async with` block close it.

#### MongoDB (motor)

```bash
pip install "mini-aiohttp[mongo]"
```

```python
import asyncio
import os
from mini_aiohttp import Request, Response, Server, serve
from mini_aiohttp.db import MongoDatabase

mongo = MongoDatabase(os.environ.get("MONGO_URI", "mongodb://localhost:27017"), "app")
app = Server()

@app.route("/notes", method="POST")
async def add_note(req: Request):
    result = await mongo.db["notes"].insert_one({"text": req.json()["text"]})
    return Response.json({"id": str(result.inserted_id)}, status=201)

@app.route("/notes")
async def list_notes(req: Request):
    cursor = mongo.db["notes"].find({}, {"_id": 0}).limit(50)
    return Response.json([doc async for doc in cursor])

async def main():
    await mongo.open()
    await serve(app, "127.0.0.1", 8080, shutdown=[mongo.close])

asyncio.run(main())
```

#### Firebase Realtime Database (REST, no extra)

This adapter calls the Realtime Database REST API through a `ClientSession`. Nothing extra needs to be installed.

```python
import asyncio
import os
from mini_aiohttp import ClientSession, Request, Response, Server, serve
from mini_aiohttp.db import FirebaseError, FirebaseRTDB

app = Server()
session = ClientSession()
firebase = FirebaseRTDB(
    session,
    os.environ["FIREBASE_URL"],               # https://<project>-default-rtdb.firebaseio.com
    auth_token=os.environ.get("FIREBASE_TOKEN"),
)

@app.route("/users/{user_id}")
async def get_user(req: Request):
    try:
        user = await firebase.get(f"users/{req.params['user_id']}")
    except FirebaseError as exc:
        return Response(502, f"upstream error {exc.status}")
    if user is None:
        return Response(404, "not found")
    return Response.json(user)

@app.route("/users/{user_id}", method="PUT")
async def put_user(req: Request):
    await firebase.put(f"users/{req.params['user_id']}", req.json())
    return Response.json({"ok": True})

@app.route("/events", method="POST")
async def add_event(req: Request):
    created = await firebase.push("events", req.json())   # {"name": generated_key}
    return Response.json(created, status=201)

async def main():
    await serve(app, "127.0.0.1", 8080, shutdown=[session.close])

asyncio.run(main())
```

- `get`, `put` (replaces), `patch` (merges), `push` (appends with a generated key), and `delete` are available.
- `auth_token` is an ID token or a database secret. Errors raise `FirebaseError` with `.status` only, so the token does not appear in logs.
- `database_url` must start with `https://`.

#### Using several databases together

```python
async def main():
    await db.open()
    await cache.open()
    await serve(app, "0.0.0.0", 8080, grace=10,
                shutdown=[cache.close, db.close])   # runs in the order listed

asyncio.run(main())
```

Close consumers before the things they depend on. For example, close a cache that writes to the database before you close the database.

### Authentication and CSRF

These helpers are middleware or route middleware. Each one returns a function you pass to `app.middleware_(...)` or `@route_middleware(...)`.

#### CSRF protection

```python
import os
from mini_aiohttp import Response, Server, csrf_protect

app = Server()
app.middleware_(csrf_protect(secret=os.environ["CSRF_SECRET"]))   # at least 32 bytes

@app.route("/form")
async def form(req):
    return Response(200, (
        '<form method="post" action="/save">'
        f'<input type="hidden" name="csrf_token" value="{req.csrf_token}">'
        '<input name="title"><button>Save</button></form>'
    ))

@app.route("/save", method="POST")
async def save(req):
    return Response.json({"saved": True})   # reached only with a valid token
```

- Each visitor gets a signed random token in an HttpOnly, SameSite=Lax cookie. `req.csrf_token` gives you the value for templates.
- POST, PUT, PATCH and DELETE must send the same token in the `X-CSRF-Token` header, or in a form field named `csrf_token`. Otherwise they get 403.
- Form fields are read only for `application/x-www-form-urlencoded` bodies. For multipart forms, send the token in the header.
- Options: `cookie_name`, `header_name`, `form_field`, `max_age` (default one day), `secure` and `samesite`.

For JavaScript clients, read the token from the page and send it in the header:

```javascript
fetch("/save", {
  method: "POST",
  headers: { "Content-Type": "application/json", "X-CSRF-Token": token },
  body: JSON.stringify({ title: "x" }),
});
```

#### HTTP Basic authentication

```python
import os
from mini_aiohttp import Response, Server, basic_auth, route_middleware

app = Server()

@app.route("/admin")
@route_middleware(basic_auth({"admin": os.environ["ADMIN_PASSWORD"]}, realm="Admin"))
async def admin(req):
    return Response.json({"hello": req.user})   # set on success
```

- Failures get 401 with `WWW-Authenticate: Basic realm="..."`.
- Passwords are stored as SHA-256 digests and compared with `hmac.compare_digest`. A wrong username takes the same work as a wrong password.
- To check against a database instead of a dict, pass `verify=`:

```python
async def check_login(user, password):   # must return a bool
    ...

```

Note: the `verify` callable is called synchronously. For a database lookup, check the password in a cache or pass a sync function. Basic auth is only safe over HTTPS.

#### API key authentication

```python
import os
from mini_aiohttp import Response, Server, api_key_auth, route_middleware

app = Server()
KEYS = os.environ["API_KEYS"].split(",")

@app.route("/api/items")
@route_middleware(api_key_auth(KEYS))
async def items(req):
    return Response.json([{"id": 1}])
```

- Keys are read from the `X-API-Key` header only. Keys in URLs are not accepted, because URLs end up in logs.
- Pass `header="X-Token"` to change the header name, or `verify=fn(key) -> bool` to check against a store.
- Every configured key is checked in constant time, so timing does not show which key matched.
- Missing or wrong keys get 401.

### Running behind Cloudflare

Behind Cloudflare, `req.client` is the Cloudflare edge address, not the visitor's address. Without a fix, every visitor shares one rate-limit bucket.

```python
from mini_aiohttp import RateLimiter, Server, cloudflare_client_ip

app = Server()
app.middleware_(cloudflare_client_ip())                 # must come first
app.middleware_(RateLimiter(rate=5.0, burst=15).middleware())
```

- `CF-Connecting-IP` is read only when the TCP peer is inside Cloudflare's published ranges (`CLOUDFLARE_NETWORKS`, from https://www.cloudflare.com/ips/).
- A request that reaches your server directly keeps its own address, so a client cannot fake the header to pick an IP.
- A missing or malformed header leaves `req.client` unchanged.
- The original peer address is kept in `req.peer_address`.
- Options: `networks=` (a list of CIDR strings) and `header=` (default `cf-connecting-ip`).

**Lock the origin to Cloudflare.** Allow inbound traffic only from Cloudflare's ranges at your firewall. Then the trust check is also enforced by the network.

Cloudflare changes its ranges occasionally. Update `CLOUDFLARE_NETWORKS` in a new release, or pass `networks=` with the current list.

If you use another proxy (Nginx, a load balancer), the same idea applies. The library does not read `X-Forwarded-For`, because a client can set that header itself. Use your proxy's real-IP setting, or ask for trusted-proxy support.

---

## 📖 API Reference

Everything below is exported from the top-level package (`from mini_aiohttp import ...`) unless a module path is given. `mini_aiohttp.__version__` holds the package version string.

### Server

```python
Server(*, max_connections=100_000, read_timeout=15.0, max_body_size=10 * 1024 * 1024, backlog=4096)
```

| Method or property | Purpose |
|---|---|
| `route(path, method="GET")` | Decorator that registers a handler. Use `{name}` for path parameters, available as `req.params`. Literal routes take priority over parameterized ones. The first registration of the same method and path wins. |
| `websocket(path)` | Decorator that registers a WebSocket endpoint on the HTTP port. The handler is `async def handler(ws, req)`. See WebSocket endpoints below. |
| `middleware_(fn)` | Registers `async def fn(req, call_next)`. Middleware runs for every HTTP request, including 404, 405 and static files. It does not run for WebSocket connections. |
| `static(prefix, directory, *, max_file_size=50 * 1024 * 1024)` | Serves files under `prefix` with the built-in static handler (GET and HEAD only, no Range). Paths must stay inside `directory`, so `..` and symlinks cannot escape it. Files are streamed in 64 KiB chunks. Larger files get 413. For Range and ETag support, use `StaticFiles`. |
| `start(host="127.0.0.1", port=8080, *, reuse_port=False, ssl=None)` | Starts listening. Pass an `ssl.SSLContext` (server side) to serve HTTPS. Returns the asyncio server. |
| `serve_forever(host="127.0.0.1", port=8080, **kwargs)` | Starts and runs until cancelled. |
| `close(*, grace=5.0)` | Stops accepting connections, lets in-flight requests finish for up to `grace` seconds, then closes the rest. Does not raise on slow shutdown. |
| `port` | Bound port (useful with `port=0`). |
| `stats` | Snapshot of counters, see Server Statistics below. |

Handlers may return a `Response`, or a `str` or `bytes` value, which is sent as a 200 response.

HEAD requests are answered by the matching GET route (the body is dropped and `Content-Length` reports the GET body length). Static files also answer HEAD.

To run the server with signal handling and ordered shutdown, use `serve()` (see the Guides section).

**Server responses and limits:**

| Situation | Result |
|---|---|
| No route for the path | 404 |
| Path exists, method does not | 405 |
| Handler raises an exception | 500 (logged with traceback) |
| Request line or header line longer than 8 KiB | 431 |
| Request head larger than 256 KiB, or more than 100 headers | 431 |
| Body larger than `max_body_size` | 413 |
| Duplicate `Host`, `Content-Length` or `Transfer-Encoding` header | 400 |
| `Content-Length` and `Transfer-Encoding` together | 400 |
| Transfer-Encoding other than exactly `chunked`, or non-HTTP/1.1 chunked body | 501 |
| HTTP version other than 1.0 or 1.1 | 505 |
| More than `max_connections` open, or server draining | 503 |
| Idle or slow client longer than `read_timeout` | Connection closed |

HTTP/1.1 connections are kept alive unless the client sends `Connection: close`. HTTP/1.0 connections are kept alive only with `Connection: keep-alive`. `Expect: 100-continue` is answered.

### Request

Passed to every handler and middleware.

| Attribute or method | Description |
|---|---|
| `method`, `path`, `query`, `version` | Request line parts. `query` is the raw query string. |
| `headers` | Dict with lowercase header names. |
| `body` | Request body as bytes (decoded from chunked encoding if needed). |
| `params` | Path parameters from `{name}` segments. |
| `client` | Peer IP address as a string, or `None`. Changed by `cloudflare_client_ip()` when the peer is Cloudflare. |
| `peer_address` | The TCP peer address, set by `cloudflare_client_ip()`. |
| `text()` | Body decoded as UTF-8. |
| `json()` | Body parsed as JSON. Raises `ValueError` on invalid input. |
| `query_params()` | Query string as `{name: [values]}`, blanks kept. |
| `form()` | Parsed `multipart/form-data` body as `{name: [str or UploadedFile, ...]}`. Raises `ValueError` on bad bodies, so catch it and return 400. |
| `session` | Set by `SessionMiddleware`. A dict-like `Session`. |
| `csrf_token` | Set by `csrf_protect()`. The token to put in forms. |
| `user` | Set by `basic_auth()` (the username), and by the `jwt_auth` extra (the claims). |
| `model` | Set by the `validated_json` extra. |

The server does not parse cookies. Read them from `req.headers["cookie"]`, or use `SessionMiddleware`.

### Response

```python
Response(status=200, body=b"", headers=None)
```

- `body` may be `str` (encoded as UTF-8) or bytes. `headers` names are stored lowercase.
- When not set, `content-type` defaults to `text/plain; charset=utf-8`. `content-length` and `connection` are added automatically.
- `Response.json(data, status=200)`: JSON body with `application/json; charset=utf-8`.
- `Response.stream(chunks, status=200, headers=None)`: sends an async iterable of bytes or str as a chunked response, without buffering it all in memory. Streamed bodies are closed if the connection drops.
- `add_set_cookie(raw)`: queues a `Set-Cookie` header. Use the `set_cookie` helper for safe defaults.
- `text(encoding="utf-8")`: body as text, for buffered responses.
- `reason`, `is_streaming`: status reason phrase and streaming flag.

On the client side, a `Response` also has `status`, `headers` (lowercase names), `body` (bytes), `set_cookies` (raw `Set-Cookie` values) and these methods for streamed bodies: `iter_chunks()`, `aread()` and `aclose()`. `text()` on a streamed response raises `RuntimeError` until `aread()` has been called. `response.json()` parses the body (see Parsing responses below).

### Static files: `StaticFiles`

```python
from mini_aiohttp import StaticFiles

StaticFiles(prefix, directory, *, max_file_size=50 * 1024 * 1024, cache_control="public, max-age=3600")
```

Register it as middleware: `app.middleware_(StaticFiles("/static", "public"))`. Requests under the prefix are answered by it. All other requests go to the next middleware and route.

| Feature | Behaviour |
|---|---|
| Methods | GET and HEAD. Other methods get 405 with `Allow: GET, HEAD`. |
| Missing file | 404. Paths that escape `directory`, including encoded `..`, are 404. |
| Range | Byte ranges: `bytes=a-b`, `bytes=a-`, or suffix `bytes=-n`. One range returns 206 with `Content-Range`. Several ranges in one request return 206 as `multipart/byteranges`. An unsatisfiable range returns 416 with `Content-Range: bytes */size`. A malformed header is ignored and the full file is sent. |
| `Accept-Ranges` | `bytes` on every response. |
| `ETag` | Built from size and modification time. Weak comparison is used. |
| `Last-Modified` | Set on every response. |
| Conditional GET | `If-None-Match` (including `*`) and `If-Modified-Since` return 304 with no body. |
| `If-Range` | When the validator matches, the partial range is kept. When it does not, the full file is sent. |
| Size limit | Files over `max_file_size` get 413. |
| Content-Type | Guessed from the file name. Unknown types are `application/octet-stream`. |

Files are read in 64 KiB chunks in a worker thread, so the event loop is not blocked by disk reads.

### Sessions: `SessionMiddleware`

```python
from mini_aiohttp import SessionMiddleware, Session

SessionMiddleware(
    secret, *, cookie_name="session", max_age=3600, path="/",
    secure=True, httponly=True, samesite="Lax",
)
```

- `secret` must be at least 32 bytes. Keep it in an environment variable or secret store.
- The session data is stored in the cookie itself, as base64url JSON signed with HMAC-SHA256. The server keeps no session state, so sessions work across multiple processes that share the secret.
- The signature prevents tampering. It does not hide the data. Do not store secrets in a session.
- A missing, forged, expired or malformed cookie gives an empty session. Expiry is checked on every request.
- `req.session` supports `[]`, `get`, `pop`, `in`, `del`, and `clear()`. Changes set a new cookie. An unchanged session sends no cookie.
- Clearing the session, or popping every key, expires the cookie (`Max-Age=0`).
- Data must fit in about 3.8 KiB. A larger session raises `ValueError`.
- Set `secure=False` only for local development over plain HTTP. Logout is `req.session.clear()`. Stolen cookies stay valid until they expire, so keep `max_age` short for sensitive apps.

### Forms: `parse_urlencoded`

```python
from mini_aiohttp.forms import parse_urlencoded, FormError

parse_urlencoded(req, *, max_bytes=64 * 1024, max_fields=100) -> dict[str, list[str]]
```

Parses `application/x-www-form-urlencoded` bodies, the format of plain HTML forms. Blank values are kept, and repeated names give several values. The `Content-Type` parameters are ignored, and the media type is matched case-insensitively.

`FormError` (a `ValueError`) has a `status` attribute to return:

| Status | Cause |
|---|---|
| 415 | Content-Type is not `application/x-www-form-urlencoded` (or is missing). |
| 413 | Body is larger than `max_bytes`. |
| 400 | Too many fields, or the body is not valid ASCII form encoding. |

For `multipart/form-data` uploads, use `req.form()` instead.

### Templates: `Template`, `TemplateLoader`, `html_response`

```python
from mini_aiohttp import Template, TemplateLoader, TemplateError, html_response
```

Syntax:

| Syntax | Meaning |
|---|---|
| `{{ name }}` | Value, HTML-escaped (`&`, `<`, `>`, `"`, `'`). |
| `{{! name }}` | Value inserted raw. Use only for trusted markup. |
| `{{ user.name }}` | Dotted lookup. Works on dicts and on object attributes. |

Anything that does not match these forms is kept as literal text. There are no loops or conditions. Keep logic in Python and pass ready values to the template.

- `Template(source)`: compile a string once. `render(**context)` returns a string.
- `TemplateLoader(directory)`: loads files from a directory. Names cannot leave the directory. Files are cached and reloaded when their modification time changes. `get(name)` returns a `Template`, and `render(name, **context)` renders directly.
- A missing template, or a variable missing from the context, raises `TemplateError`. Typos fail loudly.
- `html_response(body, status=200)`: a UTF-8 HTML `Response`.

### ASGI adapter

```python
from mini_aiohttp import ASGIApp

ASGIApp(server, *, on_startup=None, on_shutdown=None)
```

`ASGIApp` wraps a `Server` so that any ASGI 3 server can run it: uvicorn, hypercorn and similar. The adapter does no routing of its own. Routes, middleware, static files and the 404, 405 and 500 responses all come from `Server`.

| Area | Behaviour |
|---|---|
| HTTP requests | Go through the same dispatch code as the built-in server, so middleware runs for every request. |
| Path | Uses the ASGI `raw_path` when provided, so percent-escapes are decoded once, as on the built-in server. |
| Request body | Larger than `max_body_size` gets 413 before routing, as on the built-in server. |
| HEAD | The body is dropped and `Content-Length` is kept. |
| Streaming | `Response.stream` chunks are passed to the ASGI server, which does the chunked framing. |
| Framing headers | Your `Content-Length` and `Transfer-Encoding` headers are ignored, because the ASGI server sets framing. |
| Client disconnect | No response is sent. |
| Lifespan | `on_startup` and `on_shutdown` run once each. If one raises, the server receives a `lifespan.*.failed` message. |
| WebSocket | Refused with a close before accept. Use `Server.websocket` on the built-in server, or `WebSocketServer` on its own port. |
| Limits | Request-line length, header count, idle timeout and connection cap are enforced by the ASGI server, not by `Server`. `max_body_size` still applies. |
| Statistics | Requests served through `ASGIApp` are counted in `server.stats`. |

```python
from mini_aiohttp import ASGIApp, Response, Server

server = Server()

@server.route("/users/{user_id}")
async def user(req):
    return Response.json({"id": req.params["user_id"]})

async def open_resources():
    ...   # e.g. create a database pool

async def close_resources():
    ...   # e.g. close it

asgi_app = ASGIApp(server, on_startup=open_resources, on_shutdown=close_resources)
```

### ClientSession

```python
ClientSession(
    *, timeout=10.0, connect_timeout=5.0, max_per_host=1000, max_idle_per_host=100,
    max_response_size=1 << 30, max_redirects=10, retries=0,
    retry_statuses=(429, 502, 503, 504), backoff=0.5, cookies=True,
    ssl_context=None, proxy=None, dns_cache_ttl=300.0,
    on_request=(), on_response=(), user_agent="mini-aiohttp",
)
```

Use it as an async context manager, or call `close()` yourself.

| Method | Description |
|---|---|
| `get(url, **kwargs)` | Shortcut for `request("GET", url, ...)`. |
| `post(url, **kwargs)` | Shortcut for `request("POST", url, ...)`. |
| `request(method, url, *, headers=None, data=None, json=None, form=None, files=None, timeout=None, stream=False, allow_redirects=True)` | Sends one request and returns a `Response`. |
| `close()` | Closes pooled connections. |

**`request` arguments:**
- `data`: `str` or bytes body, or an async iterable of chunks. An async iterable is sent with `Transfer-Encoding: chunked`.
- `json`: any JSON-serializable value. Sets `Content-Type: application/json` unless you set one.
- `form`: dict of fields, sent as `multipart/form-data`.
- `files`: dict mapping a field name to `(filename, bytes, content_type)`, sent with `form`.
- `json` cannot be combined with `form` or `files`. A streaming `data` body cannot be combined with `json`, `form` or `files`.
- `timeout`: overrides the session timeout for this request. It covers sending the request and reading the response headers, and for non-streamed responses the whole body.
- `stream=True`: reads the body lazily with `iter_chunks()`, `aread()` or `aclose()`.
- `allow_redirects=False`: returns the 3xx response instead of following it.

**Defaults and behaviour:**
- Default headers: `Host`, `User-Agent`, `Accept: */*`, and `Accept-Encoding: gzip, deflate` (plus `br` when the brotli extra is installed). A caller header with the same name, in any case, replaces the default.
- You cannot set `Content-Length` or `Transfer-Encoding` yourself. The client raises `ValueError`.
- Retries (`retries`) apply only to GET, HEAD, PUT, DELETE, OPTIONS and TRACE, and never to streaming bodies. POST and PATCH are never retried. Backoff is exponential, capped at 10 seconds. A numeric `Retry-After` is honoured up to 60 seconds. For more control, use `RetryPolicy` (below).
- Redirects (301, 302, 303, 307, 308) are followed up to `max_redirects`. 303 becomes GET. 301 and 302 become GET when sent as POST. A streaming body cannot be replayed for 307 or 308. `Authorization` and `Proxy-Authorization` are removed when the redirect goes to another origin. Only http and https targets are followed.
- Response bodies are decoded for `gzip`, `deflate` and `br`. After decoding, `content-encoding` and `content-length` are removed from `headers`.
- Bodies larger than `max_response_size` raise `ClientError`. Pass `None` to disable the limit.
- Response headers: more than 200 headers, or a line longer than 64 KiB, raise `ClientError`.
- Cookies are stored in `session.cookie_jar` (`None` when `cookies=False`). A caller-supplied `Cookie` header turns off automatic cookies for that request.
- `on_request` hooks are called as `hook(method, url, headers)`, and may be async. `on_response` hooks are called as `hook(response)`, and may be async.
- Connection pooling is per host, port and scheme. The DNS cache uses `dns_cache_ttl` seconds (0 disables it).

```python
import asyncio
from mini_aiohttp import ClientSession

async def main():
    async with ClientSession(timeout=5, retries=2) as session:
        # JSON request
        r = await session.post("https://api.example.com/items", json={"name": "a"})
        print(r.status, r.json())

        # Form plus file upload
        r = await session.request(
            "POST", "https://api.example.com/upload",
            form={"title": "photo"},
            files={"file": ("a.png", open("a.png", "rb").read(), "image/png")},
        )

        # Streamed download
        r = await session.get("https://example.com/big.bin", stream=True)
        async for chunk in r.iter_chunks():
            ...

asyncio.run(main())
```

### AutoClientSession

```python
from mini_aiohttp import AutoClientSession

AutoClientSession(*, timeout=10.0, ssl_context=None, **client_options)
```

A session for mixed HTTP/1.1, HTTP/2 and HTTP/3 traffic.
- For https URLs, the first request uses HTTP/2 when ALPN negotiates it. Otherwise it uses HTTP/1.1.
- Responses with an `Alt-Svc: h3=...` header switch later requests to that origin over HTTP/3. A failed HTTP/3 request falls back to the other protocols.
- HTTP/2 is used only when `ssl_context` is not given. With a custom context, requests use HTTP/1.1, and HTTP/3 if Alt-Svc advertises it.
- Methods: `request(method, url, *, headers=None, data=b"")`, `get(url, **kwargs)`, `post(url, **kwargs)`, `close()`. It supports async context management. It always returns a buffered `Response`, so it has no `stream` option.
- Other keyword arguments go to `ClientSession` for the HTTP/1.1 path.

### Batch requests

```python
from mini_aiohttp import fetch_all, gather_requests
```

- `fetch_all(requests, *, concurrency=100, timeout=10.0, return_exceptions=True)`: opens a session, runs all requests, and closes it.
- `gather_requests(session, requests, *, concurrency=100, return_exceptions=True)`: runs requests on an existing session.

Each item in `requests` can be a URL string (GET), a `(method, url)` or `(method, url, kwargs)` tuple, or a dict such as `{"method": "POST", "url": ..., "json": ...}`. Results come back in input order. By default a failed request is returned as its exception object, and it does not cancel the others. `concurrency` must be an integer of at least 1.

```python
results = await fetch_all([
    "https://example.com/a",
    ("POST", "https://example.com/b", {"json": {"x": 1}}),
    {"method": "GET", "url": "https://example.com/c"},
])
```

### Retry policy: `RetryPolicy`

```python
from mini_aiohttp import ClientSession, RetryPolicy

policy = RetryPolicy(max_retries=3, base_delay=0.25, max_delay=10.0)
async with ClientSession() as session:
    response = await policy.send(session, "GET", "https://example.com/api")
```

- 429 is retried for every method, because the server rejected the request before processing it.
- 502, 503 and 504 are retried only for GET, HEAD, PUT, DELETE, OPTIONS and TRACE.
- A numeric `Retry-After` header is honoured, capped at `retry_after_cap` (60 seconds). Otherwise the delay is exponential backoff with full jitter, capped at `max_delay`. An HTTP-date `Retry-After` falls back to backoff.
- Connection errors and timeouts are retried for idempotent methods only. Async-iterable request bodies are never replayed.
- When the budget runs out, `send` returns the last response (or raises the last connection error).

### Proxies and headers

```python
from mini_aiohttp import ClientSession, HeaderRotator, merge_headers, socks5_url, tor_proxy
```

- `tor_proxy(host="127.0.0.1", port=9050)` returns `socks5h://127.0.0.1:9050`, the Tor and Orbot default. Use it as `ClientSession(proxy=tor_proxy())`. The `socks5h` scheme sends DNS lookups through the proxy.
- `socks5_url(host, port, *, username=None, password=None, remote_dns=True)` builds a SOCKS5 URL and percent-encodes the credentials.
- `merge_headers(*sources)` merges header dicts. A later name replaces an earlier one, ignoring case. Values are checked for header injection.
- `HeaderRotator(presets, *, shuffle=False, seed=None)`: `next()` returns a copy of the next preset, round robin, or a random one with `shuffle=True`.

```python
rotator = HeaderRotator([{"User-Agent": "agent-a"}, {"User-Agent": "agent-b"}])
headers = merge_headers({"Accept": "application/json"}, rotator.next(), {"X-Debug": "1"})
```

### Webhooks: `WebhookRouter`

```python
import os
from mini_aiohttp import Server, WebhookRouter

async def on_update(update: dict) -> None:
    ...

app = Server()
router = WebhookRouter(on_update, secret_token=os.environ["WEBHOOK_SECRET"])
router.attach(app, "/webhook")
# on shutdown:
await router.close()
```

- The `X-Telegram-Bot-Api-Secret-Token` header is checked in constant time. A missing or wrong token gets 403. Pass `secret_header=` for other providers.
- Invalid JSON, or a body that is not a JSON object, gets 400.
- A valid update gets 200 `{"ok": true}` at once. The handler runs in a background task.
- Recent `update_id` values are remembered for `dedupe_ttl` seconds (default 1 hour, up to `dedupe_size` entries). A repeat gets 200 with `"duplicate": true` and is not dispatched again.
- Handler errors are logged and never reach the client. At most `max_tasks` handlers run at once. Beyond that the webhook answers 503 and Telegram retries later.

For Telegram specifically, prefer `TelegramWebhook` (see the Guides section), which also manages the webhook through the Bot API.

### Telegram polling: `TelegramPoller`

```python
from mini_aiohttp import ClientSession, TelegramPoller

async with ClientSession() as session:
    poller = TelegramPoller(session, token, on_update, timeout=25)
    await poller.run()   # call poller.stop() to end the loop
```

- Calls `getUpdates` with an offset, so each update is handled once. The offset moves past an update before its handler runs.
- Network errors and Telegram errors are logged by class name, and polling continues after a short backoff.
- The token appears only in the request URL and is never logged.
- Do not poll and use a webhook for the same bot at the same time.

### Traffic recorder: `TrafficRecorderMiddleware`

```python
from mini_aiohttp import Server, TrafficRecorderMiddleware

recorder = TrafficRecorderMiddleware(maxlen=500, body_preview=200)
app = Server()
app.middleware_(recorder)

for entry in recorder.records:
    print(entry["method"], entry["path"], entry["status"], entry["duration_ms"])
```

- Each record has `method`, `path`, `status`, `duration_ms`, `client` and `body_preview`. The list is oldest first.
- `body_preview` is 0 bytes by default. Request bodies can contain passwords and tokens, so turn it on only while debugging.
- A handler that raises is recorded as status 500. `clear()` empties the buffer.

### Parsing responses: `response.json()`

```python
response = await session.get("https://api.example.com/items")
items = response.json()
```

`response.json()` parses the body of a response you received. `Response.json(data, status=200)` still builds a JSON response on the server, and the same name works both ways. Parsing a streamed body needs `await response.aread()` first.

### Cookies: `CookieJar`

```python
from mini_aiohttp import CookieJar
```

`CookieJar` stores `Set-Cookie` values and returns the matching `Cookie` header. It supports the Domain, Path, Secure and Max-Age attributes. `Expires` is ignored. A Domain attribute that does not match the response host is rejected. Host-only cookies are the default. Expired cookies are removed. `update(url, set_cookie_values)` and `header_for(url)` are the main methods. `ClientSession` creates one automatically (`cookies=True`).

### WebSocket endpoints on the HTTP port

```python
from mini_aiohttp import Server

app = Server()

@app.websocket("/ws/{room}")
async def chat(ws, req):
    ...
```

`Server.websocket(path)` registers a WebSocket endpoint that is served on the same port as the HTTP routes. A client sends a GET request with `Upgrade: websocket`. If the path matches a registered endpoint, the server completes the RFC 6455 handshake and calls your handler with the connection and the request.

- `ws` is a `WebSocket`. Use `send(data)`, `recv()` and `close(code, reason)` as described in the WebSocket section below.
- `req.params` holds the path parameters, such as `room` in the example above.
- `req.headers` holds the handshake headers, including `origin` and `cookie`.

| Behaviour | Detail |
|---|---|
| Matching | A GET with `Upgrade: websocket` on a registered path. Path parameters use the same `{name}` syntax as `route`. |
| Other paths | An upgrade request to an unregistered path is handled as normal HTTP, so it gets 404 or 405. |
| Bad handshake | 400, with the reason in the body. |
| Middleware | Not run for WebSocket connections. |
| Origin | Not checked. Check `req.headers["origin"]` in the handler, especially when you authenticate with cookies. |
| Normal close | When the handler returns, the server sends close code 1000. |
| Handler error | An unhandled exception is logged and the connection is closed with code 1011. |
| Limits | Frames and messages are limited to 16 MiB. Each WebSocket connection counts toward `max_connections`. |
| Shutdown | `close()` waits up to `grace` seconds for open connections, then closes them. |

Because middleware does not run, put authentication inside the handler:

```python
ALLOWED_ORIGINS = {"https://example.com"}

@app.websocket("/live")
async def live(ws, req):
    if req.headers.get("origin") not in ALLOWED_ORIGINS:
        await ws.close(1008, "origin not allowed")
        return
    # ... your checks, then the message loop ...
```

### WebSocket (standalone server and client)

```python
from mini_aiohttp import WebSocket, WebSocketServer, WebSocketError
```

Use the standalone server when the WebSocket endpoints need their own port, or when the handler needs the full HTTP headers. It is independent of `Server`.

**Client:**

```python
WebSocket.connect(url, *, headers=None, ssl_context=None, timeout=10.0, compression=True)  # ws:// or wss://
```

- `send(data)`: `str` is sent as a text message, bytes-like as a binary message.
- `recv()`: returns a `str` for text, `bytes` for binary, or `None` after a close frame. Pings are answered automatically.
- `close(code=1000, reason="")`: sends a close frame. `reason` is at most 123 UTF-8 bytes.
- `closed`: `True` after the connection has closed.
- `headers` cannot override handshake headers (`Host`, `Upgrade`, `Connection`, `Sec-WebSocket-Key`, `Sec-WebSocket-Version`).

```python
ws = await WebSocket.connect("wss://example.com/chat")
await ws.send("hello")
reply = await ws.recv()
await ws.close(1000, "bye")
```

**Standalone server:**

```python
WebSocketServer(handler, *, ssl=None, max_connections=1000)
```

- `handler` is `async def handler(websocket, path)`. `path` is the request target, including any query string.
- `start(host="127.0.0.1", port=8765)` and `close()`.
- Frames and messages are limited to 16 MiB. The handshake must complete within 10 seconds.
- The `Origin` header is not checked, and the handler receives only the path, not the headers.

```python
async def handler(ws, path):
    while True:
        msg = await ws.recv()
        if msg is None:
            break
        await ws.send(msg)

server = WebSocketServer(handler)
await server.start("127.0.0.1", 8765)
```

Message compression (permessage-deflate, RFC 7692) is offered by default. Pass `compression=False` to `WebSocket.connect()` to turn it off. `WebSocket.accept()` negotiates it on the server side.

`WebSocketError` is raised for invalid handshakes, frames, or use of a closed connection.

### AI chat helper (`mini_aiohttp.ai`)

```python
import os
from mini_aiohttp import ClientSession, groq, gemini, openrouter, ollama, ChatClient, AIError
```

An OpenAI-compatible `/chat/completions` client. It builds requests and reads replies only, so no provider SDK is needed.

```python
async with ClientSession() as session:
    ai = groq(session, api_key=os.environ["GROQ_API_KEY"], model="<model name>")
    print(await ai.chat([{"role": "user", "content": "Hello"}], temperature=0.2))

    async for piece in ai.stream([{"role": "user", "content": "Tell me a story"}]):
        print(piece, end="")
```

- Presets: `groq(session, *, api_key, model)`, `gemini(session, *, api_key, model)`, `openrouter(session, *, api_key, model)`, and `ollama(session, *, model, host="http://127.0.0.1:11434")`. Ollama needs no key.
- `ChatClient(session, *, base_url, api_key, model, timeout=60.0, headers=None)`: for any other OpenAI-compatible endpoint. `base_url` must start with `https://` or `http://`. Pass `api_key=None` for servers that need no key.
- `chat(messages, **options)` returns the reply text. `stream(messages, **options)` is an async generator of text pieces.
- Extra keyword options (`temperature`, `max_tokens` and so on) are sent unchanged. `model`, `messages` and `stream` cannot be passed as options, because the client sets them. Passing one raises `ValueError`.
- A non-200 reply raises `AIError`, which has `status` and `message`. The API key is never included in the message.
- Model names change often, so check each provider's current model list.

### HTTP/2 adapters (extra: `http2`)

```python
from mini_aiohttp import HTTP2Client, HTTP2Server
```

- `HTTP2Client(*, ssl_context=None, timeout=15.0)`: `get(url, *, headers=None)` and `request(method, url, *, headers=None, data=b"")`. Both return `(status, headers, body)`. Only `https://` URLs are supported, and ALPN must negotiate `h2`. Requests to the same origin share one pooled connection and run as parallel streams. The client waits for the server's `MAX_CONCURRENT_STREAMS` before opening a stream. `close()` closes the pooled connections.
- `HTTP2Server(handler, *, ssl_context, max_body_size=10 * 1024 * 1024, max_concurrent_streams=100, max_header_list_size=64 * 1024, idle_timeout=30.0)`: `start(host="127.0.0.1", port=8443)`, the `port` property, and `close(*, grace=5.0)`. The handler is `async def handler(method, path, headers, body)` and returns `(status, headers, body)`. Each stream runs in its own task, so a slow request does not hold up the others. The adapter sets `h2` as the ALPN protocol on `ssl_context`, so use a context dedicated to this server.

Server limits (each violation affects only what it names):

| Situation | Result |
|---|---|
| More than `max_concurrent_streams` open at once | The connection gets GOAWAY (PROTOCOL_ERROR) and is closed. |
| Request headers larger than `max_header_list_size` | That stream is reset (ENHANCE_YOUR_CALM). |
| Body larger than `max_body_size` | That stream is reset. Other streams continue. |
| No bytes received for `idle_timeout` seconds | The connection gets GOAWAY (NO_ERROR) and is closed. |
| Requests that arrive during `close()` | The stream is refused (REFUSED_STREAM). |

`close(grace=...)` stops accepting new connections, lets in-flight streams finish for up to `grace` seconds, then sends GOAWAY(NO_ERROR) and closes the connections.

```python
import ssl

ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ctx.load_cert_chain("cert.pem", "key.pem")

async def handler(method, path, headers, body):
    return 200, {"content-type": "text/plain"}, b"hello over h2"

server = HTTP2Server(handler, ssl_context=ctx)
await server.start("127.0.0.1", 8443)
```

### HTTP/3 adapters (extra: `http3`)

```python
from mini_aiohttp import HTTP3Client, HTTP3Server
```

HTTP/3 runs over QUIC on UDP and requires TLS 1.3. It is not a fallback for TCP connections, and the endpoint must support HTTP/3.

- `HTTP3Client(*, verify=True, timeout=15.0, max_concurrent_streams=100)`: `get(url, *, headers=None)` and `request(method, url, *, headers=None, data=b"")`. Both return `(status, headers, body)`. `verify=False` disables certificate verification. Requests to the same origin share one pooled QUIC connection. `max_concurrent_streams` caps parallel requests on it, and must not exceed the server's limit. If a connection drops, its pending requests fail with `ConnectionError`, and the next request opens a new connection. `close()` closes the pooled connections.
- `HTTP3Server(handler, *, certificate, private_key, max_body_size=10 * 1024 * 1024, max_concurrent_streams=100, max_header_list_size=64 * 1024, idle_timeout=30.0)`: `start(host="0.0.0.0", port=443)`, the `port` property, and `close(*, grace=5.0)`. `certificate` and `private_key` are file paths. The handler signature is the same as HTTP/2.

Server limits:

| Situation | Result |
|---|---|
| More than `max_concurrent_streams` requests open on one connection | The new stream is reset (H3_EXCESSIVE_LOAD). |
| Request headers larger than `max_header_list_size` | The stream is reset (H3_EXCESSIVE_LOAD). |
| Body larger than `max_body_size` | 413. |
| No traffic for `idle_timeout` seconds | The QUIC connection is closed. |

`close(grace=...)` refuses new requests with H3_REQUEST_REJECTED, which tells the client it is safe to retry elsewhere. In-flight responses finish for up to `grace` seconds, then the QUIC connections close with NO_ERROR.

### Security helpers

```python
from mini_aiohttp import cors, security_headers, RateLimiter, ip_policy, set_cookie, require_json
```

- `cors(*, allow_origins, allow_methods=("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"), allow_headers=("Content-Type", "Authorization"), allow_credentials=False, max_age=600)`: middleware. Only origins in `allow_origins` are reflected, by exact match. `"*"` is allowed only without credentials, otherwise `ValueError`. Preflight requests (OPTIONS with `Access-Control-Request-Method`) get 204.
- `security_headers(*, csp="default-src 'self'", hsts_max_age=None)`: middleware. Adds `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`, `Referrer-Policy: no-referrer`, a CSP, and optionally HSTS. Headers the handler already set are kept.
- `RateLimiter(*, rate, burst, max_clients=10_000, clock=time.monotonic)`: token bucket keyed by client IP. `rate` is tokens per second, `burst` is the bucket size. `check(key)` returns `None` if allowed, or the seconds to wait. `middleware()` returns 429 with `Retry-After`. Memory is bounded by `max_clients`.
- `ip_policy(*, allow=None, deny=None)`: middleware that allows or denies IP addresses and CIDR networks. `deny` is checked first. If `allow` is given, only those networks pass. Clients with an unknown address get 403. IPv4-mapped IPv6 clients are checked as IPv4.
- `set_cookie(resp, name, value, *, max_age=None, path="/", secure=True, httponly=True, samesite="Lax")`: adds a `Set-Cookie` header to `resp` and returns `resp`. The value is URL-encoded. `SameSite=None` requires `secure=True`.
- `require_json(schema)`: decorator for handlers. `schema` maps field names to types, or tuples of types such as `(int, float)`. A missing field or wrong type gets 400, and so does invalid JSON. A bool is rejected for an int field.

```python
@app.route("/users", method="POST")
@require_json({"name": str, "age": int})
async def create(req):
    return Response.json({"ok": True})
```

### Compression

```python
from mini_aiohttp import gzip_middleware
```

`gzip_middleware(*, min_size=1024, level=6)` compresses buffered responses for clients that accept gzip. It skips bodies under `min_size`, responses that already have `Content-Encoding`, and types that are not text, JSON, JavaScript, XML or SVG. Streamed responses pass through unchanged. It adds `Vary: Accept-Encoding`. `level` must be 1 to 9.

### Multipart uploads

- Client: `session.request("POST", url, form={"title": "x"}, files={"avatar": ("a.png", data, "image/png")})`.
- Server: `req.form()` returns `{name: [str or UploadedFile]}`. `UploadedFile` is a dataclass with `filename`, `content_type` and `data`. At most 200 parts are accepted.

### Proxies

- `proxy="http://host:port"`: HTTP proxy. HTTPS targets use CONNECT tunnels.
- `proxy="socks5://user:pass@host:port"`: SOCKS5 proxy. DNS is resolved locally.
- `proxy="socks5h://..."`: SOCKS5 proxy with DNS resolved by the proxy.

An HTTP proxy must support CONNECT for HTTPS. Proxy authentication is supported for SOCKS5 only.

### Optional accelerators

- `use_uvloop()`: switches asyncio to uvloop if it is installed. Call it before `asyncio.run()`. Returns `True` when it switched.
- `brotli` extra: enables `br` decoding. Without it, a `br` response raises `ClientError`.
- `orjson` extra: used automatically by `Response.json`, `Request.json` and the JSON helpers when installed.
- `certifi` extra: the default TLS context uses the certifi CA bundle.

### Extras (`mini_aiohttp.extras`)

Each extra imports its package only when it is used.

- `metrics.prometheus_middleware(registry=None, *, namespace="http")` and `metrics_handler(registry=None)`: counts requests by method and status, records latency by method, and serves the Prometheus text format. Extra: `prometheus`.
- `jwt_auth.jwt_auth(secret, *, algorithms=("HS256",), audience=None, issuer=None, exempt=())`: requires `Authorization: Bearer <token>`. Decoded claims are stored in `req.user`. Missing or invalid tokens get 401 with `WWW-Authenticate`. `exempt` lists exact paths that skip authentication. Extra: `jwt`.
- `pydantic_json.validated_json(model)`: decorator that parses the body into a pydantic v2 model and stores it in `req.model`. Invalid JSON gets 400. Validation errors get 422 with `{"detail": [...]}`. Extra: `pydantic`.
- `redis_limiter.RedisRateLimiter(client, *, rate, window=1, prefix="mini-aiohttp:rl", clock=time.time)`: fixed-window rate limit shared across processes. `client` is any async Redis client with `incr` and `expire`, such as `redis.asyncio.Redis`. `check(key)` is async. `middleware()` returns 429 with `Retry-After`. No extra package is needed.
- `tracing.tracing_middleware(*, tracer_name="mini_aiohttp")`: creates one server span per request, with the method, path and status code. Spans use the globally configured OpenTelemetry tracer. Extra: `otel`.

### Errors

- `ClientError`: client-side failures, including malformed responses, oversized bodies, too many redirects and unsupported encodings.
- `WebSocketError`: WebSocket handshake, frame and connection errors.
- `FormError`: a form body that cannot be parsed. It has a `status` for the response.
- `TemplateError`: a missing template, or a variable missing from the template context.
- `TelegramError`: Telegram answered with `ok=false`, or with an unexpected reply. Its `description` attribute holds Telegram's message.
- `AIError`: the AI provider returned an error, or a reply that could not be understood. It has `status` and `message`.
- `ValueError`: invalid arguments, such as an unsupported URL scheme, an invalid header, a session secret shorter than 32 bytes, or a reserved option passed to `ChatClient`.
- The server never lets a handler exception reach the client. It returns 500 and logs the traceback.

---

## 🌊 Streaming Responses

Create asynchronous chunked responses without buffering the complete response body.

```python
import asyncio
from mini_aiohttp import Server, Response

app = Server()

@app.route("/stream")
async def stream(req):

    async def generate():
        for i in range(5):
            yield f"Message {i}\n"
            await asyncio.sleep(1)

    return Response.stream(
        generate(),
        headers={"Content-Type": "text/plain"}
    )

asyncio.run(app.serve_forever("127.0.0.1", 8080))
```

Supports HTTP/1.1 chunked response transfer.

---

## 📊 Server Statistics

Monitor basic server activity:

```python
print(app.stats)
```

Example output:

```python
{
    "active_connections": 0,
    "total_connections": 1,
    "total_requests": 2,
    "status_counts": {
        200: 1,
        404: 1
    },
    "uptime_requests_per_connection": 2.0
}
```

Statistics are process-local and represent basic runtime counters.

---

## 🧪 Testing

From a source checkout, install the development tools:

```bash
pip install -e ".[dev,all]"
```

Run tests with coverage:

```bash
python -m pytest -v --cov
```

Lint, format and type-check:

```bash
ruff check .
ruff format --check .
mypy
```

Enable the git hooks once:

```bash
pre-commit install
```

Build distributions:

```bash
pip install build twine
python -m build
python -m twine check dist/*
```

---

## ⚙️ Compatibility

| Component | Support |
|---|---|
| Python | 3.11+ |
| Type hints | Yes (`py.typed`) |
| HTTP/1.1 | Yes |
| HTTPS Server | Yes |
| Chunked Requests | Yes |
| Streaming Responses | Yes |
| Content-Encoding | gzip, deflate (core); br (`brotli` extra) |
| Response compression | gzip middleware (core) |
| Static files | `StaticFiles`: Range (single and multi-range), ETag, Last-Modified, conditional GET |
| Sessions | `SessionMiddleware`: signed cookies, HMAC-SHA256 |
| Forms and templates | `parse_urlencoded`, `Template`, `TemplateLoader` |
| Auth and CSRF | `csrf_protect`, `basic_auth`, `api_key_auth` |
| Proxies and Cloudflare | `cloudflare_client_ip`; SOCKS5, Tor, HTTP CONNECT |
| Background work | `BackgroundTasks`, `TaskQueue`, `serve()` with ordered shutdown |
| Route limits | `route_middleware`, `route_limit` |
| Retries | `RetryPolicy`: 429 and 502/503/504 with Retry-After and jittered backoff |
| Webhooks and Telegram | `TelegramWebhook`, `WebhookRouter`, `TelegramPoller` |
| Databases | `PostgresPool` (asyncpg), `RedisClient`, `SQLAlchemyEngine`, `MongoDatabase` (motor), `FirebaseRTDB` (REST) |
| AI chat | `ChatClient` and presets for Groq, Gemini, OpenRouter, Ollama |
| HTTP/2 | Optional adapters (`HTTP2Client`, `HTTP2Server`): pooled client, parallel streams, limits, graceful close |
| HTTP/3 | Optional adapters (`HTTP3Client`, `HTTP3Server`): pooled client, parallel streams, limits, graceful close |
| WebSockets | RFC 6455 client; server on the HTTP port (`Server.websocket`) or standalone (`WebSocketServer`); permessage-deflate |
| ASGI 3 | `ASGIApp` for HTTP and lifespan (no WebSocket) |
| Prebuilt wheels | Linux x86_64, macOS x86_64 and arm64, Windows x86_64 (CPython 3.11–3.13) |

---

## 🛡️ Protocol Limitations

- HTTP/2 adapters require TLS ALPN negotiation. The client keeps one connection per origin. The server enforces its own stream, header and idle limits, which are listed in the HTTP/2 section.
- HTTP/3 requires QUIC/UDP and TLS 1.3 certificates, plus an HTTP/3-capable endpoint. It cannot be transparently substituted on arbitrary TCP connections.
- HTTP/3 `max_concurrent_streams` is enforced by the application. aioquic advertises a fixed QUIC stream limit of 128 per connection, so keep the server's limit at or below that.
- WebSocket endpoints on the HTTP port (`Server.websocket`) bypass middleware, so authentication and Origin checks belong in the handler. The standalone `WebSocketServer` has the same limits.
- WebSocket Origin checking is not built in. If you authenticate with cookies, validate the `Origin` header in your handler.
- WebSocket compression (`permessage-deflate`) is negotiated by default for `WebSocket.connect()`. Pass `compression=False` to turn it off.
- `StaticFiles` answers multi-range requests with `multipart/byteranges`. Range and ETag are not available through the older `Server.static()` handler.
- `StaticFiles` ETags are based on size and modification time, not content hashes.
- Sessions are signed, not encrypted. The data is readable by the client, and a stolen cookie is valid until it expires.
- The template engine has no loops, conditions or template inheritance. Use Python for logic.
- No automatic multi-process worker manager is included. Use your ASGI server's worker options (for example `uvicorn --workers`) with `ASGIApp`.
- WebSockets are not available through `ASGIApp`. Use `Server.websocket` on the built-in server, or run `WebSocketServer` on its own port.
- `TelegramPoller`, `TelegramWebhook` and `WebhookRouter` deliver updates to your handler. They do not handle Telegram's file downloads or payment flows.
- `ChatClient` is a thin OpenAI-compatible client. It does not handle provider-specific features such as tool calling or file uploads.
- Basic auth and CSRF helpers are for simple apps. Use HTTPS in production, and treat `verify=` callbacks as synchronous.
- `TaskQueue` keeps jobs in memory. Jobs that have not run are lost if the process stops. Use a database or Redis queue for jobs that must survive a restart.

Additional notes:

- HTTP/2 and HTTP/3 are optional adapters and require their respective extras.
- `AutoClientSession` uses HTTP/2 only when no custom `ssl_context` is given.
- `max_response_size` limits encoded and decoded response sizes. Gzip/deflate use zlib's bounded-output API; Brotli's Python decoder has no output-length parameter, so its output is checked after each decoder call rather than hard-capped during allocation.
- HTTPS proxy tunnels require a CONNECT-capable HTTP proxy or SOCKS5 proxy.
- Streaming responses require asynchronous iterables.
- Under `ASGIApp`, the ASGI server enforces the request-line, header-count and keep-alive limits from the server limits table. `max_connections` and `read_timeout` on `Server` apply only to the built-in `start()` and `serve_forever()` path.
- Prebuilt wheels are not published for Linux aarch64, 32-bit platforms or musl Linux. Those platforms install from the source distribution.

---

## 📚 Links

- **PyPI Package:** https://pypi.org/project/mini-aiohttp/

---

## 📄 License

This project is licensed under the MIT License.

---

**mini-aiohttp v1.4.1**

*Lightweight. Asynchronous. Flexible.*
