TUN-7253: Adopt http.ResponseWriter for connection.ResponseWriter

This commit is contained in:
Devin Carr 2023-03-07 10:41:15 -08:00
parent 27f88ae209
commit bf3136debb
4 changed files with 31 additions and 9 deletions

View File

@ -200,6 +200,7 @@ func (h *HTTPResponseReadWriteAcker) AckConnection(tracePropagation string) erro
type ResponseWriter interface { type ResponseWriter interface {
WriteRespHeaders(status int, header http.Header) error WriteRespHeaders(status int, header http.Header) error
AddTrailer(trailerName, trailerValue string) AddTrailer(trailerName, trailerValue string)
http.ResponseWriter
io.Writer io.Writer
} }

View File

@ -197,6 +197,7 @@ type http2RespWriter struct {
flusher http.Flusher flusher http.Flusher
shouldFlush bool shouldFlush bool
statusWritten bool statusWritten bool
respHeaders http.Header
log *zerolog.Logger log *zerolog.Logger
} }
@ -276,6 +277,14 @@ func (rp *http2RespWriter) WriteRespHeaders(status int, header http.Header) erro
return nil return nil
} }
func (rp *http2RespWriter) Header() http.Header {
return rp.respHeaders
}
func (rp *http2RespWriter) WriteHeader(status int) {
rp.WriteRespHeaders(status, rp.respHeaders)
}
func (rp *http2RespWriter) WriteErrorResponse() bool { func (rp *http2RespWriter) WriteErrorResponse() bool {
if rp.statusWritten { if rp.statusWritten {
return false return false

View File

@ -402,6 +402,7 @@ func (s *streamReadWriteAcker) AckConnection(tracePropagation string) error {
// httpResponseAdapter translates responses written by the HTTP Proxy into ones that can be used in QUIC. // httpResponseAdapter translates responses written by the HTTP Proxy into ones that can be used in QUIC.
type httpResponseAdapter struct { type httpResponseAdapter struct {
*quicpogs.RequestServerStream *quicpogs.RequestServerStream
headers http.Header
connectResponseSent bool connectResponseSent bool
} }
@ -426,6 +427,14 @@ func (hrw *httpResponseAdapter) WriteRespHeaders(status int, header http.Header)
return hrw.WriteConnectResponseData(nil, metadata...) return hrw.WriteConnectResponseData(nil, metadata...)
} }
func (hrw *httpResponseAdapter) Header() http.Header {
return hrw.headers
}
func (hrw *httpResponseAdapter) WriteHeader(status int) {
hrw.WriteRespHeaders(status, hrw.headers)
}
func (hrw *httpResponseAdapter) WriteErrorResponse(err error) { func (hrw *httpResponseAdapter) WriteErrorResponse(err error) {
hrw.WriteConnectResponseData(err, quicpogs.Metadata{Key: "HttpStatus", Val: strconv.Itoa(http.StatusBadGateway)}) hrw.WriteConnectResponseData(err, quicpogs.Metadata{Key: "HttpStatus", Val: strconv.Itoa(http.StatusBadGateway)})
} }

View File

@ -700,8 +700,7 @@ func TestConnections(t *testing.T) {
cancel() cancel()
assert.Equal(t, test.want.err, err != nil) assert.Equal(t, test.want.err, err != nil)
assert.Equal(t, test.want.message, replayer.Bytes()) assert.Equal(t, test.want.message, replayer.Bytes())
respPrinter := respWriter.(responsePrinter) assert.Equal(t, test.want.headers, respWriter.Header())
assert.Equal(t, test.want.headers, respPrinter.headers())
replayer.rw.Reset() replayer.rw.Reset()
}) })
} }
@ -794,10 +793,6 @@ func (p *pipedRequestBody) Close() error {
return nil return nil
} }
type responsePrinter interface {
headers() http.Header
}
type wsRespWriter struct { type wsRespWriter struct {
w io.Writer w io.Writer
responseHeaders http.Header responseHeaders http.Header
@ -836,12 +831,16 @@ func (w *wsRespWriter) AddTrailer(trailerName, trailerValue string) {
} }
// respHeaders is a test function to read respHeaders // respHeaders is a test function to read respHeaders
func (w *wsRespWriter) headers() http.Header { func (w *wsRespWriter) Header() http.Header {
// Removing indeterminstic header because it cannot be asserted. // Removing indeterminstic header because it cannot be asserted.
w.responseHeaders.Del("Date") w.responseHeaders.Del("Date")
return w.responseHeaders return w.responseHeaders
} }
func (w *wsRespWriter) WriteHeader(status int) {
// unused
}
type mockTCPRespWriter struct { type mockTCPRespWriter struct {
w io.Writer w io.Writer
responseHeaders http.Header responseHeaders http.Header
@ -862,7 +861,7 @@ func (m *mockTCPRespWriter) Write(p []byte) (n int, err error) {
return m.w.Write(p) return m.w.Write(p)
} }
func (w *mockTCPRespWriter) AddTrailer(trailerName, trailerValue string) { func (m *mockTCPRespWriter) AddTrailer(trailerName, trailerValue string) {
// do nothing // do nothing
} }
@ -873,10 +872,14 @@ func (m *mockTCPRespWriter) WriteRespHeaders(status int, header http.Header) err
} }
// respHeaders is a test function to read respHeaders // respHeaders is a test function to read respHeaders
func (m *mockTCPRespWriter) headers() http.Header { func (m *mockTCPRespWriter) Header() http.Header {
return m.responseHeaders return m.responseHeaders
} }
func (m *mockTCPRespWriter) WriteHeader(status int) {
// do nothing
}
func createSingleIngressConfig(t *testing.T, service string) ingress.Ingress { func createSingleIngressConfig(t *testing.T, service string) ingress.Ingress {
ingressConfig := &config.Configuration{ ingressConfig := &config.Configuration{
Ingress: []config.UnvalidatedIngressRule{ Ingress: []config.UnvalidatedIngressRule{