Metadata-Version: 2.2
Name: httpp
Version: 0.8.1
Summary: A Cython project with scikit-build
Author-Email: Mohammad Raziei <mohammadraziei1375@gmail.com>
License: MIT
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: Free Threading :: 1 - Unstable
Project-URL: Homepage, https://github.com/MohammadRaziei/httpp
Project-URL: Bug Tracker, https://github.com/MohammadRaziei/httpp/issues
Project-URL: Document, https://MohammadRaziei.github.io/httpp
Requires-Python: >=3.8
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == "test"
Requires-Dist: pytest-xdist>=3.0; extra == "test"
Requires-Dist: pytest-timeout>=2.3; extra == "test"
Requires-Dist: pytest-cov>=4.0; extra == "test"
Description-Content-Type: text/markdown

# httpp

<div align="center">

**A lightweight HTTP client and server for Python and C++: one `pip install`, no system dependencies.**

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE.txt)
[![Python 3.8+](https://img.shields.io/badge/Python-3.8+-blue.svg)](https://www.python.org/)
[![C++17](https://img.shields.io/badge/C++-17-blue.svg)](https://en.cppreference.com/w/cpp/17)
[![CMake 3.21+](https://img.shields.io/badge/CMake-3.21+-blue.svg)](https://cmake.org/)
[![Status: alpha](https://img.shields.io/badge/status-alpha-orange.svg)](#status)

</div>

```python
from httpp import Server

app = Server()

@app.get("/hello/{name}")
def hello(name):
    return f"hello {name}"

app.listen("127.0.0.1", 8000)   # that's it, no separate server process needed
```

`pip install httpp` gives you a Python package, a command-line tool, and a compiled C++ library with
headers and a CMake config. Every third-party component is built from source, so nothing has to be
installed system-wide (no `apt`, `brew` or `choco`).

## Contents

- [Features](#features)
- [Install](#install)
- [Python](#python)
  - [Serving: routes](#serving-routes) · [Handler reference](#handler-reference) · [`def` or `async def`](#def-or-async-def) · [Routers](#routers) · [Forms and uploads](#forms-and-uploads) · [`response` and background work](#the-response-argument-and-background-work) · [Streaming and files](#streaming-and-files) · [File browser](#a-read-only-file-browser) · [Settings](#settings) · [Hooks and error handling](#hooks-and-error-handling) · [Free-threaded Python](#free-threaded-python-313t-314t) · [Command line](#command-line) · [Running the server](#running-the-server) · [Sessions and async](#sessions-and-async) · [Testing your app](#testing-your-app)
- [Build from source](#build-from-source)
- [Status](#status)
- [Logging](#logging)
- [License](#license)

## Features

- **Client and server in one library.** Most lightweight HTTP libraries only do one side.
- **Decorator-style routing in Python**, in the spirit of Flask and FastAPI: path parameters, typed
  conversion, JSON responses, `async def` handlers.
- **Built-in server.** No ASGI/WSGI layer and no separate process to launch.
- **Real async.** `async def` handlers run on one shared asyncio event loop and release their
  worker thread while they `await`. Idle connections wait in an event loop too (epoll, kqueue or IOCP,
  via [Asio](https://think-async.com/Asio/)), so thousands of slow or idle requests cost
  almost no threads.
- **Python, C++ and a CLI from the same wheel.** One binary core, three front ends.
- **HTTP and HTTPS** on the client (mbedtls, vendored).
- **WebSocket server (C++ so far).** `server::route_ws` with callbacks on the I/O thread, thread-safe sends and a bounded send
  queue ([ADR-0026](docs/adr/0026-websocket.md)); Python: `@app.websocket` with `async def` ([ADR-0027](docs/adr/0027-websocket-python-and-async-handshake.md)).
- **Hooks, streaming and files** on the server: `before_request` / `after_request`, error and exception
  handlers, generators as streamed bodies, files sent from disk, cookies and redirects.
- **A client with a real error model**: `res.json()`, `res.error`, `raise_for_status()`, typed
  exceptions, sessions, an `AsyncClient`, timeouts, redirects, TLS verification.
- **Downloads** that can resume, with a tqdm-style progress bar (drawn on stderr, so piped output
  stays clean). The features above exist in both C++ and Python, with similar APIs.
- **A migration path for `libcurl` code**: `httpp/curl_compat.h` provides a `curl_easy_*` subset.
- **No system dependencies.** Nothing to install system-wide; the only build-time download is the
  header-only Asio (about 3 MB, pinned by hash), which can be supplied locally instead.

## Install

```bash
pip install httpp
```

Requires Python 3.8 or newer. Building the C++ side on its own needs CMake 3.21+ and a C++17 compiler
(see [Build from source](#build-from-source)).

## Python

### Serving: routes

Register handlers with decorators, then call `listen()`:

```python
import asyncio
from httpp import Server

app = Server()

@app.get("/hello/{name}")              # "/hello/<name>" is accepted too
def hello(name):
    return f"hello {name}"

@app.get("/square/{n}")
def square(n: int):                    # annotate as int/float to get a converted value
    return {"n": n, "square": n * n}   # dict/list -> JSON

@app.post("/items")
def create_item(request):              # ask for `request` by name
    return {"got": request.json()}, 201, {"Location": "/items/1"}

@app.get("/slow")
async def slow():                      # async handlers: no thread is held while awaiting
    await asyncio.sleep(1)
    return "done"

app.listen("127.0.0.1", 8000)
```

All of `get`, `post`, `put`, `patch` and `delete` are available, and `route()` takes a list of methods
(`@app.route("/thing", methods=["PUT", "PATCH"])`). Register routes **before** calling `listen()`.

Static files can be served next to your routes:

```python
app.serve_directory("/static", "./public")
```

### Handler reference

A handler may declare any of its path parameters by name, plus `request`. It returns one of:

| Return value | Response |
|---|---|
| `str` | `200`, `text/plain; charset=utf-8` |
| `bytes` | `200`, `application/octet-stream` |
| `dict` / `list` | `200`, `application/json` |
| `None` | `200`, empty body |
| `(body, status)` | same, with your status code |
| `(body, status, headers)` | same, with extra headers: a dict, or a list of `(name, value)` pairs when a name repeats, e.g. several `Set-Cookie` (a `Content-Type` you set wins) |
| a generator / async generator | `200`, sent in chunks as it produces `str` or `bytes` pieces (see [Streaming and files](#streaming-and-files)) |
| `httpp.file(path)` | `200`, the file streamed from disk, `Content-Type` guessed from its extension |

The `request` object:

| Attribute | Meaning |
|---|---|
| `method`, `path` | HTTP method and path |
| `body` / `text` / `json()` | raw bytes, decoded text, parsed JSON |
| `headers` | `dict`, names lower-cased (`request.headers["x-token"]`) |
| `query` | `dict` of `?a=1` parameters |
| `path_params` | `dict` of captured path parameters (all `str`) |
| `cookies` | `dict` parsed from the `Cookie` header (first value wins) |

For repeated headers or query names the first value wins.

Error behavior:

- Return a status yourself for expected failures: `return {"error": "not found"}, 404`.
- A path parameter annotated `int`/`float` that does not parse answers **422** automatically.
- An uncaught exception answers a generic **500**; the traceback goes to stderr, never to the client.
  `@app.exception_handler(...)` and `@app.error_handler(...)` let you shape these answers (see
  [Hooks and error handling](#hooks-and-error-handling)).
- A response header whose name or value contains a line break is refused (a 500), never sent.

### `def` or `async def`

Use `def` for CPU work and for blocking libraries (a database driver, `requests`, `time.sleep`). It
runs on a pool thread, which is the faster path when the handler does nothing slow. Use `async def`
when the handler mostly waits on the network and you can `await` it.

An `async def` handler with no `await` at all (and no `Depends`) skips the hop to the event loop thread when the server is idle
enough: it runs right on the I/O thread, while the loop thread sleeps and no `def` handler runs (Python 3.12+; ADR-0023). It gives the same
answers as before; it only makes such handlers about 30 percent cheaper. A handler that awaits always runs on the loop thread.

The difference shows when handlers wait. With a 10 ms wait per request (2 cores, Python 3.13, one
run; see `benchmarks/io_bench.py`):

| connections | 32 | 128 | 512 |
|---|---|---|---|
| `def` with `time.sleep` | 3.0k req/s | 3.1k | 3.2k |
| `async def` with `asyncio.sleep` | 2.8k req/s | 10.2k | 32.5k |

A blocking `def` handler holds one pool thread (about 32) for the whole wait, so throughput stops at
roughly 3k req/s and latency grows with the load. A waiting coroutine holds no thread, so `async def`
keeps scaling. Never call a blocking function inside `async def`: it stalls the shared event loop
for every request.

### Routers

Group routes under a shared prefix and add them to a server with `include()`. The same idea exists in C++
(`httpp::router`, `server::include`), with the same rules:

```python
from httpp import APIRouter, Server

users = APIRouter("/users", tags=["users"])

@users.get("/{user_id}")
def one(user_id: int):
    return {"id": user_id}

api = APIRouter("/api")
api.include(users)            # GET /api/users/{user_id}

app = Server()
app.include(api, "/v1")       # GET /v1/api/users/{user_id}  (include_router is the same method)
```

The order is: outer prefix, the router's own prefix, then the route's path. A route path `/` under a prefix is the
prefix itself (`/users`, never `/users/`). Prefixes are joined once, when `include()` runs, so a request costs the same as
with plain `@app.get(...)`. A router is not kept by the server: routes added to it afterwards are not on the server.
WebSocket routes work the same way (`@users.websocket("/ws/{room}")`, C++: `router::route_ws`), and a WebSocket handler may use `Depends` (for example to check a token before the upgrade).

### The `response` argument and background work

A handler that declares an argument called `response` gets a mutable object, the Python side of the C++ `response&`.
What you set is added to the answer; the handler still returns the body:

```python
from httpp import BackgroundTasks

@app.post("/signup")
def signup(request, response, tasks: BackgroundTasks):
    response.status = 201                       # wins over a status the handler returns
    response.add_header("X-Trace", "abc")
    response.set_cookie("sid", "token")
    tasks.add_task(send_welcome_mail, "a@b.c")  # runs after the answer was written
    return {"ok": True}
```

`tasks` is found by its annotation (`BackgroundTasks`), any name works. Tasks run in order on a worker thread after the
answer was written, so the client never waits for them; an `async def` task is awaited on the shared event loop. A task
that raises is printed and the next one still runs. If the handler raises, no task runs. Both are available to
routes only, not to dependencies.

Underneath there is one job per answer, `response.after_send` (C++: `res.after_send = [] { ... };`), which you can set
yourself. It never runs when the answer was never sent (the server stopped first).

### Forms and uploads

`request.form()` returns a `FormData` for an `application/x-www-form-urlencoded` or `multipart/form-data` body (any other
`Content-Type` gives an empty one). C++ parses it, and only when you ask. In C++ it is `req.form()` with the same names:

```python
@app.post("/upload")
def upload(request):
    form = request.form()            # a malformed body answers 400 by itself
    doc = form.file("doc")           # an UploadFile: filename, content_type, data, size, text(), save(path)
    return {"who": form.field("who"), "tags": form.getlist("tag"), "name": doc.filename, "size": doc.size}
```

Or declare what you need, like FastAPI. These are converted and checked like query parameters (a bad one answers **422**,
`loc` starting with `"body"`), and they appear in the OpenAPI document as a form body:

```python
from httpp import File, Form, UploadFile

@app.post("/profile")
async def profile(name: str = Form(), age: int = Form(18), avatar: UploadFile = File(), extra: list[UploadFile] = File(None)):
    ...
```

`File()` can also be annotated `bytes`. The whole body is in memory: size it with `max_body` (default 100 MiB). `filename` is what the
client sent, cut to its last path component; never build a disk path from it unchecked. At most 1024 parts and 8 KiB of headers per part.
`UploadFile.read()` is not there: the data is already in `data` (no `await` needed).

### Streaming and files

```python
import asyncio, httpp

@app.get("/events")
async def events():
    async def pieces():
        for n in range(3):
            await asyncio.sleep(1)
            yield f"data: {n}\n\n"            # str or bytes
    return pieces(), 200, {"Content-Type": "text/event-stream"}

@app.get("/report")
def report():
    return httpp.file("/srv/reports/latest.pdf")      # streamed from disk, length known

@app.get("/old")
def old():
    return httpp.redirect("/new", 301)                # default status 302

@app.get("/login")
def login():
    return "welcome", 200, [httpp.set_cookie("session", token, max_age=3600, same_site="lax"),
                            httpp.set_cookie("theme", "dark", http_only=False)]
```

- A **generator** (or **async generator**, in a plain or an `async def` route) is sent chunked, one
  piece at a time, so the whole body never sits in memory. Without a `Content-Type` of yours it is
  `text/plain; charset=utf-8`. A generator that raises midway **drops the connection**: the client sees a
  truncated body rather than a clean end, and the traceback goes to stderr. If the client hangs up, the
  generator is released.
- `httpp.file(path)` takes an optional `content_type`. A missing or unreadable file answers **404**.
  `path` is trusted: never build it from user input (path traversal); use `serve_directory` for that.
- `httpp.set_cookie` defaults to `Path=/; HttpOnly`, like the C++ helper. A name or value that is not
  cookie-safe raises `ValueError` (percent-encode spaces, quotes, commas and semicolons yourself).

### A read-only file browser

```python
app.mount_file_browser("/files", "/srv/shared", title="Shared", theme="dark", accent="#2a7de1", show_hidden=False)
```

```
httpp browse ~/Downloads --port 8000 --theme auto
```

Lists folders, opens text and images, downloads files; nothing can be written, renamed or deleted. The folder you give is a ceiling:
`..`, absolute paths and symlinks that leave it are refused, dotfiles are hidden (404 too) unless `show_hidden=True` / `--hidden`, and html, svg and
other active types are always downloads. A folder is cut at `max_entries` (5000). It has **no login**: it listens on 127.0.0.1 by default, and for anything
else put a `before_request` hook in front of it. A bad option or folder raises `ValueError`.

### Settings

```python
app = Server(threads=8, read_timeout=10, max_body=5_000_000, handler_timeout=30)
```

| Argument | Meaning (leave it out to keep the default) |
|---|---|
| `threads`, `max_threads` | worker threads for plain `def` handlers: `threads` are kept, up to `max_threads` are used under load. Default: about one per core, at least 8, growing to 4x that |
| `read_timeout`, `write_timeout`, `keep_alive_timeout` | seconds to read a request, write a response, keep an idle connection (default 5 each) |
| `keep_alive_max` | requests served per connection (default 1000, `0` = no limit) |
| `max_body` | largest request body in bytes, refused above it with a **413** (a client still sending a huge body may see the connection closed instead); `0` = no limit |
| `header_timeout` | seconds the whole request head (request line and headers) may take, however slowly it is dripped (default 10, `0` = no limit): the slowloris guard. |
| `body_timeout` | the same guard for the request body, counted from the end of the head (default 120, `0` = no limit) |
| `compression` | gzip text answers (`text/*`, JSON, XML, JavaScript, SVG) of at least this many bytes, for clients that send `Accept-Encoding: gzip` (default: off). `compression_level` is 1 (fastest, default) to 9. Files, streams and already encoded bodies are left alone; a `Vary: Accept-Encoding` header is added |
| `io_threads` | I/O threads sharing the port (default 1; not on Windows). On a free-threaded Python each also gets an asyncio loop of its own |
| `max_connections` | open connections allowed at once; one more gets a **503** and is closed (default: no limit) |
| `handler_timeout` | seconds an `async def` handler may take (default **60**, `None` = never): after that the coroutine is **cancelled** and the client gets a **504** |

`handler_timeout` only covers `async def` handlers: a plain `def` handler cannot be cancelled. The time
spent streaming the body afterwards is not counted. The same settings exist as methods
(`app.set_read_timeout(10)`, ...), each returning `app`, mirroring the C++ `httpp::server`.

### Hooks and error handling

```python
@app.on_startup
def starting():
    print("about to listen")

@app.before_request
def require_key(request):
    if request.headers.get("x-key") != "secret":
        return {"error": "forbidden"}, 403          # answers here; the route never runs

@app.after_request
def stamp(request, response):
    response.set_header("X-Served-By", "httpp")      # status and headers only

@app.error_handler(404)
def not_found(request):
    return {"error": "no such page", "path": request.path}

@app.exception_handler(ValueError)
def bad_value(request, exc):
    return {"error": str(exc)}, 400
```

- `before_request` returns `None` to carry on, or anything a route may return to answer right there.
  `after_request` gets a `ResponseHead` (`status`, `headers` as a list of pairs, plus `header()`,
  `set_header()`, `add_header()`); the body is not passed, because it may be a huge file or a stream.
- Both run for **every** request, static files and unknown paths included, and you can register several:
  they run in registration order. Hooks may be `async def`, but they hold their worker thread while they
  await. A hook that raises answers a **500**.
- `error_handler(404, ...)` dresses only errors **the server** makes itself (no such route, body too
  large, ...), never a response a route returned on purpose, and it keeps the original status unless
  you return another. A handler that raises leaves the plain error as it was.
- `exception_handler(Exc, ...)` runs when a route raises; the most specific class wins, the status
  defaults to 500, and an exception nobody handles is still the generic 500. A handler that raises
  itself is a generic 500 too.
- `on_startup` / `on_shutdown` (plain or `async def`) run around `listen()`: startup before the server
  listens, shutdown after it stopped, in reverse order. A failing startup hook stops the launch; a failing
  shutdown hook is logged and the others still run.

### Free-threaded Python (3.13t, 3.14t)

There are three kinds of wheel: a `cp39-abi3` wheel per platform for every ordinary Python 3.9+, a
`cp313-abi3` wheel per platform that pip prefers on Python 3.13+ (it is built against the newer stable ABI),
and version-specific `cp313-cp313t` / `cp314-cp314t` wheels for the free-threaded builds, which have no
stable ABI yet (it arrives with 3.15 as `abi3t`; we will move to it once Cython and
scikit-build-core support it). The extension declares itself free-threading compatible, so importing
httpp keeps the GIL off. On such an interpreter plain `def` handlers on the server's pool threads
are meant to run in parallel, in one process. What is verified so far: the whole Python test suite
and a mixed sync/async/client stress test pass on 3.14t with the GIL off, on a single-core machine;
speed-up on several cores is not measured yet, and the classifier says "Unstable" until it is.
Your own handler code must be thread-safe: shared dicts, counters and globals now really are
touched by several threads at once.

### Command line

`httpp` is a command that starts a native executable (`httpp/sdk/bin/httpp` inside the package; `bin/httpp` of a C++ SDK install runs on its own). The commands below need no Python and run without the GIL:

```sh
httpp server ./site --port 8000          # serve a directory
httpp browse ~/Downloads                 # read-only file browser (see below)
httpp download https://example.com/f.zip -o f.zip
httpp curl https://example.com/api -X POST -H "X-Key: 1" -d "a=1"
```

Commands that run your Python code go behind `py`; everything after `py` goes to Python as typed (the SDK's own `bin/httpp` starts the Python named by `HTTPP_PYTHON`, or the one on PATH):

```sh
httpp py run main:app --reload           # was `httpp run main:app`
httpp py path --include-dir
```

### Running the server

`app.run()` is the launcher: `python app.py` is all it takes. The same app also runs from the shell
with `httpp py run main:app`.

```python
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)     # defaults: 127.0.0.1, 8000
```

```sh
httpp py run main:app --host 0.0.0.0 --port 8000
httpp py run main:app --workers 4             # four processes sharing the port (Linux only)
httpp py run main:app --workers auto          # one per usable CPU
httpp py run main:app --reload                # development: restart when a .py file changes
```

- `run()` stops on **Ctrl+C or SIGTERM**, gracefully: the port closes at once, requests in progress finish (they are answered with
  `Connection: close`), then `on_shutdown` hooks run. `--shutdown-timeout SECONDS` (`run(shutdown_timeout=...)`, default 10) is how long
  that may take before what is still open is cut; a second Ctrl+C cuts at once. `app.shutdown(grace)` / `server::shutdown(grace)` do the same from code.
- **`--reload` is for development.** The app runs in a child process; when a file matching `--reload-pattern` (repeatable, default `*.py`) under
  `--reload-dir` (repeatable, default the current directory) changes, the child is stopped (1 second grace) and a new one starts, so the new code is
  imported fresh. A syntax error leaves the watcher waiting for the next save. It needs an import string and one worker, and it polls the files
  (hidden directories, `__pycache__`, `node_modules` and virtual environments are skipped), so it works the same on every OS.
  `httpp.run(app_or_"module:attr", ...)` is the same function; `listen()` stays as the low-level call.
- **`--workers N` (`workers=N`) is how you get past the GIL.** Python code in a handler needs the GIL
  whatever the server does, so one process runs at most one handler's Python at a time. N processes
  each have their own GIL and share one port (`SO_REUSEPORT`; the kernel spreads connections). Each
  worker is a fresh process that imports the app itself, which is why `workers > 1` needs an import
  string (`"main:app"`), like uvicorn and gunicorn; module-level state is therefore per worker.
  Linux only for now. If a worker dies, the supervisor starts a new one (at once the first time, then
  after 0.5, 1, 2 and 4 seconds while it keeps dying right after it starts); a worker that keeps dying
  that way stops the whole group with exit code 1, so a broken app or a busy port is not hidden. If the
  supervisor itself is killed, even with SIGKILL, the kernel sends SIGTERM to its workers.
- `workers="auto"` / `--workers auto` starts one worker per CPU the process can use: its affinity mask,
  capped by a container CPU quota (rounded down, at least 1). It runs a single process on one CPU, off
  Linux, and when `app` is an object rather than an import string (it says so).
- `reuse_port=True` / `--reuse-port` lets separately started httpp processes share a port the same way.
  It is off by default, because it also lets an unrelated process of the same user bind the port silently.

- It blocks until `stop()` is called or you press **Ctrl+C**.
- It raises `OSError` if the address cannot be bound (for example, the port is already taken).
  Two httpp servers cannot share a port unless both set `reuse_port` (above).
- Plain `def` handlers run on the server's thread pool (see [Settings](#settings)), so a handler that
  blocks holds its thread for as long as it blocks.
- `async def` handlers run on **one shared asyncio event loop** (a background thread, started when the
  server starts listening). The server's I/O thread parses the request, matches the route and queues it
  for the loop right there, without taking the GIL and without a thread-pool hop (a `before_request` hook,
  which may block, still goes through the pool first). It pokes the loop through a socket, and the loop
  thread takes the GIL once per burst of requests, not once per request. Each request is one asyncio task,
  and a handler that does not take `request` never gets one built. While they `await`, no thread is held,
  so far more requests can be in flight than there are threads. Because every request shares the same
  loop, loop-bound objects (an `aiohttp.ClientSession`, an async DB pool) can be created once and reused.
  A blocking call inside an `async def` handler blocks **every** async route, since they all share that
  loop.
- **Speed.** `benchmarks/bench.py` measures requests per second with `oha` against uvicorn (a bare ASGI app, uvloop,
  httptools) and against FastAPI on uvicorn. On two cores, with the server and the load generator sharing them,
  `async def` routes did about 1.1 to 1.4 times the requests per second of bare uvicorn, and 5 to 17 times
  FastAPI, which also validates and routes (hello, a path parameter with a JSON body, the last of 300 routes). Plain `def`
  routes did about 2 times bare uvicorn on both CPython 3.13 and free-threaded 3.14t, where a CPU-bound
  `def` handler also uses both cores (about 2 times from 1 to 8 connections); `async def` handlers share one loop thread
  and do not. Run to run the numbers move by about 10 percent, so only comparisons inside one run mean anything.
  Details, tables and what is not verified: [ADR-0021](docs/adr/0021-pool-thread-state-and-two-core-numbers.md).
- **The server is event-driven** on every platform: all socket I/O (accepting, reading, keep-alive idle
  connections, timeouts, writing) runs on one Asio thread (epoll on Linux, kqueue on macOS, IOCP on
  Windows), and requests are parsed by llhttp. Idle connections cost no worker thread. A worker is used
  only while your handler (or a file/stream body piece) is actually running, never while waiting for the
  network. This also applies to async routes. Keep-alive works on every platform.

### Sessions and async

Requests made from asyncio code run on the library's own non-blocking network thread (Asio + llhttp, with
TLS and proxy support): no thread per request, keep-alive connections are pooled and reused, so thousands
can be in flight.

```python
import asyncio, httpp

async def main():
    # one request
    r = await httpp.Request("https://example.com/api").params({"q": "x"}).run_async()

    # many requests to one server; at most 16 in flight at once, the rest wait their turn
    async with httpp.AsyncClient("https://example.com", max_connections=16, headers={"X-Token": "abc"}) as c:
        replies = await asyncio.gather(*(c.get(f"/items/{i}") for i in range(1000)))

asyncio.run(main())
```

Redirects are off by default (`follow_redirects=True` follows up to 20 hops; `Authorization` and `Cookie`
are not sent to a different origin). A cancelled `await` stops waiting, but the request itself keeps
running until it finishes. In C++: `request::run_async()` returns a `std::future`, or takes a callback
that runs on the network thread; `async_client` is the session form.

### Testing your app

```python
from httpp.testclient import TestClient

def test_items():
    with TestClient(app) as client:                      # startup hooks run here, shutdown hooks when the block ends
        r = client.post("/items", json={"name": "apple"})
        assert r.status_code == 200 and r.json()["name"] == "apple"
        assert client.get("/items/1", params={"q": "x"}).json()["id"] == 1
```

`TestClient(app)` serves the app on a free loopback port of its own and sends the requests through httpp's client, so routing, middleware, validation and gzip are the real ones.
It follows Starlette's interface: `get`, `post`, `put`, `patch`, `delete`, `head`, `options`, `request`; `params`, `headers`, `json`, `data` (a dict is a form), `content`, `cookies`, `auth=("user", "pw")`,
`follow_redirects` (on by default), `base_url` (default `http://testserver`, the `Host` the app sees). Cookies the server sets are kept and sent back (`client.cookies`).
A handler that raises with no exception handler for it is raised again in your test (`raise_server_exceptions=False` gives you the 500 instead). Without `with`, the first request starts the server and `client.close()` stops it.
`files=` sends multipart, and `client.websocket_connect("/ws")` gives a session with `send_text`, `receive_text`, `send_json` and friends (ADR-0046). Not there: a mode without a socket (ADR-0042).

### TLS (https, wss)

```bash
httpp py run main:app --port 8443 --ssl-certfile cert.pem --ssl-keyfile key.pem   # [--ssl-keyfile-password ...]
```

```python
app.set_tls("cert.pem", "key.pem")        # C++: server.set_tls(cert, key)
```

PEM files (certificate chain, leaf first; private key), TLS 1.2 and 1.3, no client certificates, no session resumption. The files are checked when you call it and are
not watched: restart after renewing. One port serves TLS or plain HTTP, never both; a plain request to a TLS port is dropped. `request.secure` / `request.scheme` tell a handler which one it got.
Behind a proxy that terminates TLS, `add_middleware(HTTPSRedirectMiddleware, trust_forwarded_proto=True)` redirects what arrived as plain http.

### Middleware

```python
from httpp import Server, CORSMiddleware, TrustedHostMiddleware, GZipMiddleware

app = Server()
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["example.com", "*.example.com"])
app.add_middleware(CORSMiddleware, allow_origins=["https://app.example.com"], allow_methods=["GET", "POST"],
                   allow_headers=["Authorization"], allow_credentials=True)
app.add_middleware(GZipMiddleware, minimum_size=1000)
```

The options are Starlette's. The work runs in C++ without the GIL: a CORS preflight is answered on the I/O thread and never reaches Python.
`before_request` hooks run after the middleware, `after_request` hooks before it. In C++: `httpp::add_cors(server&, cors_options)`,
`httpp::add_trusted_host(server&, trusted_host_options)` (`httpp/middleware.hpp`), or your own pair of hooks with `server::use(before, after)`.
Not supported: ASGI middleware. `HTTPSRedirectMiddleware` sends plain requests to the https address (see TLS below).

## Build from source

Most dependencies are vendored (`llhttp`, unmodified) or git submodules
(`mbedtls`, `liburlparser`, `asio`). The full history of the `mbedtls` submodule is large; if you only need the source, clone
with `git clone --depth 1 --recurse-submodules --shallow-submodules`.

[Asio](https://think-async.com/Asio/) (header-only, used unmodified) is found in this order:
`-DHTTPP_ASIO_INCLUDE_DIR=<dir containing asio.hpp>`, then a copy in `src/third_party/asio` (for example a
git submodule), then it is downloaded at configure time (about 3 MB, pinned to 1.34.2 by SHA-256). Offline
builds should pass the first option. See [`src/third_party/README.md`](src/third_party/README.md#asio).

```sh
cmake -B build -G Ninja -DHTTPP_BUILD_TESTS=ON && ninja -C build   # the C++ library and its tests
build/tests/cpp/test_httpp_cpp
pip install .                                                       # the Python package
```

## Status

httpp is **alpha**: the API can still change between minor versions. Current limitations:

- Awaitable downloads (`Download.run_async`) still run on the event loop's thread pool; downloads have no
  timeout option; the server's hooks hold a worker thread while they await; a plain `def` handler cannot
  be cancelled by `handler_timeout`. Handler code is Python, so it still needs the GIL: `async def`
  handlers share one loop thread, plain `def` handlers take the GIL on pool threads (set `threads=` to
  fit your blocking calls), and real parallelism needs `--workers` or a free-threaded Python (below).
- HTTP/1.1 only (the server speaks TLS, see the TLS section); a cancelled `await` stops waiting but the request itself
  keeps running until it finishes.
- Platforms: the connection layer is tested on Linux. Windows and macOS are untested by the author; CI
  on those platforms is the real check.
- There is no ASGI or WSGI interface (see above), so ASGI/WSGI applications such as FastAPI or Flask
  cannot be mounted on it yet.
- cpp-httplib is no longer used: the server, the client (blocking and async) and downloads all run on our own
  engines (Asio + llhttp + mbedTLS), see ADR-0051 to ADR-0053.
- The libcurl compatibility layer covers a subset of `curl_easy_*`.
- `--workers` is Linux only.

Issues and pull requests are welcome.

## Logging

The library logs through a private logger of its own (it never touches `std::clog` or your own logging). By default
warnings and errors go to stderr, e.g. a handler that throws with no exception handler installed.

```cpp
httpp::set_log_level(httpp::log_level::debug);       // trace, debug, info, warning (default), error, off
httpp::set_log_file("httpp.log");                    // instead of stderr; "" goes back
httpp::set_log_callback([](httpp::log_level, const std::string& where, const std::string& message) { /* your logger */ });
```

```python
httpp.set_log_level("debug")       # same levels, as strings
httpp.set_log_file("httpp.log")
httpp.set_log_callback(lambda level, where, message: ...)   # called from library threads
```

## License

[MIT](LICENSE.txt)
