Skip to content

Commit 55c9363

Browse files
author
Peter Kieltyka
committed
Simplify go17 and go18 differences to support http2 WrapWriter
1 parent be7a9b9 commit 55c9363

13 files changed

+209
-360
lines changed

middleware/compress.go

+6-75
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package middleware
22

33
import (
4-
"bufio"
54
"compress/flate"
65
"compress/gzip"
76
"io"
8-
"net"
97
"net/http"
108
"strings"
119
)
@@ -62,7 +60,7 @@ func Compress(level int, types ...string) func(next http.Handler) http.Handler {
6260
}
6361
defer mcw.Close()
6462

65-
next.ServeHTTP(mkGenericWrapper(mcw), r)
63+
next.ServeHTTP(mcw, r)
6664
}
6765

6866
return http.HandlerFunc(fn)
@@ -175,82 +173,15 @@ func (w *maybeCompressResponseWriter) Write(p []byte) (int, error) {
175173
return w.w.Write(p)
176174
}
177175

178-
func (w *maybeCompressResponseWriter) Unwrap() http.ResponseWriter {
179-
return w.ResponseWriter
176+
func (w *maybeCompressResponseWriter) Flush() {
177+
if f, ok := w.w.(http.Flusher); ok {
178+
f.Flush()
179+
}
180180
}
181181

182182
func (w *maybeCompressResponseWriter) Close() error {
183-
if c, ok := w.w.(io.Closer); ok {
183+
if c, ok := w.w.(io.WriteCloser); ok {
184184
return c.Close()
185185
}
186186
return nil
187187
}
188-
189-
type responseWriterWrapper interface {
190-
http.ResponseWriter
191-
Unwrap() http.ResponseWriter
192-
}
193-
194-
type genericWrapper struct {
195-
inner responseWriterWrapper
196-
}
197-
198-
func (g *genericWrapper) Unwrap() http.ResponseWriter {
199-
return g.inner
200-
}
201-
202-
func (g *genericWrapper) Header() http.Header {
203-
return g.inner.Header()
204-
}
205-
206-
func (g *genericWrapper) Write(data []byte) (int, error) {
207-
return g.inner.Write(data)
208-
}
209-
210-
func (g *genericWrapper) WriteHeader(status int) {
211-
g.inner.WriteHeader(status)
212-
}
213-
214-
var _ http.ResponseWriter = &genericWrapper{}
215-
216-
type httpGenericWrapper struct {
217-
genericWrapper
218-
}
219-
220-
func (h *httpGenericWrapper) CloseNotify() <-chan bool {
221-
if cn, ok := h.genericWrapper.inner.(http.CloseNotifier); ok {
222-
return cn.CloseNotify()
223-
}
224-
225-
return h.genericWrapper.inner.Unwrap().(http.CloseNotifier).CloseNotify()
226-
}
227-
func (h *httpGenericWrapper) Flush() {
228-
if f, ok := h.genericWrapper.inner.(http.Flusher); ok {
229-
f.Flush()
230-
return
231-
}
232-
233-
h.genericWrapper.inner.Unwrap().(http.Flusher).Flush()
234-
}
235-
236-
func (h *httpGenericWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
237-
if hj, ok := h.genericWrapper.inner.(http.Hijacker); ok {
238-
return hj.Hijack()
239-
}
240-
241-
return h.genericWrapper.inner.Unwrap().(http.Hijacker).Hijack()
242-
}
243-
244-
func (h *httpGenericWrapper) ReadFrom(r io.Reader) (n int64, err error) {
245-
if rf, ok := h.genericWrapper.inner.(io.ReaderFrom); ok {
246-
return rf.ReadFrom(r)
247-
}
248-
249-
return h.genericWrapper.inner.Unwrap().(io.ReaderFrom).ReadFrom(r)
250-
}
251-
252-
var _ http.ResponseWriter = &httpGenericWrapper{}
253-
var _ http.CloseNotifier = &httpGenericWrapper{}
254-
var _ http.Flusher = &httpGenericWrapper{}
255-
var _ http.Hijacker = &httpGenericWrapper{}
256-
var _ io.ReaderFrom = &httpGenericWrapper{}

middleware/compress_17.go

-22
This file was deleted.

middleware/compress_18.go

-58
This file was deleted.

middleware/compress_18_test.go

-69
This file was deleted.

middleware/compress_test.go

-69
This file was deleted.

middleware/logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler {
2929
return func(next http.Handler) http.Handler {
3030
fn := func(w http.ResponseWriter, r *http.Request) {
3131
entry := f.NewLogEntry(r)
32-
ww := NewWrapResponseWriter(w)
32+
ww := NewWrapResponseWriter(w, r.ProtoMajor)
3333

3434
t1 := time.Now()
3535
defer func() {

0 commit comments

Comments
 (0)