fix: use safe type assertions in Hijack and CloseNotify to prevent pa…#4628
fix: use safe type assertions in Hijack and CloseNotify to prevent pa…#4628odlev wants to merge 1 commit intogin-gonic:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4628 +/- ##
==========================================
- Coverage 99.21% 98.37% -0.84%
==========================================
Files 42 48 +6
Lines 3182 3142 -40
==========================================
- Hits 3157 3091 -66
- Misses 17 42 +25
- Partials 8 9 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
For |
|
Nice point). Returning nil is ambiguous and problematic in range patterns. Changed to return [make(chan bool)] - a channel that is never closed and never fires. This makes the "not supported" semantic explicit: callers in a select will simply never receive, same as nil but without the edge cases. |
|
Fair point — a channel that never fires does make the "not supported" semantic explicit. Though for callers who range over this channel, an always-blocking channel could stall their loop indefinitely. Worth adding a comment clarifying the contract so consumers don't misuse it as a "done" signal. |
|
Small suggestion to close @afurm's ambiguity point without further code churn: surface the "never fires when not supported" semantics directly in the godoc. Something like: // CloseNotify implements the http.CloseNotifier interface.
//
// If the underlying ResponseWriter doesn't implement http.CloseNotifier
// (e.g. httptest.NewRecorder), the returned channel will never fire.
// Use Request.Context().Done() to observe client disconnects instead.
//
// Deprecated: the CloseNotifier interface predates Go's context package.
// New code should use Request.Context instead.Makes the contract self-documenting for anyone who lands on this via IDE hover, and keeps the impl unchanged. |
017f5d9 to
a2792d8
Compare
|
added detailed comment |
|
这是一封自动回复邮件。已经收到您的来信,我会尽快回复。
|
Pull Request Checklist
Please ensure your pull request meets the following requirements:
masterbranch.docs/doc.md.What
responseWriter.Hijack()andresponseWriter.CloseNotify()use unsafe type assertions on the underlyinghttp.ResponseWriter, which causes panics when the writer doesn't implementhttp.Hijackerorhttp.CloseNotifier— for example when usinghttptest.NewRecorder()throughgin.CreateTestContext().Why
Flush()andPusher()already use the safevalue, ok := iface.(Type)pattern.Hijack()andCloseNotify()are the only methods that still do bare type assertions, which is inconsistent and crashes in test environments.Changes
Hijack(): returns a descriptive error (errHijackNotSupported) instead of panicking when the underlying writer doesn't implementhttp.HijackerCloseNotify(): returnsnilchannel instead of panicking when the underlying writer doesn't implementhttp.CloseNotifierTestResponseWriterHijackto expect error return instead of panicTestHijackWithoutHijacker,TestCloseNotifyWithCloseNotifier,TestCloseNotifyWithoutCloseNotifierHow to verify
Fixes #2970
p.s.
codecov/project failure is a stale base comparison issue (275 commits behind), patch coverage is 100%.