Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use gobwas/ws instead of gorilla/websocket #799

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module knative.dev/networking
go 1.18

require (
github.com/gobwas/httphead v0.1.0
github.com/gobwas/ws v1.2.0
github.com/gogo/protobuf v1.3.2
github.com/google/go-cmp v0.5.8
github.com/gorilla/websocket v1.4.2
github.com/hashicorp/golang-lru v0.5.4
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
go.uber.org/atomic v1.9.0
Expand All @@ -18,8 +19,8 @@ require (
k8s.io/client-go v0.25.4
k8s.io/code-generator v0.25.4
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2
knative.dev/hack v0.0.0-20230417170854-f591fea109b3
knative.dev/pkg v0.0.0-20230420071539-300df436f953
knative.dev/hack v0.0.0-20230501013555-7d81248b4638
knative.dev/pkg v0.0.0-20230501013355-904966742b58
sigs.k8s.io/yaml v1.3.0
)

Expand All @@ -43,6 +44,7 @@ require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
Expand Down Expand Up @@ -71,7 +73,7 @@ require (
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/tools v0.1.12 // indirect
Expand Down
20 changes: 12 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.2.0 h1:u0p9s3xLYpZCA1z5JgCkMeB34CKCMMQbM+G8Ii7YD0I=
github.com/gobwas/ws v1.2.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
Expand Down Expand Up @@ -238,8 +244,6 @@ github.com/googleapis/gax-go/v2 v2.1.1/go.mod h1:hddJymUZASv3XPyGkUpKj8pPO47Rmb0
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4GkDEdKCRJduHpTxT3/jcw=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
Expand Down Expand Up @@ -592,8 +596,8 @@ golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down Expand Up @@ -873,10 +877,10 @@ k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 h1:MQ8BAZPZlWk3S9K4a9NCkI
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1/go.mod h1:C/N6wCaBHeBHkHUesQOQy2/MZqGgMAFPqGsGQLdbZBU=
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2 h1:GfD9OzL11kvZN5iArC6oTS7RTj7oJOIfnislxYlqTj8=
k8s.io/utils v0.0.0-20221108210102-8e77b1f39fe2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
knative.dev/hack v0.0.0-20230417170854-f591fea109b3 h1:+W4WBOq83tfGXKhtv8OB/uJeYqze3zh69GKiz1ucuqk=
knative.dev/hack v0.0.0-20230417170854-f591fea109b3/go.mod h1:yk2OjGDsbEnQjfxdm0/HJKS2WqTLEFg/N6nUs6Rqx3Q=
knative.dev/pkg v0.0.0-20230420071539-300df436f953 h1:GAB1JB35FWv5zqypwzmO7v4EPY8xXsomPtHEtb8xdx4=
knative.dev/pkg v0.0.0-20230420071539-300df436f953/go.mod h1:2qWPP9Gjh9Q7ETti+WRHnBnGCSCq+6q7m3p/nmUQviE=
knative.dev/hack v0.0.0-20230501013555-7d81248b4638 h1:9IuXHdwp5jNmIg+0LVTQr8o4u0FYD99uCfynM9tS0XY=
knative.dev/hack v0.0.0-20230501013555-7d81248b4638/go.mod h1:yk2OjGDsbEnQjfxdm0/HJKS2WqTLEFg/N6nUs6Rqx3Q=
knative.dev/pkg v0.0.0-20230501013355-904966742b58 h1:A8F5gaIpL34Zh746M2q7HEI2+wLulYMMNrFuetRwymM=
knative.dev/pkg v0.0.0-20230501013355-904966742b58/go.mod h1:2qWPP9Gjh9Q7ETti+WRHnBnGCSCq+6q7m3p/nmUQviE=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
41 changes: 23 additions & 18 deletions test/conformance/ingress/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import (
"context"
"fmt"
"math/rand"
"net"
"net/http"
"net/url"
"strings"
"testing"
"time"

"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"github.com/google/go-cmp/cmp"
"github.com/gorilla/websocket"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/networking/pkg/apis/networking/v1alpha1"
Expand Down Expand Up @@ -63,14 +65,14 @@ func TestWebsocket(t *testing.T) {
}},
})

dialer := websocket.Dialer{
NetDialContext: dialCtx,
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
dialer := ws.Dialer{
NetDial: dialCtx,
Timeout: 45 * time.Second,
Header: ws.HandshakeHeaderHTTP(http.Header{"Host": {domain}}),
}

u := url.URL{Scheme: "ws", Host: domain, Path: "/"}
conn, _, err := dialer.Dial(u.String(), http.Header{"Host": {domain}})
conn, _, _, err := dialer.Dial(ctx, u.String())
if err != nil {
t.Fatal("Dial() =", err)
}
Expand Down Expand Up @@ -124,17 +126,17 @@ func TestWebsocketSplit(t *testing.T) {
}},
})

