Skip to content

Commit 3765482

Browse files
authored
Merge pull request #2472 from dprotaso/h2c-echo-server
add h2c server to echo-basic
2 parents 8b9b22c + 2bbdb33 commit 3765482

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

conformance/echo-basic/echo-basic.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
"strconv"
2929
"strings"
3030
"time"
31+
32+
"golang.org/x/net/http2"
33+
"golang.org/x/net/http2/h2c"
3134
)
3235

3336
// RequestAssertions contains information about the request and the Ingress
@@ -76,6 +79,10 @@ func main() {
7679
if httpPort == "" {
7780
httpPort = "3000"
7881
}
82+
h2cPort := os.Getenv("H2C_PORT")
83+
if h2cPort == "" {
84+
h2cPort = "3001"
85+
}
7986

8087
httpsPort := os.Getenv("HTTPS_PORT")
8188
if httpsPort == "" {
@@ -105,6 +112,8 @@ func main() {
105112
}
106113
}()
107114

115+
go runH2CServer(h2cPort, errchan)
116+
108117
// Enable HTTPS if certificate and private key are given.
109118
if os.Getenv("TLS_SERVER_CERT") != "" && os.Getenv("TLS_SERVER_PRIVKEY") != "" {
110119
go func() {
@@ -152,6 +161,28 @@ func delayResponse(request *http.Request) error {
152161
return nil
153162
}
154163

164+
func runH2CServer(h2cPort string, errchan chan<- error) {
165+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
166+
if r.ProtoMajor != 2 && r.Header.Get("Upgrade") != "h2c" {
167+
w.WriteHeader(http.StatusBadRequest)
168+
fmt.Fprint(w, "Expected h2c request")
169+
return
170+
}
171+
172+
echoHandler(w, r)
173+
})
174+
h2c := &http.Server{
175+
ReadHeaderTimeout: time.Second,
176+
Addr: fmt.Sprintf(":%s", h2cPort),
177+
Handler: h2c.NewHandler(handler, &http2.Server{}),
178+
}
179+
fmt.Printf("Starting server, listening on port %s (h2c)\n", h2cPort)
180+
err := h2c.ListenAndServe()
181+
if err != nil {
182+
errchan <- err
183+
}
184+
}
185+
155186
func echoHandler(w http.ResponseWriter, r *http.Request) {
156187
fmt.Printf("Echoing back request made to %s to client (%s)\n", r.RequestURI, r.RemoteAddr)
157188

conformance/echo-basic/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module sigs.k8s.io/gateway-api/conformance/echo-basic
22

33
go 1.20
4+
5+
require golang.org/x/net v0.17.0
6+
7+
require golang.org/x/text v0.13.0 // indirect

conformance/echo-basic/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
2+
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
3+
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
4+
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
5+
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
6+
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=

go.work.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
12
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
3+
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
4+
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
5+
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
6+
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
7+
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
8+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
9+
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
10+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
11+
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
12+
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13+
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
14+
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
15+
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
16+
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
17+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
18+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=

0 commit comments

Comments
 (0)