Migrate Server.serve to Std.Http.Server (4.31)#1
Open
junjihashimoto wants to merge 1 commit into
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Per Zulip review — 4.31 ships a real
Std.Http.Server(authors: Sofia Rodrigues, Lean FRO) with cancellation, connection limits,100-continuehandling, and proper async lifecycle. This PR sits LeanTea's server on top of it instead of the hand-rolled HTTP/1.1 accept loop inLeanTea/Net/Server.lean.The public
LeanTea.Net.Server.serve port host handlersignature is unchanged so no handler code in the codebase needs to change.What changed
LeanTea/Net/Server.leanbecomes a thin adapter:drainStream— pullsChunk.dataoff the stream into aByteArray(no streaming yet, see Trade-offs).pathAndQueryOf— flattens Std's typedRequestTargetback to(path, query)strings. Strips the leading?fromURI.Query.toStringto match our previous parser's shape.headersToPairs— usesName.value(lowercase) nottoString n(canonical PascalCase) soRequest.header?'sname.toLowerlookup keeps working.statusOfNat— maps ourNatstatus to Std'sStatusviaStatus.ofCode none, defaulting to.ok.LegacyHandler— typeclass wrapper hookingHandler := Request → IO ResponseintoStd.Http.Server.Handler'sContextAsynconRequest.IO → ContextAsynclift comes free viaMonadLiftinStd.Async.Test plan
Full spec suite green on this branch:
persist_spec— 32 passedsecurity_spec— 82 passedpure_spec— 36 passedauth_spec— 17 passed (round-trip against the in-process IdP, which itself usesServer.serve— so this exercises the new server end-to-end)valkey_smoke— 8 passeds3_smoke— 6 passedwebdav_smoke— 11 passed (PROPFIND / MKCOL / PUT / GET / DELETE through the new server)leanjs_spec— 51 passedTrade-offs
ByteArraybefore calling the handler — so we keep our existingRequest.body : ByteArrayshape. Apps wanting zero-copy streaming can write a handler againstStd.Http.Server.Handlerdirectly.LeanTea.Net.Http.{Request, Response}(our own records). Migrating those to type aliases overStd.Http.{Request, Response}is a larger follow-up because the surfaces are different (Std usesMethodinductive,Headersmap,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.