mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-17 15:23:44 +08:00
Merge c2a0362c0864e55312f9d2fe8b23d1e01fcbe106 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646
This commit is contained in:
commit
030c90d3ba
@ -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").
|
||||
|
||||
@ -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())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user