http3: add compatibility with net/http.ResponseController (#3790)

* feat: compatibility with "net/http".ResponseController

* better deadline tests

* don't run deadline tests on Go 1.19

* skip deadline tests on Go 1.19
This commit is contained in:
Kévin Dunglas 2023-05-01 13:40:33 +02:00 committed by GitHub
parent 4a2a5740b2
commit 172123c340
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 129 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import (
"net/http"
"strconv"
"strings"
"time"
"github.com/quic-go/quic-go"
"github.com/quic-go/quic-go/internal/utils"
@ -15,6 +16,7 @@ import (
type responseWriter struct {
conn quic.Connection
str quic.Stream
bufferedStr *bufio.Writer
buf []byte
@ -36,6 +38,7 @@ func newResponseWriter(str quic.Stream, conn quic.Connection, logger utils.Logge
header: http.Header{},
buf: make([]byte, 16),
conn: conn,
str: str,
bufferedStr: bufio.NewWriter(str),
logger: logger,
}
@ -121,6 +124,14 @@ func (w *responseWriter) StreamCreator() StreamCreator {
return w.conn
}
func (w *responseWriter) SetReadDeadline(deadline time.Time) error {
return w.str.SetReadDeadline(deadline)
}
func (w *responseWriter) SetWriteDeadline(deadline time.Time) error {
return w.str.SetWriteDeadline(deadline)
}
// copied from http2/http2.go
// bodyAllowedForStatus reports whether a given response status code
// permits a body. See RFC 2616, section 4.4.

View file

@ -4,6 +4,7 @@ import (
"bytes"
"io"
"net/http"
"time"
mockquic "github.com/quic-go/quic-go/internal/mocks/quic"
"github.com/quic-go/quic-go/internal/utils"
@ -25,6 +26,8 @@ var _ = Describe("Response Writer", func() {
strBuf = &bytes.Buffer{}
str := mockquic.NewMockStream(mockCtrl)
str.EXPECT().Write(gomock.Any()).DoAndReturn(strBuf.Write).AnyTimes()
str.EXPECT().SetReadDeadline(gomock.Any()).Return(nil).AnyTimes()
str.EXPECT().SetWriteDeadline(gomock.Any()).Return(nil).AnyTimes()
rw = newResponseWriter(str, nil, utils.DefaultLogger)
})
@ -156,4 +159,9 @@ var _ = Describe("Response Writer", func() {
fields := decodeHeader(strBuf)
Expect(fields).To(HaveKeyWithValue("content-type", []string{"text/html; charset=utf-8"}))
})
It(`is compatible with "net/http".ResponseController`, func() {
Expect(rw.SetReadDeadline(time.Now().Add(1 * time.Second))).To(BeNil())
Expect(rw.SetWriteDeadline(time.Now().Add(1 * time.Second))).To(BeNil())
})
})