From c2a0362c0864e55312f9d2fe8b23d1e01fcbe106 Mon Sep 17 00:00:00 2001 From: Raju Ahmed Date: Mon, 2 Nov 2026 23:51:00 +0600 Subject: [PATCH] fix(context): improve websocket header detection in IsWebsocket --- context.go | 9 ++++----- context_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/context.go b/context.go index 582d00f2..36aad278 100644 --- a/context.go +++ b/context.go @@ -25,6 +25,7 @@ import ( "github.com/gin-contrib/sse" "github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/render" + "golang.org/x/net/http/httpguts" ) // Content-Type MIME of the most common data formats. @@ -1062,11 +1063,9 @@ func (c *Context) ContentType() string { // IsWebsocket returns true if the request headers indicate that a websocket // handshake is being initiated by the client. func (c *Context) IsWebsocket() bool { - if strings.Contains(strings.ToLower(c.requestHeader("Connection")), "upgrade") && - strings.EqualFold(c.requestHeader("Upgrade"), "websocket") { - return true - } - return false + header := c.Request.Header + return httpguts.HeaderValuesContainsToken(header["Connection"], "upgrade") && + httpguts.HeaderValuesContainsToken(header["Upgrade"], "websocket") } // Scheme returns the HTTP scheme of the request ("http" or "https"). diff --git a/context_test.go b/context_test.go index b82e04d5..f2f9ceaa 100644 --- a/context_test.go +++ b/context_test.go @@ -3143,6 +3143,35 @@ func TestWebsocketsRequired(t *testing.T) { assert.False(t, c.IsWebsocket()) } +func TestContextIsWebsocketHeaders(t *testing.T) { + tests := []struct { + name string + header http.Header + want bool + }{ + {"standard handshake", http.Header{"Connection": {"Upgrade"}, "Upgrade": {"websocket"}}, true}, + {"mixed case values", http.Header{"Connection": {"UPGRADE"}, "Upgrade": {"WebSocket"}}, true}, + {"connection token list", http.Header{"Connection": {"keep-alive, Upgrade"}, "Upgrade": {"websocket"}}, true}, + {"connection on separate lines", http.Header{"Connection": {"keep-alive", "Upgrade"}, "Upgrade": {"websocket"}}, true}, + {"upgrade token list", http.Header{"Connection": {"Upgrade"}, "Upgrade": {"websocket, foo"}}, true}, + {"connection token is only a substring", http.Header{"Connection": {"not-upgrade"}, "Upgrade": {"websocket"}}, false}, + {"missing upgrade header", http.Header{"Connection": {"Upgrade"}}, false}, + {"missing connection header", http.Header{"Upgrade": {"websocket"}}, false}, + {"upgrade to another protocol", http.Header{"Connection": {"Upgrade"}, "Upgrade": {"h2c"}}, false}, + {"no headers", http.Header{}, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest(http.MethodGet, "/chat", nil) + c.Request.Header = tt.header + + assert.Equal(t, tt.want, c.IsWebsocket()) + }) + } +} + func TestContextScheme(t *testing.T) { // TLS connection takes highest priority. c, _ := CreateTestContext(httptest.NewRecorder())