Skip to content

Migrate Server.serve to Std.Http.Server (4.31)#1

Open
junjihashimoto wants to merge 1 commit into
mainfrom
feat/std-http-migration
Open

Migrate Server.serve to Std.Http.Server (4.31)#1
junjihashimoto wants to merge 1 commit into
mainfrom
feat/std-http-migration

Conversation

@junjihashimoto

@junjihashimoto junjihashimoto commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Per Zulip review — 4.31 ships a real Std.Http.Server (authors: Sofia Rodrigues, Lean FRO) with cancellation, connection limits, 100-continue handling, and proper async lifecycle. This PR sits LeanTea's server on top of it instead of the hand-rolled HTTP/1.1 accept loop in LeanTea/Net/Server.lean.

The public LeanTea.Net.Server.serve port host handler signature is unchanged so no handler code in the codebase needs to change.

What changed

LeanTea/Net/Server.lean becomes a thin adapter:

  • drainStream — pulls Chunk.data off the stream into a ByteArray (no streaming yet, see Trade-offs).
  • pathAndQueryOf — flattens Std's typed RequestTarget back to (path, query) strings. Strips the leading ? from URI.Query.toString to match our previous parser's shape.
  • headersToPairs — uses Name.value (lowercase) not toString n (canonical PascalCase) so Request.header?'s name.toLower lookup keeps working.
  • statusOfNat — maps our Nat status to Std's Status via Status.ofCode none, defaulting to .ok.
  • LegacyHandler — typeclass wrapper hooking Handler := Request → IO Response into Std.Http.Server.Handler's ContextAsync onRequest. IO → ContextAsync lift comes free via MonadLift in Std.Async.

Test plan

Full spec suite green on this branch:

  • persist_spec — 32 passed
  • security_spec — 82 passed
  • pure_spec — 36 passed
  • auth_spec — 17 passed (round-trip against the in-process IdP, which itself uses Server.serve — so this exercises the new server end-to-end)
  • valkey_smoke — 8 passed
  • s3_smoke — 6 passed
  • webdav_smoke — 11 passed (PROPFIND / MKCOL / PUT / GET / DELETE through the new server)
  • leanjs_spec — 51 passed

Trade-offs

  • Bodies drained up front. The adapter pulls the entire request body into a ByteArray before calling the handler — so we keep our existing Request.body : ByteArray shape. Apps wanting zero-copy streaming can write a handler against Std.Http.Server.Handler directly.
  • Std types not surfaced. We continue to expose LeanTea.Net.Http.{Request, Response} (our own records). Migrating those to type aliases over Std.Http.{Request, Response} is a larger follow-up because the surfaces are different (Std uses Method inductive, Headers map, RequestTarget, Body.Any, etc.).

Why a PR not main

Marking this for review before merging — the new server has different connection lifecycle (graceful shutdown, cancellation, connection limit) that could surface differences vs the old simple accept loop under load.

Per Zulip review: 4.31 ships a real `Std.Http.Server` (authors:
Sofia Rodrigues, Lean FRO) with cancellation, connection limits,
`100-continue` handling, and proper async lifecycle. Sit on top
of it instead of hand-rolling an HTTP/1.1 accept loop.

The public LeanTea API stays:

    LeanTea.Net.Server.serve port host handler

with `handler : Request → IO Response` unchanged. All existing
handlers continue to work — the conversion between our raw record
`Request`/`Response` and Std's typed `Std.Http.Request Body.Stream`
/ `Std.Http.Response Body.Any` happens in a small adapter inside
this module.

Key details of the shim:

  * `drainStream` pulls every `Chunk.data` off the stream and
    concatenates into a `ByteArray` so handlers still see a
    complete body (no streaming yet).
  * `pathAndQueryOf` flattens the typed `RequestTarget` back to a
    `(path, query)` String pair. Strips the leading `?` from Std's
    `URI.Query.toString` to match our previous parser's
    `q="k=v&k=v"` shape.
  * `headersToPairs` reads `Name.value` (lowercase) not
    `toString n` (canonical PascalCase) so `Request.header?` keeps
    working with its existing `name.toLower` comparison.
  * `statusOfNat` maps our `Nat` status to Std's `Status` via
    `Status.ofCode none`, defaulting to `.ok` on unknown codes
    (Std's mapping covers RFC 9110).
  * `LegacyHandler` is the typeclass wrapper that hooks our
    `Handler := Request → IO Response` into
    `Std.Http.Server.Handler`'s `ContextAsync`-monadic
    `onRequest`. The `IO → ContextAsync` lift is provided by
    `MonadLift` in Std.Async.

Trade-offs documented in the module: this shim drains the entire
body up front (no zero-copy streaming yet) and keeps our existing
`Request` shape so app code doesn't change. Future work can opt
into Std's streaming shape by talking to `Std.Http.Server`
directly.

Full spec suite re-runs green:
  persist_spec 32 · security_spec 82 · pure_spec 36 · auth_spec 17
  valkey_smoke 8 · s3_smoke 6 · webdav_smoke 11 · leanjs_spec 51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant