Skip to content

Commit c28ba10

Browse files
committed
feat(middleware-logging): implement http.Hijacker
http.Hijacker is mandatory to forward websocket connexions Signed-off-by: Guilhem Lettron <[email protected]>
1 parent 70aa9be commit c28ba10

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

go.mod

+15-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.2
55
require (
66
github.com/go-logr/logr v1.4.2
77
github.com/google/go-cmp v0.6.0
8+
github.com/gorilla/websocket v1.5.1
89
github.com/hashicorp/go-immutable-radix/v2 v2.1.0
910
github.com/kedacore/keda/v2 v2.14.1-0.20240429185716-c55e306af94b
1011
github.com/kelseyhightower/envconfig v1.4.0
@@ -45,6 +46,20 @@ require (
4546
github.com/beorn7/perks v1.0.1 // indirect
4647
github.com/blang/semver/v4 v4.0.0 // indirect
4748
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
49+
github.com/go-logr/stdr v1.2.2 // indirect
50+
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
51+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
52+
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
53+
go.opentelemetry.io/otel/trace v1.28.0 // indirect
54+
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
55+
go.uber.org/zap v1.27.0 // indirect
56+
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
57+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
58+
k8s.io/component-base v0.29.4 // indirect
59+
k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 // indirect
60+
)
61+
62+
require (
4863
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4964
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
5065
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
@@ -53,12 +68,10 @@ require (
5368
github.com/expr-lang/expr v1.16.9 // indirect
5469
github.com/fsnotify/fsnotify v1.7.0 // indirect
5570
github.com/go-errors/errors v1.5.1 // indirect
56-
github.com/go-logr/stdr v1.2.2 // indirect
5771
github.com/go-logr/zapr v1.3.0 // indirect
5872
github.com/go-openapi/jsonpointer v0.21.0 // indirect
5973
github.com/go-openapi/jsonreference v0.21.0 // indirect
6074
github.com/go-openapi/swag v0.23.0 // indirect
61-
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
6275
github.com/gogo/protobuf v1.3.2 // indirect
6376
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
6477
github.com/golang/protobuf v1.5.4 // indirect
@@ -67,8 +80,6 @@ require (
6780
github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect
6881
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
6982
github.com/google/uuid v1.6.0 // indirect
70-
github.com/gorilla/websocket v1.5.1 // indirect
71-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
7283
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
7384
github.com/imdario/mergo v0.3.16 // indirect
7485
github.com/inconshreveable/mousetrap v1.1.0 // indirect
@@ -80,7 +91,6 @@ require (
8091
github.com/modern-go/reflect2 v1.0.2 // indirect
8192
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
8293
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
83-
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
8494
github.com/pkg/errors v0.9.1 // indirect
8595
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
8696
github.com/prometheus/client_golang v1.19.1
@@ -93,11 +103,8 @@ require (
93103
go.opentelemetry.io/otel/exporters/prometheus v0.50.0
94104
go.opentelemetry.io/otel/metric v1.28.0
95105
go.opentelemetry.io/otel/sdk/metric v1.28.0
96-
go.opentelemetry.io/otel/trace v1.28.0 // indirect
97-
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
98106
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 // indirect
99107
go.uber.org/multierr v1.11.0 // indirect
100-
go.uber.org/zap v1.27.0 // indirect
101108
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
102109
golang.org/x/mod v0.18.0 // indirect
103110
golang.org/x/net v0.26.0 // indirect
@@ -108,15 +115,11 @@ require (
108115
golang.org/x/time v0.5.0 // indirect
109116
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
110117
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
111-
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
112118
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
113-
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
114119
gopkg.in/inf.v0 v0.9.1 // indirect
115120
gopkg.in/yaml.v2 v2.4.0 // indirect
116121
gopkg.in/yaml.v3 v3.0.1 // indirect
117122
k8s.io/apiextensions-apiserver v0.29.4 // indirect
118-
k8s.io/component-base v0.29.4 // indirect
119-
k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 // indirect
120123
k8s.io/klog/v2 v2.120.1 // indirect
121124
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
122125
knative.dev/pkg v0.0.0-20240423132823-3c6badc82748 // indirect

interceptor/middleware/responsewriter.go

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package middleware
22

33
import (
4+
"bufio"
5+
"errors"
6+
"net"
47
"net/http"
58
)
69

@@ -46,3 +49,11 @@ func (rw *responseWriter) WriteHeader(statusCode int) {
4649

4750
rw.statusCode = statusCode
4851
}
52+
53+
func (rw *responseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
54+
if hj, ok := rw.downstreamResponseWriter.(http.Hijacker); ok {
55+
return hj.Hijack()
56+
}
57+
58+
return nil, nil, errors.New("http.Hijacker not implemented")
59+
}

interceptor/middleware/responsewriter_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"net/http/httptest"
66

7+
"github.com/gorilla/websocket"
78
. "github.com/onsi/ginkgo/v2"
89
. "github.com/onsi/gomega"
910
)
@@ -119,4 +120,49 @@ var _ = Describe("responseWriter", func() {
119120
Expect(w.Code).To(Equal(sc))
120121
})
121122
})
123+
124+
Context("Websocket", func() {
125+
It("returns the expected values when http.Hijacker is implemented", func() {
126+
127+
// Create a server that will accept websocket connections
128+
w := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
129+
// Upgrade the connection to a websocket connection
130+
upgrader := websocket.Upgrader{}
131+
conn, err := upgrader.Upgrade(w, r, nil)
132+
Expect(err).To(BeNil())
133+
Expect(conn).NotTo(BeNil())
134+
135+
// Write a message to the client
136+
err = conn.WriteMessage(websocket.TextMessage, []byte("hello"))
137+
Expect(err).To(BeNil())
138+
139+
// Close the connection
140+
err = conn.Close()
141+
Expect(err).To(BeNil())
142+
}))
143+
defer w.Close()
144+
145+
w.Client()
146+
147+
lrw := &loggingResponseWriter{
148+
downstreamResponseWriter: w.Client(),
149+
}
150+
151+
// Create a client that will connect to the server
152+
dialer := websocket.Dialer{}
153+
conn, _, err := dialer.Dial(w.URL, nil)
154+
})
155+
156+
It("returns an error when http.Hijacker is not implemented", func() {
157+
w := httptest.NewRecorder()
158+
lrw := &loggingResponseWriter{
159+
downstreamResponseWriter: w,
160+
}
161+
162+
c, r, err := lrw.Hijack()
163+
Expect(err).To(MatchError("http.Hijacker not implemented"))
164+
Expect(c).To(BeNil())
165+
Expect(r).To(BeNil())
166+
})
167+
})
122168
})

0 commit comments

Comments
 (0)