Skip to content

Commit

Permalink
Merge pull request #33 from lxzan/testing
Browse files Browse the repository at this point in the history
v1.6.3: adapt to firefox
  • Loading branch information
lxzan authored Jul 23, 2023
2 parents b5c74d3 + 7c8cd8f commit 0facb00
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func (c *connector) checkHeaders() error {
if c.resp.StatusCode != http.StatusSwitchingProtocols {
return internal.ErrStatusCode
}
if !internal.HttpHeaderEqual(c.resp.Header.Get(internal.Connection.Key), internal.Connection.Val) {
if !internal.HttpHeaderContains(c.resp.Header.Get(internal.Connection.Key), internal.Connection.Val) {
return internal.ErrHandshake
}
if !internal.HttpHeaderEqual(c.resp.Header.Get(internal.Upgrade.Key), internal.Upgrade.Val) {
if !strings.EqualFold(c.resp.Header.Get(internal.Upgrade.Key), internal.Upgrade.Val) {
return internal.ErrHandshake
}
if c.resp.Header.Get(internal.SecWebSocketAccept.Key) != internal.ComputeAcceptKey(c.secWebsocketKey) {
Expand Down
4 changes: 4 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func HttpHeaderEqual(a, b string) bool {
return strings.ToLower(a) == strings.ToLower(b)
}

func HttpHeaderContains(a, b string) bool {
return strings.Contains(strings.ToLower(a), strings.ToLower(b))
}

func SelectValue[T any](ok bool, a, b T) T {
if ok {
return a
Expand Down
5 changes: 5 additions & 0 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ func TestHttpHeaderEqual(t *testing.T) {
assert.Equal(t, false, HttpHeaderEqual("WebSocket@", "websocket"))
}

func TestHttpHeaderContains(t *testing.T) {
assert.Equal(t, true, HttpHeaderContains("WebSocket", "websocket"))
assert.Equal(t, true, HttpHeaderContains("WebSocket@", "websocket"))
}

func TestSelectInt(t *testing.T) {
assert.Equal(t, 1, SelectValue(true, 1, 2))
assert.Equal(t, 2, SelectValue(false, 1, 2))
Expand Down
6 changes: 3 additions & 3 deletions updrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func (c *Upgrader) doUpgrade(r *http.Request, netConn net.Conn, br *bufio.Reader
if r.Method != http.MethodGet {
return nil, internal.ErrGetMethodRequired
}
if !internal.HttpHeaderEqual(r.Header.Get(internal.SecWebSocketVersion.Key), internal.SecWebSocketVersion.Val) {
if !strings.EqualFold(r.Header.Get(internal.SecWebSocketVersion.Key), internal.SecWebSocketVersion.Val) {
msg := "websocket version not supported"
return nil, errors.New(msg)
}
if !internal.HttpHeaderEqual(r.Header.Get(internal.Connection.Key), internal.Connection.Val) {
if !internal.HttpHeaderContains(r.Header.Get(internal.Connection.Key), internal.Connection.Val) {
return nil, internal.ErrHandshake
}
if !internal.HttpHeaderEqual(r.Header.Get(internal.Upgrade.Key), internal.Upgrade.Val) {
if !strings.EqualFold(r.Header.Get(internal.Upgrade.Key), internal.Upgrade.Val) {
return nil, internal.ErrHandshake
}
if val := r.Header.Get(internal.SecWebSocketExtensions.Key); strings.Contains(val, "permessage-deflate") && c.option.CompressEnabled {
Expand Down

0 comments on commit 0facb00

Please sign in to comment.