Add a chunked transfer-encoding encoder (TransferEncoding.chunk)#17
Merged
Conversation
TransferEncoding could decode chunked bodies (dechunk) but not produce them. Add chunk-with-size (split into size-bounded chunks) and chunk (single chunk), both closed by the terminating zero-size chunk per RFC 7230 §4.1. 9 new tests, property-checked as round-trips against dechunk (incl. the hex-digit boundary and mid-codepoint UTF-8 splits).
There was a problem hiding this comment.
Build & Tests
Checked out claude/chunked-encoder at 6e93a19:
carp -x test/http.carp— 151 pass / 1 fail (the same pre-existingForm.parse ""bug; all 9 new encoder tests pass).angler,carp-fmt --check,gendocs— clean. Matches the green CI.
Findings
The round-trip-against-dechunk tests are a good oracle. I added the one case they didn't cover — a payload that itself contains framing bytes — since that's exactly where a length-prefixed codec tends to break:
chunk "a\r\nb",chunk "0\r\n\r\n"(payload equal to the terminator pattern),chunk "5\r\nhello\r\n", and CRLF-containing bodies split at sizes 1 and 2 all round-trip cleanly. ✓ The framing is genuinely length-prefixed, so embedded control bytes are safe.chunk-with-size(http.carp:611) clampssizeto ≥ 1, capsendat the body length, emits lowercase hex size lines (verified across theff→100boundary), closes with the zero-size chunk, and frees theStringBuf. Empty body → just0\r\n\r\n.- Style matches the existing
dechunk; the module doc was updated to mention encoding. Purely additive.
Verdict: merge
Correct, robust to framing bytes in the payload, well-tested, all gates green.
hellerve
approved these changes
Jul 14, 2026
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.
TransferEncodingcould decode a chunked body (dechunk) but had no way to produce one — the codec was one-directional. This adds the inverse.New functions
chunk-with-size body size— splitsbodyinto chunks of at mostsizebytes, each written as a hex size line, the data, and a CRLF, then closed by the terminating zero-size chunk (RFC 7230 §4.1).sizeis clamped to at least 1 so a bad size can't spin.chunk body— convenience: the whole body as a single chunk plus terminator. An empty body yields just0\r\n\r\n.Both are pure Carp (
StringBuf+fmt "%x"for the lowercase hex size lines) and mirror the existingdechunkin style.Verification
dechunkis the natural oracle, so the tests are round-tripsdechunk(chunk(x)) == x:chunk "hello"→5\r\nhello\r\n0\r\n\r\n,chunk ""→0\r\n\r\n,chunk-with-size "hello" 2framing);ff→100);Full suite 151 passed / 1 failed locally; the 1 failure is the pre-existing
Form.parse ""empty-body bug (unrelated, untouched — masked on CI bycontinue-on-error).carp-fmt -cclean,anglerclean,gendocsbuilds. Purely additive.