Conversation
quinnj
force-pushed
the
fix/reusable-string-response-bodies
branch
from
September 14, 2026 21:25
d0a2bc8 to
ae0d04d
Compare
…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
force-pushed
the
fix/reusable-string-response-bodies
branch
from
September 14, 2026 21:39
ae0d04d to
d736f6a
Compare
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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.
Fixes #1333.
Problem
A
Responsebuilt from aStringbody wrapped it in a cursor-basedBytesBody, so the first send consumed it. Returning that same "baked" response a second time wrote a head promisingContent-Lengthbytes and then failed: the client saw a truncated body (serve!), a500(streamhandler, whose adapter noticed the closed body), or an empty DATA frame (HTTP/2). AVector{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!,/stringanswered once and then failed withtruncated fixed-length HTTP/1 body;/bytesworked repeatedly.Change
ResponsestoresStringbodies as-is (_response_body_arg(::AbstractString)returns theString), exactly likeVector{UInt8}bodies. Every write path already emitsAbstractStringbodies 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 positionalResponse(status, ::AbstractString)forms, which copied the string into aBytesBody, now go through the same rule. Requests keepBytesBody, sinceRequestrequires anAbstractBody._check_response_body_unsentthrows for aBytesBody/CallbackBodythat was already sent or closed, or aBytesBodyshorter than the declaredContent-Length.write_response!and the HTTP/2 server's streaming path call it, and the request-handler server catches it and answers500instead of dropping the connection mid-response. OtherAbstractBodyimplementations are left to their own semantics.Tests
http_core_tests.jl: String bodies are stored by identity (keyword, positional, compat andSubStringforms);write_response!of the same response twice produces identical bytes; a spentBytesBodyresponse throwsProtocolError(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 oneVector{UInt8}-body response served twice each on bothserve!andlisten!(streamhandler(...)); aBytesBodyresponse answers once and then500.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 request500); it now asserts the second request succeeds with the same body.http_forms_tests.jl/http_multipart_tests.jl: the multipart helpers acceptString(and anyAbstractVector{UInt8}) response bodies.Full
Pkg.teston Julia 1.12.6 (macOS) passes: 29 files,Testing HTTP tests passed.🤖 Generated with Claude Code