When gin is used with http.TimeoutHandler or any other middleware that
wraps http.ResponseWriter with a type that does not implement
http.Hijacker or http.CloseNotifier, the direct type assertions in
Hijack() and CloseNotify() cause a runtime panic.
Replace with checked assertions following the same pattern already used
in Flush(). CloseNotify() returns nil when unsupported (http.CloseNotifier
is deprecated since Go 1.11). Hijack() returns a descriptive error,
consistent with the existing errHijackAlreadyWritten pattern.
Fixes#4460
* feat: refine hijack behavior for response lifecycle and add tests
- Clarify the error message for attempted hijack after response body data is written
- Modify hijack behavior: allow hijacking after headers are written (for better websocket compatibility), but block hijacking after any body data is sent
- Add comprehensive tests to validate allowed hijack after header write and disallowed hijack after body write
fix https://github.com/gin-gonic/gin/issues/4372
Signed-off-by: appleboy <appleboy.tw@gmail.com>
* test: use require for immediate test failure on errors
- Replace assert with require for error checks to ensure test failures immediately halt execution
Signed-off-by: appleboy <appleboy.tw@gmail.com>
* Update response_writer.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---------
Signed-off-by: appleboy <appleboy.tw@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>