fix: isByteResponse returns empty body in BuildResponse after #232#246
Open
glowww wants to merge 1 commit into
Open
fix: isByteResponse returns empty body in BuildResponse after #232#246glowww wants to merge 1 commit into
glowww wants to merge 1 commit into
Conversation
bogdanfinn#232 (commit d0fc595, "fix EOF errors caused by the charset reader by previewing the response body whether at least one byte exists") added a first-byte preview + io.MultiReader + io.ReadAll to BuildResponse to guard against charset.NewReader raising EOF on zero-length text bodies. The entire block, including io.ReadAll, was placed inside the !isByteResponse branch. As a result, when isByteResponse=true the body is never read: respBodyBytes stays nil, DetectContentType(nil) returns "text/plain; charset=utf-8", base64-of-nil is empty, and the returned data URI becomes "data:text/plain; charset=utf-8;base64," with no payload. Fix: hoist the peek + io.ReadAll out so they run for both modes, and keep charset.NewReader inside !isByteResponse since charset detection is only meaningful for text responses. The empty-body EOF guard from bogdanfinn#232 is preserved (n == 0 still short-circuits to respBodyBytes = nil before anything else runs). Adds cffi_src/factory_test.go with four cases covering: - isByteResponse=true + non-empty binary body (regression guard; fails on master with "byte-response payload is empty; body = \"data:text/plain; charset=utf-8;base64,\"") - isByteResponse=true + empty body - isByteResponse=false + empty text body (the original bogdanfinn#232 case) - isByteResponse=false + non-empty text body
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.
Bug
BuildResponsenever reads the response body whenisByteResponse=true, producing an empty payload wrapped in atext/plaindata URI regardless of the actual response content.Repro:
Actual:
data:text/plain; charset=utf-8;base64,Expected:
data:application/octet-stream;base64,AAECA/8=Root cause
#232 (commit
d0fc595, "fix EOF errors caused by the charset reader by previewing the response body whether at least one byte exists") added a first-byte preview +io.MultiReader+io.ReadAllto guard againstcharset.NewReaderraising EOF on zero-length text bodies. The block — including theio.ReadAll— was placed insideif !isByteResponse:So when
isByteResponse=true, that entire block is skipped.respBodyBytesis never populated; the body is never read. Downstream:which is exactly the string I see in the repro above.
I think the intent of #232 was to gate charset detection behind
!isByteResponse(because charset sniffing is only meaningful for text responses), but the patch ended up gating the whole read behind that branch.Fix
Hoist the peek +
io.ReadAllout of the conditional so both paths read the body. Keepcharset.NewReaderinside!isByteResponse. The empty-body guard from #232 is preserved via then == 0short-circuit, which runs before either path.Tests
Added
cffi_src/factory_test.gowith four cases:isByteResponse=true+ non-empty binary bodybyte-response payload is empty)isByteResponse=true+ empty bodyisByteResponse=false+ empty text body (the #232 case)isByteResponse=false+ non-empty text bodyThe first one is the regression guard — it fails on current master with the exact symptom described above.
Scope
One function in
cffi_src/factory.goplus a newcffi_src/factory_test.go. No behavior change for non-byte callers; no change to theStreamOutputPathpath beyond running in the normalized block. Pre-existingBuildResponsecallers incffi_dist/main.gounaffected.