Skip to content

Keep String response bodies reusable and refuse to resend a spent streaming body - #1364

Open
quinnj wants to merge 1 commit into
masterfrom
fix/reusable-string-response-bodies
Open

quinnj wants to merge 1 commit into
masterfrom
fix/reusable-string-response-bodies

Conversation

@quinnj

@quinnj quinnj commented Sep 14, 2026

Copy link
Copy Markdown
Member

Fixes #1333.

Problem

A Response built from a String body wrapped it in a cursor-based BytesBody, so the first send consumed it. Returning that same "baked" response a second time wrote a head promising Content-Length bytes and then failed: the client saw a truncated body (serve!), a 500 (streamhandler, whose adapter noticed the closed body), or an empty DATA frame (HTTP/2). A Vector{UInt8} body was stored as-is and never had the problem, which is the asymmetry the issue describes.

Reproduced with the issue's script: with serve!, /string answered once and then failed with truncated fixed-length HTTP/1 body; /bytes worked repeatedly.

Change

  • Response stores String bodies as-is (_response_body_arg(::AbstractString) returns the String), exactly like Vector{UInt8} bodies. Every write path already emits AbstractString bodies zero-copy (HTTP/1 fixed-length and chunked, HTTP/2 fast path, stream adapter, display), so this also drops an allocation per response. The two positional Response(status, ::AbstractString) forms, which copied the string into a BytesBody, now go through the same rule. Requests keep BytesBody, since Request requires an AbstractBody.
  • Genuinely single-use bodies are refused before the head is written: _check_response_body_unsent throws for a BytesBody/CallbackBody that was already sent or closed, or a BytesBody shorter than the declared Content-Length. write_response! and the HTTP/2 server's streaming path call it, and the request-handler server catches it and answers 500 instead of dropping the connection mid-response. Other AbstractBody implementations are left to their own semantics.
  • Server guide gains a "Reusing Responses" note; CHANGELOG entry.

Tests

  • http_core_tests.jl: String bodies are stored by identity (keyword, positional, compat and SubString forms); write_response! of the same response twice produces identical bytes; a spent BytesBody response throws ProtocolError (consumed) / ArgumentError (closed) before writing anything; a closed body with a declared empty payload still sends.

  • http_server_http1_tests.jl: one String-body and one Vector{UInt8}-body response served twice each on both serve! and listen!(streamhandler(...)); a BytesBody response answers once and then 500.

  • http2_server_tests.jl: a String-body response served twice over one HTTP/2 connection.

  • http_handlers_tests.jl: the existing "streamhandler reused String response body" test pinned the old behavior (second request 500); it now asserts the second request succeeds with the same body.

  • http_forms_tests.jl/http_multipart_tests.jl: the multipart helpers accept String (and any AbstractVector{UInt8}) response bodies.

Full Pkg.test on Julia 1.12.6 (macOS) passes: 29 files, Testing HTTP tests passed.

🤖 Generated with Claude Code

@quinnj
quinnj force-pushed the fix/reusable-string-response-bodies branch from d0a2bc8 to ae0d04d Compare September 14, 2026 21:25
…eaming body

A `Response` built from a `String` wrapped it in a cursor-based `BytesBody`,
so the first send consumed it: the second send of the same "baked" response
wrote a head promising `Content-Length` bytes and then failed, leaving the
client with a truncated body (`serve!`), a `500` (`streamhandler`), or an
empty DATA frame (HTTP/2). `Vector{UInt8}` bodies were stored as-is and never
had the problem.

Store `String` bodies as-is too. Every write path already emits `AbstractString`
bodies zero-copy, so this also removes an allocation per response. Requests
keep `BytesBody`, since `Request` requires an `AbstractBody`.

Genuinely single-use bodies (`BytesBody`, `CallbackBody`) that were already
sent or closed now fail before the head is written, on both the HTTP/1 and
HTTP/2 server paths, and the request-handler server answers `500` instead of
dropping the connection mid-response.

Fixes #1333.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@quinnj
quinnj force-pushed the fix/reusable-string-response-bodies branch from ae0d04d to d736f6a Compare September 14, 2026 21:39
@codecov

codecov Bot commented Sep 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.65%. Comparing base (dcf6428) to head (d736f6a).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/http_core.jl 87.50% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1364      +/-   ##
==========================================
+ Coverage   89.51%   89.65%   +0.13%     
==========================================
  Files          31       31              
  Lines       12594    12648      +54     
==========================================
+ Hits        11274    11340      +66     
+ Misses       1320     1308      -12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

Documentation: Baked responses via streamhandler must have Vector{UInt8} bodies

1 participant