Skip to content

Commit

Permalink
add h2c server to echo-basic
Browse files Browse the repository at this point in the history
  • Loading branch information
dprotaso committed Oct 12, 2023
1 parent 881f13b commit f5c84fc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
41 changes: 41 additions & 0 deletions conformance/echo-basic/echo-basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
"strconv"
"strings"
"time"

"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

// RequestAssertions contains information about the request and the Ingress
Expand Down Expand Up @@ -76,6 +79,10 @@ func main() {
if httpPort == "" {
httpPort = "3000"
}
h2cPort := os.Getenv("H2C_PORT")
if h2cPort == "" {
h2cPort = "3001"
}

httpsPort := os.Getenv("HTTPS_PORT")
if httpsPort == "" {
Expand Down Expand Up @@ -105,6 +112,8 @@ func main() {
}
}()

go runH2CServer(h2cPort, errchan)

// Enable HTTPS if certificate and private key are given.
if os.Getenv("TLS_SERVER_CERT") != "" && os.Getenv("TLS_SERVER_PRIVKEY") != "" {
go func() {
Expand Down Expand Up @@ -152,6 +161,38 @@ func delayResponse(request *http.Request) error {
return nil
}

func runH2CServer(h2cPort string, errchan chan<- error) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor != 2 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Expected h2(c) request")
return
}

echoHandler(w, r)
})
h1s := &http.Server{
ReadHeaderTimeout: time.Second,
Addr: fmt.Sprintf(":%s", h2cPort),
Handler: h2c.NewHandler(handler, &http2.Server{}),
}
fmt.Printf("Starting server, listening on port %s (h2c)\n", h2cPort)
err := h1s.ListenAndServe()
if err != nil {
errchan <- err
}
}

func h2cHandler(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor != 2 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Expected h2(c) request")
return
}

echoHandler(w, r)
}

func echoHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Echoing back request made to %s to client (%s)\n", r.RequestURI, r.RemoteAddr)

Expand Down
4 changes: 4 additions & 0 deletions conformance/echo-basic/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module sigs.k8s.io/gateway-api/conformance/echo-basic

go 1.20

require golang.org/x/net v0.17.0

require golang.org/x/text v0.13.0 // indirect
6 changes: 6 additions & 0 deletions conformance/echo-basic/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
17 changes: 17 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=

0 comments on commit f5c84fc

Please sign in to comment.