Description
Go version
go version go1.22.0 linux/amd64
Output of go env
in your module/workspace:
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/akashem/.cache/go-build'
GOENV='/home/akashem/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/akashem/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/akashem/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/akashem/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.linux-amd64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/akashem/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.linux-amd64/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/akashem/go/src/k8s.io/kubernetes/go.mod'
GOWORK='/home/akashem/go/src/k8s.io/kubernetes/go.work'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2560711445=/tmp/go-build -gno-record-gcc-switches'
What did you do?
The base implementation of http.ResponseWriter
that net/http
passes to the user defined request handler additionally implements the following interfaces:
For http/1x:
http.Flusher
interface{ FlushError() error }
(recently added to supportResponseController
)http.CloseNotifier
http.Hijacker
For http/2.0:
http.Flusher
interface{ FlushError() error }
http.CloseNotifier
(there are more additional interfaces, but we are only interested in the above)
Can we use the following as an invariant?
- a) the
ResponseWriter
object for any http request implements the following interface(s)
type CloseNotifierFlusher interface {
http.CloseNotifier
http.Flusher
FlushError() error
}
b) the ResponseWriter
object for http/1x
request additionally implements http.Hijacker
We use the above invariant to wrap a ResponseWriter
object:
func wrap(w http.ResponseWriter) {
if notifierFlusher, ok := w.(CloseNotifierFlusher); ok {
// wrap the object, this covers http/2
}
if hijacker, ok := inner.(http.Hijacker); ok {
// extend the wrapped object with http.Hijacker
// this covers http/1x
}
}
My question is - is there any type of request for which the above invariant does not hold? Another alternate is to use httpsnoop which checks for every combination of these additional interfaces possible.
The following tests assert on the invariant for http/1x and http/2.0:
func TestHTTP1xResponseWriterInvariant(t *testing.T) {
doneCh := make(chan struct{})
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer close(doneCh)
if r.ProtoMajor != 1 {
t.Errorf("expected http/1x")
}
if _, ok := w.(http.Flusher); !ok {
t.Errorf("expected the ResponseWriter object to implement http.Flusher")
}
if _, ok := w.(interface{ FlushError() error }); !ok {
t.Errorf("expected the ResponseWriter object to implement Flusher with error")
}
if _, ok := w.(http.CloseNotifier); !ok {
t.Errorf("expected the http.ResponseWriter object to implement http.CloseNotifier")
}
if _, ok := w.(http.Hijacker); !ok {
t.Errorf("expected the http.ResponseWriter object to implement http.Hijacker")
}
})
server := httptest.NewUnstartedServer(handler)
defer server.Close()
server.StartTLS()
if _, err := server.Client().Get(server.URL + "/ping"); err != nil {
t.Errorf("unexpected error: %v", err)
}
select {
case <-doneCh:
default:
t.Errorf("expected the request handler to be invoked")
}
}
func TestHTTP2ResponseWriterInvariant(t *testing.T) {
doneCh := make(chan struct{})
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer close(doneCh)
if r.ProtoMajor != 2 {
t.Errorf("expected http/2.0")
}
if _, ok := w.(http.Flusher); !ok {
t.Errorf("expected the ResponseWriter object to implement http.Flusher")
}
if _, ok := w.(interface{ FlushError() error }); !ok {
t.Errorf("expected the ResponseWriter object to implement Flusher with error")
}
if _, ok := w.(http.CloseNotifier); !ok {
t.Errorf("expected the http.ResponseWriter object to implement http.CloseNotifier")
}
})
server := httptest.NewUnstartedServer(handler)
server.EnableHTTP2 = true
defer server.Close()
server.StartTLS()
if _, err := server.Client().Get(server.URL + "/ping"); err != nil {
t.Errorf("unexpected error: %v", err)
}
select {
case <-doneCh:
default:
t.Errorf("expected the request handler to be invoked")
}
}
I would appreciate your thoughts on this, thanks!