mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 06:03:48 +03:00
reverseproxy: Fix hijack ordering which broke websockets (#5679)
This commit is contained in:
parent
4aa4f3ac70
commit
e2fc08bd34
1 changed files with 12 additions and 11 deletions
|
@ -58,6 +58,13 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, rw http.ResponseWrit
|
|||
return
|
||||
}
|
||||
|
||||
// write header first, response headers should not be counted in size
|
||||
// like the rest of handler chain.
|
||||
copyHeader(rw.Header(), res.Header)
|
||||
rw.WriteHeader(res.StatusCode)
|
||||
|
||||
logger.Debug("upgrading connection")
|
||||
|
||||
//nolint:bodyclose
|
||||
conn, brw, hijackErr := http.NewResponseController(rw).Hijack()
|
||||
if errors.Is(hijackErr, http.ErrNotSupported) {
|
||||
|
@ -65,6 +72,11 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, rw http.ResponseWrit
|
|||
return
|
||||
}
|
||||
|
||||
if hijackErr != nil {
|
||||
h.logger.Error("hijack failed on protocol switch", zap.Error(hijackErr))
|
||||
return
|
||||
}
|
||||
|
||||
// adopted from https://github.com/golang/go/commit/8bcf2834afdf6a1f7937390903a41518715ef6f5
|
||||
backConnCloseCh := make(chan struct{})
|
||||
go func() {
|
||||
|
@ -78,17 +90,6 @@ func (h *Handler) handleUpgradeResponse(logger *zap.Logger, rw http.ResponseWrit
|
|||
}()
|
||||
defer close(backConnCloseCh)
|
||||
|
||||
// write header first, response headers should not be counted in size
|
||||
// like the rest of handler chain.
|
||||
copyHeader(rw.Header(), res.Header)
|
||||
rw.WriteHeader(res.StatusCode)
|
||||
|
||||
logger.Debug("upgrading connection")
|
||||
if hijackErr != nil {
|
||||
h.logger.Error("hijack failed on protocol switch", zap.Error(hijackErr))
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
defer func() {
|
||||
conn.Close()
|
||||
|
|
Loading…
Reference in a new issue