fix: websocket server upgrade in golang1.20

This commit is contained in:
wwqgtxx 2025-04-16 08:47:44 +08:00
parent 3d2cb992fa
commit dcb20e2824
3 changed files with 13 additions and 1 deletions

View file

@ -555,7 +555,7 @@ func StreamUpgradedWebsocketConn(w http.ResponseWriter, r *http.Request) (net.Co
w.Header().Set("Sec-Websocket-Accept", getSecAccept(r.Header.Get("Sec-WebSocket-Key")))
}
w.WriteHeader(http.StatusSwitchingProtocols)
if flusher, isFlusher := w.(interface{ FlushError() error }); isFlusher {
if flusher, isFlusher := w.(interface{ FlushError() error }); isFlusher && writeHeaderShouldFlush {
err = flusher.FlushError()
if err != nil {
return nil, fmt.Errorf("flush response: %w", err)

View file

@ -0,0 +1,7 @@
//go:build !go1.21
package vmess
// Golang1.20's net.http Flush will mistakenly call w.WriteHeader(StatusOK) internally after w.WriteHeader(http.StatusSwitchingProtocols)
// https://github.com/golang/go/issues/59564
const writeHeaderShouldFlush = false

View file

@ -0,0 +1,5 @@
//go:build go1.21
package vmess
const writeHeaderShouldFlush = true