dialer := websocket.Dialer{
NetDialContext: dialCtx,
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
dialer := ws.Dialer{
NetDial: dialCtx,
Timeout: 45 * time.Second,
Header: ws.HandshakeHeaderHTTP(http.Header{"Host": {domain}}),
}
u := url.URL{Scheme: "ws", Host: domain, Path: "/"}

const maxRequests = 100
got := sets.NewString()
for i := 0; i < maxRequests; i++ {
conn, _, err := dialer.Dial(u.String(), http.Header{"Host": {domain}})
conn, _, _, err := dialer.Dial(ctx, u.String())
if err != nil {
t.Fatal("Dial() =", err)
}
Expand All @@ -160,20 +162,22 @@ func TestWebsocketSplit(t *testing.T) {
t.Errorf("(over %d requests) (-want, +got) = %s", maxRequests, cmp.Diff(want.List(), got.List()))
}

func findWebsocketSuffix(ctx context.Context, t *testing.T, conn *websocket.Conn) string {
func findWebsocketSuffix(_ context.Context, t *testing.T, conn net.Conn) string {
t.Helper()
// Establish the suffix that corresponds to this socket.
message := fmt.Sprint("ping -", rand.Intn(1000))
if err := conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
if err := wsutil.WriteMessage(conn, ws.StateClientSide, ws.OpText, []byte(message)); err != nil {
t.Error("WriteMessage() =", err)
return ""
}

_, recv, err := conn.ReadMessage()
var messages []wsutil.Message
messages, err := wsutil.ReadMessage(conn, ws.StateClientSide, messages)
if err != nil {
t.Error("ReadMessage() =", err)
return ""
}
recv := messages[0].Payload
gotMsg := string(recv)
if !strings.HasPrefix(gotMsg, message) {
t.Errorf("ReadMessage() = %s, wanted %s prefix", gotMsg, message)
Expand All @@ -182,18 +186,19 @@ func findWebsocketSuffix(ctx context.Context, t *testing.T, conn *websocket.Conn
return strings.TrimSpace(strings.TrimPrefix(gotMsg, message))
}

func checkWebsocketRoundTrip(ctx context.Context, t *testing.T, conn *websocket.Conn, suffix string) {
func checkWebsocketRoundTrip(_ context.Context, t *testing.T, conn net.Conn, suffix string) {
t.Helper()
message := fmt.Sprint("ping -", rand.Intn(1000))
if err := conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
if err := wsutil.WriteMessage(conn, ws.StateClientSide, ws.OpText, []byte(message)); err != nil {
t.Error("WriteMessage() =", err)
return
}

// Read back the echoed message and compared with sent.
if _, recv, err := conn.ReadMessage(); err != nil {
var messages []wsutil.Message
if messages, err := wsutil.ReadMessage(conn, ws.StateClientSide, messages); err != nil {
t.Error("ReadMessage() =", err)
} else if got, want := string(recv), message+" "+suffix; got != want {
} else if got, want := string(messages[0].Payload), message+" "+suffix; got != want {
t.Errorf("ReadMessage() = %s, wanted %s", got, want)
}
}
58 changes: 33 additions & 25 deletions test/test_images/wsserver/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ limitations under the License.
package main

import (
"errors"
"flag"
"io"
"log"
"net/http"
"os"

"github.com/gorilla/websocket"
"github.com/gobwas/httphead"
"github.com/gobwas/ws"
"github.com/gobwas/ws/wsutil"
"knative.dev/networking/pkg/http/header"
"knative.dev/networking/pkg/http/probe"
"knative.dev/networking/test"
Expand All @@ -39,10 +43,9 @@ func messageSuffix() string {
return value
}

var upgrader = websocket.Upgrader{
// Allow any origin, since we are spoofing requests anyway.
CheckOrigin: func(r *http.Request) bool {
return true
var upgrader = ws.HTTPUpgrader{
Negotiate: func(opt httphead.Option) (ret httphead.Option, err error) {
return httphead.Option{}, nil
},
}

Expand All @@ -51,36 +54,41 @@ func handler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
return
}
conn, err := upgrader.Upgrade(w, r, nil)
conn, _, _, err := upgrader.Upgrade(r, w)
if err != nil {
log.Println("Error upgrading websocket:", err)
return
}
defer conn.Close()
log.Println("Connection upgraded to WebSocket. Entering receive loop.")
for {
messageType, message, err := conn.ReadMessage()
if err != nil {
// We close abnormally, because we're just closing the connection in the client,
// which is okay. There's no value delaying closure of the connection unnecessarily.
if websocket.IsCloseError(err, websocket.CloseAbnormalClosure) {
log.Println("Client disconnected.")
} else {
log.Println("Handler exiting on error:", err)
var messages []wsutil.Message
messages, err = wsutil.ReadMessage(conn, ws.StateServerSide, messages)
for _, m := range messages {
message := m.Payload
messageType := m.OpCode
if err != nil {
// We close abnormally, because we're just closing the connection in the client,
// which is okay. There's no value delaying closure of the connection unnecessarily.
if errors.Is(err, io.ErrUnexpectedEOF) {
log.Println("Client disconnected.")
} else {
log.Println("Handler exiting on error:", err)
}
return
}
if suffix := messageSuffix(); suffix != "" {
respMes := string(message) + " " + suffix
message = []byte(respMes)
}
return
}
if suffix := messageSuffix(); suffix != "" {
respMes := string(message) + " " + suffix
message = []byte(respMes)
}

log.Printf("Successfully received: %q", message)
if err = conn.WriteMessage(messageType, message); err != nil {
log.Println("Failed to write message:", err)
return
log.Printf("Successfully received: %q", message)
if err = wsutil.WriteClientMessage(conn, messageType, message); err != nil {
log.Println("Failed to write message:", err)
return
}
log.Printf("Successfully wrote: %q", message)
}
log.Printf("Successfully wrote: %q", message)
}
}

Expand Down
21 changes: 21 additions & 0 deletions third_party/VENDOR-LICENSE/github.com/gobwas/httphead/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Sergey Kamardin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions third_party/VENDOR-LICENSE/github.com/gobwas/pool/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017-2019 Sergey Kamardin <gobwas@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions third_party/VENDOR-LICENSE/github.com/gobwas/ws/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017-2021 Sergey Kamardin <gobwas@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 0 additions & 22 deletions third_party/VENDOR-LICENSE/github.com/gorilla/websocket/LICENSE

This file was deleted.

Loading
Loading