thesis / http-server
Asynchronous HTTP/1 server for TCP and Unix sockets with streaming bodies, trailers, TLS and graceful shutdown.
Fund package maintenance!
Requires
- php: ^8.6
- thesis/async: ^0.1
- thesis/http: ^0.1
- thesis/http-protocol: ^0.1
- thesis/io: ^0.1
- thesis/net: ^0.1
- thesis/time: ^0.4
Requires (Dev)
- ext-openssl: *
- testo/assert: ^0.1.17
- testo/codecov: ^0.2.5
- testo/test: ^0.1.8
- testo/testo: ^0.10.29
- thesis/testo-plugin: ^0.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-23 23:14:51 UTC
README
Thesis HTTP Server
This read-only repository mirrors
packages/http-server. Open issues and pull requests in the thesis-php/thesis monorepo.
Serve HTTP/1 requests on a TCP or Unix listener. Each connection runs in its own task.
Requests on the same connection run in order. The handler receives the shared Thesis\Http\Request
and a Thesis\HttpServer\Context with connection information. It returns a Thesis\Http\Response.
Streaming echo
use Thesis\Async\Group; use Thesis\Http\Request; use Thesis\Http\Response; use Thesis\HttpServer\Config; use Thesis\HttpServer\Server; use function Thesis\Async\awaitSignal; use function Thesis\Async\group; $server = new Server( config: new Config(8080), handler: static fn(Request $request) => new Response( body: $request->body, ), ); group(static function (Group $group) use ($server) { $server->run($group); awaitSignal(SIGINT, SIGTERM); $server->close(); });
The response reads from the incoming body as it is written. A chunked request's trailers become available after its terminating chunk. The callback then supplies them for the last chunk of the response.
From the Thesis monorepo, run the example in Docker.
docker compose run --rm -p 127.0.0.1:8080:8080 php php packages/http-server/examples/echo.php
In another terminal, send two requests with one curl process. The first uses chunked transfer encoding. Curl prints the number of new connections it opened for the second request; expect zero.
curl --http1.1 -sS -H 'Transfer-Encoding: chunked' --data-binary 'one' http://127.0.0.1:8080/first \ --next -sS --data-binary 'two' -w '\nnew connections: %{num_connects}\n' http://127.0.0.1:8080/second
Connection lifetime
The server constructor takes a Config and the request handler and does no I/O.
Pass an integer port to listen on 127.0.0.1. With new Config(0), the OS chooses the port.
To specify the address, pass a string such as '0.0.0.0:8080' or '[::1]:8080', or pass an InetAddress.
For Unix, pass an absolute socket path such as '/run/app.sock'. Use UnixEndpoint for a relative path.
Both endpoint types let you set backlog, which defaults to 511. TcpEndpoint also takes reusePort and noDelay.
$server->run($group) binds the endpoint and returns once the server is ready. A bind failure throws
Thesis\Net\ListenFailed from run(). With TCP port zero, $server->address gives the assigned port. Call $server->close() to stop accepting clients.
The group waits for shutdown to finish. The server closes idle sockets and lets active handlers
and response writers finish until the shutdown deadline. Cancelling the serving scope also stops the server.
The example uses ext-pcntl and handles SIGINT and SIGTERM by closing the listener.
The constructor accepts an optional onError callback for connection and application failures. If the handler throws
before the final response starts, the client receives an empty 500. Once a final response has started,
a failure closes the connection. Each connection runs independently of the others.
When the handler leaves the request body unread, the server finishes writing its response and closes the connection. An echo or another streaming response may still read the request body after the handler returns. A fixed-length request is complete when its final payload byte is read. A chunked request is complete when the decoder has consumed the terminating chunk and all its trailers.
Limits
Pass limits and timeouts as named arguments of Config, for example new Config($endpoint, maxConnections: 256).
Sizes count bytes. Durations use Time\Duration, for example Thesis\Time\sec(5).
| Option | Default | Applies to |
|---|---|---|
maxConnections |
1024 | Accepted connections, including idle ones |
maxRequestsPerConnection |
null (unlimited) |
Set a positive integer to limit requests per connection |
maxHeadBytes |
64 KiB | The request line and fields, including the terminating empty line |
maxBodyBytes |
16 MiB | Payload after decoding HTTP/1 framing |
headTimeout |
10 s | Receipt of the complete head |
idleTimeout |
30 s | Waiting for the next request on a persistent connection |
bodyIdleTimeout |
30 s | A transport read while receiving the request body |
writeTimeout |
30 s | A transport write |
tlsHandshakeTimeout |
10 s | Negotiation when a TLS configuration is supplied |
shutdownTimeout |
5 s | Finishing active exchanges after shutdown starts |
TLS and request expectations
Pass a Thesis\Net\ServerTls configuration through the tls argument of Config to enable HTTPS.
The TLS handshake runs in the task for the accepted socket. The handler can inspect the negotiated details
through $context->tls. Use http/1.1 when configuring ALPN for this server.
For HTTP/1.1 uploads with Expect: 100-continue, a handler can reject a request from its headers or read
its body. A body read sends the interim response if the client is waiting. A successful streaming response
can also consume the request body later. The server sends Continue before that response starts. A known length above the
configured limit gets 413 before the server accepts the upload. An unsupported expectation gets 417.
This package implements HTTP message exchanges. Protocol switching, CONNECT tunnels and HTTP/2 are outside this proof of concept.