Honor Discard() in httpFancyWriter.ReadFrom#1110
Merged
Merged
Conversation
eleboucher
pushed a commit
to eleboucher/apoci
that referenced
this pull request
Jul 6, 2026
…#132) This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [github.com/go-chi/chi/v5](https://github.com/go-chi/chi) | `v5.3.0` → `v5.3.1` |  |  | --- ### Release Notes <details> <summary>go-chi/chi (github.com/go-chi/chi/v5)</summary> ### [`v5.3.1`](https://github.com/go-chi/chi/releases/tag/v5.3.1) [Compare Source](go-chi/chi@v5.3.0...v5.3.1) #### What's Changed - Honor Discard() in httpFancyWriter.ReadFrom by [@​DucMinhNe](https://github.com/DucMinhNe) in [#​1110](go-chi/chi#1110) - Tidy build directives by [@​JRaspass](https://github.com/JRaspass) in [#​1113](go-chi/chi#1113) - feat(middleware): add text/xml and application/xml to default compressible types by [@​VojtechVitek](https://github.com/VojtechVitek) in [#​1127](go-chi/chi#1127) - Fix defaultLogEntry.Panic not respecting NoColor setting by [@​doganarif](https://github.com/doganarif) in [#​1050](go-chi/chi#1050) - middleware: document printPrettyStack and harden NoColor panic test by [@​VojtechVitek](https://github.com/VojtechVitek) in [#​1131](go-chi/chi#1131) - feat(mux): support http QUERY method ietf rfc10008 by [@​VojtechVitek](https://github.com/VojtechVitek) in [#​1132](go-chi/chi#1132) - ci: pin GitHub Actions to full commit SHAs by [@​XananasX7](https://github.com/XananasX7) in [#​1116](go-chi/chi#1116) #### New Contributors - [@​DucMinhNe](https://github.com/DucMinhNe) made their first contribution in [#​1110](go-chi/chi#1110) - [@​doganarif](https://github.com/doganarif) made their first contribution in [#​1050](go-chi/chi#1050) - [@​XananasX7](https://github.com/XananasX7) made their first contribution in [#​1116](go-chi/chi#1116) **Full Changelog**: <go-chi/chi@v5.3.0...v5.3.1> </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xMDEuMSIsInVwZGF0ZWRJblZlciI6IjQzLjEwMS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJ0eXBlL3BhdGNoIl19--> Reviewed-on: https://git.erwanleboucher.dev/eleboucher/apoci/pulls/132
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.
httpFancyWriter.ReadFromonly routes throughbasicWriter.Writewhen a tee writer is set. When it isn't, it streams straight to the originalResponseWriter'sio.ReaderFrom— bypassing thediscardflag entirely.So after a caller invokes
Discard()(to suppress the proxied output and write the response itself later), any body sent viaReadFrom/io.Copy— e.g.http.ServeContent,io.Copy(w, file),http.ServeFile— still reaches the client, defeating the purpose ofDiscard(). The plainWritepath already respects discard; only theReadFromfast path missed it.Fix
Extend the slow-path condition to also cover discard, so
basicWriter.Write(which honors both tee and discard) handles those cases. This mirrors the existing tee handling in the same function:The fast
rf.ReadFrompath is now taken only for pure pass-through (no tee, no discard), where it's safe.Test
Added
TestHttpFancyWriterReadFromHonorsDiscard, which writes viaReadFromafterDiscard()and asserts the original writer received nothing whileBytesWritten()still reflects the consumed bytes. It fails onmaster(original receives all 11 bytes) and passes with the fix. Fullgo test ./middleware/passes;gofmt/go vetclean.