Skip to content

26 streaming request body - #27

Merged
EdmondDantes merged 5 commits into
mainfrom
26-streaming-request-body
May 16, 2026
Merged

EdmondDantes merged 5 commits into
mainfrom
26-streaming-request-body

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

No description provided.

Adds opt-in streaming for request bodies, replacing the buffer-the-whole-
body-then-dispatch path that holds Content-Length bytes per concurrent
request. Targets the HttpArena `upload` profile where 1000 concurrent
20 MiB POSTs currently peak at ~20 GiB RSS.

New API
- HttpServerConfig::setBodyStreamingEnabled(bool) — server-wide flag,
  default false. When true, H1 and H2 parsers push DATA chunks into a
  per-request linked-list queue instead of accumulating into req->body.
- HttpRequest::readBody(int $maxLen = 65536): ?string — drains one
  chunk; parks the coroutine on a per-request trigger event when the
  queue is empty; returns null at EOF.

Internals (src/http_body_stream.c, include/http_body_stream.h)
- New http_body_chunk_t linked list owned by http_request_t.
- ZEND_ASYNC_NEW_TRIGGER_EVENT for wakeup — same pattern as the H2
  stream write_event.
- Disposed unconditionally from http_request_destroy.

Integration
- H1 on_headers_complete: flips req->body_streaming when the connection
  view advertises body_streaming_enabled (multipart still bypasses).
- H1 on_body: streaming branch pushes via http_body_stream_push and
  skips both the pre-sized req->body buffer and the chunked smart_str
  accumulator.
- H1 on_message_complete: streaming branch calls
  http_body_stream_close (EOF signal) instead of finalising
  body_builder into req->body.
- H2 cb_on_data_chunk_recv: streaming branch enqueues without
  appending to stream->request_body_buf.
- H2 finalize_request_body: streaming branch fires EOF and exits
  before the smart_str_extract path.

HttpRequest::readBody also falls back to draining req->body once in
non-streaming mode so handlers can use a uniform readBody() loop
without branching on the server config.

Measured impact (h2load -n50 -c50 -d 20MiB upload, release PHP, WSL2):
- buffered:  peak RSS 1170 MiB, 36 req/s
- streaming: peak RSS  197 MiB, 100 req/s  (~6x RSS, ~2.7x RPS)

Tests
- tests/phpt/server/h1/050-stream-request-body.phpt
- tests/phpt/server/h2/050-stream-request-body.phpt

Backpressure (llhttp_pause, deferred nghttp2_session_consume),
HTTP/3, readBodyChunks() and the trichotomous mode lock from the
plan are deferred to follow-up PRs — see docs/PLAN_STREAMING_INGRESS.md.
Pure comment / docstring changes — no behaviour change.

