You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 support ResponseController)
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!
The text was updated successfully, but these errors were encountered:
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
Go version
go version go1.22.0 linux/amd64
Output of
go env
in your module/workspace:What did you do?
The base implementation of
http.ResponseWriter
thatnet/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?
ResponseWriter
object for any http request implements the following interface(s)b) the
ResponseWriter
object forhttp/1x
request additionally implementshttp.Hijacker
We use the above invariant to wrap a
ResponseWriter
object: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:
I would appreciate your thoughts on this, thanks!
The text was updated successfully, but these errors were encountered: