From c7a6bc59345ca4075cc8bf97bd80900d28d8e05d Mon Sep 17 00:00:00 2001
From: WeidiDeng <weidi_deng@icloud.com>
Date: Sun, 18 Sep 2022 08:47:53 +0800
Subject: [PATCH] caddyhttp: responseRecorder save status in all cases (#5049)

---
 modules/caddyhttp/responsewriter.go | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/modules/caddyhttp/responsewriter.go b/modules/caddyhttp/responsewriter.go
index 4e2c8f00f..d29f6a762 100644
--- a/modules/caddyhttp/responsewriter.go
+++ b/modules/caddyhttp/responsewriter.go
@@ -170,9 +170,11 @@ func (rr *responseRecorder) WriteHeader(statusCode int) {
 		return
 	}
 
+	// save statusCode in case http middleware upgrading websocket
+	// connections by manually setting headers and writing status 101
+	rr.statusCode = statusCode
 	// 1xx responses aren't final; just informational
 	if statusCode < 100 || statusCode > 199 {
-		rr.statusCode = statusCode
 		rr.wroteHeader = true
 
 		// decide whether we should buffer the response