summaryrefslogtreecommitdiff
path: root/modules/caddyhttp
diff options
context:
space:
mode:
authorWeidiDeng <weidi_deng@icloud.com>2023-08-03 12:08:12 +0800
committerGitHub <noreply@github.com>2023-08-03 04:08:12 +0000
commite2fc08bd34cc17b8cbb6ac364fa1ec41c4c643b9 (patch)
tree8b2464700b71779759ee66bdac5680598b1c917c /modules/caddyhttp
parent4aa4f3ac70c682fb25db1283ea8516598528995a (diff)
reverseproxy: Fix hijack ordering which broke websockets (#5679)
Diffstat (limited to 'modules/caddyhttp')
-rw-r--r--modules/caddyhttp/reverseproxy/streaming.go23
1 files changed, 12 insertions, 11 deletions
diff --git a/modules/caddyhttp/reverseproxy/streaming.go b/modules/caddyhttp/reverseproxy/streaming.go
index 3f2489d..71b9386 100644
--- a/modules/caddyhttp/reverseproxy/streaming.go
+++ b/modules/caddyhttp/reverseproxy/streaming.go
@@ -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()