- include/http1/http_parser.h: drop the misleading "naturally throttles"
  hint from the body_streaming block, mark the unused body_h2_*,
  body_h3_stream, body_paused fields and HTTP_BODY_QUEUE_WATERMARK as
  reserved-for-followup with explicit TODO(issue #26 backpressure)
  references.
- src/http2/http2_session.c: rewrite the cb_on_data_chunk_recv
  streaming-branch comment to state correctly that nghttp2's auto
  window update is on by default and the queue grows up to
  max_body_size, and to point at the planned
  NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE follow-up.
- src/http1/http_parser.c on_body: same TODO pointing at llhttp_pause /
  resume.
- src/http_request.c readBody docstring: replace the vague "few KiB"
  H1 chunk estimate with the actual DEFAULT_READ_BUFFER_SIZE = 8 KiB,
  reword $maxLen note as a TODO so the eventual coalesce wiring is
  signalled in-source rather than only in the plan.

173/173 phpt green.
@EdmondDantes EdmondDantes linked an issue May 16, 2026 that may be closed by this pull request
@github-actions

github-actions Bot commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 77.66% → 77.64% (-0.02 pp)

File Baseline Current Δ Touched
src/http1/http_parser.c 81.94% 81.74% -0.20 pp ●
src/http2/http2_session.c 86.14% 86.36% +0.23 pp ●
src/http_body_stream.c 0.00% 83.05% +83.05 pp ●
src/http_request.c 92.73% 86.94% -5.78 pp ●
src/http_server_class.c 75.81% 75.85% +0.04 pp ●
src/http_server_config.c 86.53% 86.72% +0.19 pp ●

❌ Regression in touched files (> 1.0 pp drop)

  • src/http_request.c dropped -5.78 pp

Add [coverage-drop-ok] to a commit message in this PR to override.

The Fuzz embedded CI job linked fuzz_http1_parser / fuzz_h2_session
without the streaming-body translation unit added in be96bac,
producing undefined references to http_body_stream_push / _close /
_dispose. Add the new file to HTTP1_SRC and H2_SRC so both binaries
link again.
CHANGELOG entry covers the new HttpRequest::readBody() and
HttpServerConfig::setBodyStreamingEnabled API plus the measured
upload-RSS and throughput numbers. Buffered handlers are unaffected
(A/B benchmarks show no regression beyond WSL2 noise).

PHP_HTTP_SERVER_VERSION bumped to 0.5.0 — minor version bump because
the new API is additive (no signature changes to existing methods).
@EdmondDantes
EdmondDantes merged commit 21ec058 into main May 16, 2026
5 checks passed
@EdmondDantes
EdmondDantes deleted the 26-streaming-request-body branch May 16, 2026 12:26
EdmondDantes added a commit that referenced this pull request May 16, 2026
…rces on OpenSSL

Three issues prevented a clean Windows MSVC build after the streaming
request body merge (PR #27):

1. src/http_body_stream.c (new in PR #27) was missing from CMakeLists.txt.

2. src/core/http_connection_tls.c and src/core/tls_layer.c unconditionally
   include <openssl/bio.h> / <openssl/err.h> but were in CORE_SUBSYSTEM_SOURCES
   which compiled regardless of OpenSSL availability. Moved them to a
   TLS_SOURCES list that is only populated when find_package(OpenSSL) succeeds.

3. http2_conn_notify_emit (defined in src/http2/http2_strategy.c) was declared
   extern in http_connection.c without any HAVE_HTTP2 guard. Without nghttp2
   the HTTP/2 sources are excluded and the symbol is undefined at link time.
   Added a static inline no-op fallback under #ifndef HAVE_HTTP2.
   Also added full nghttp2 detection (>= 1.57, mirrors config.m4) to
   CMakeLists.txt so the HTTP/2 sources are compiled when nghttp2 is present.
EdmondDantes added a commit that referenced this pull request May 16, 2026
Four issues prevented the unit test suite from building and running on
Windows MSVC after the streaming request body merge (PR #27):

1. tests/CMakeLists.txt included `${PHP_SRC_DIR}/win32` as a system
   search directory, which caused PHP's win32/unistd.h and win32/time.h
   to shadow CRT system headers (same prohibition already in the main
   CMakeLists.txt — the test build was missing the matching guard).

2. tests/unit/CMakeLists.txt HTTP1_PARSER_SRC was missing four sources
   that http_parser.c and multipart_processor.c now depend on:
     - src/http_body_stream.c  (new in PR #27, called by http_parser.c)
     - src/core/body_pool.c    (body slab pool, called by http_parser.c)
     - src/http_rfc5987.c      (called by multipart_processor.c)
     - src/http_param_parse.c  (called by multipart_processor.c)
   The same rfc5987/param_parse pair was also missing from the standalone
   test_multipart_processor target.

3. test_compression_negotiate linked http_compression_negotiate.c but had
   no provider for http_compression_lookup (defined in http_compression.c,
   which in turn requires gzip/brotli/zstd vtables → external libs).
   Added tests/unit/compression/compression_stubs.c with a lightweight
   stub that returns a zero-initialized vtable for all three codecs so
   the selection logic is exercised without pulling in compression backends.

4. All unit test targets except Win32Compat and StaticDecoders were missing
   ENVIRONMENT_MODIFICATION to prepend PHP_DLL_DIR and CMOCKA_DLL_DIR to
   PATH, causing 0xc0000135 (DLL not found) at CTest run time on Windows.
   Added the matching property blocks (mirroring the existing Win32Compat
   pattern) for all remaining tests.

Result: 7/7 tests pass under `ctest -C Debug`.
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.

Streaming request body

1 participant