Skip to content

Commit

Permalink
WIP: make it work with older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
aldas committed Mar 6, 2024
1 parent c0d727f commit 2100345
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
4 changes: 2 additions & 2 deletions middleware/body_dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ func (w *bodyDumpResponseWriter) Write(b []byte) (int, error) {
}

func (w *bodyDumpResponseWriter) Flush() {
err := http.NewResponseController(w.ResponseWriter).Flush()
err := responseControllerFlush(w.ResponseWriter)
if err != nil && errors.Is(err, http.ErrNotSupported) {
panic(errors.New("response writer flushing is not supported"))
}
}

func (w *bodyDumpResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return http.NewResponseController(w.ResponseWriter).Hijack()
return responseControllerHijack(w.ResponseWriter)
}

func (w *bodyDumpResponseWriter) Unwrap() http.ResponseWriter {
Expand Down
4 changes: 2 additions & 2 deletions middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,15 @@ func (w *gzipResponseWriter) Flush() {
}

w.Writer.(*gzip.Writer).Flush()
http.NewResponseController(w.ResponseWriter).Flush()
_ = responseControllerFlush(w.ResponseWriter)
}

func (w *gzipResponseWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}

func (w *gzipResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return http.NewResponseController(w.ResponseWriter).Hijack()
return responseControllerHijack(w.ResponseWriter)
}

func (w *gzipResponseWriter) Push(target string, opts *http.PushOptions) error {
Expand Down
41 changes: 41 additions & 0 deletions middleware/responsecontroller_1.19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build !go1.20

package middleware

import (
"bufio"
"fmt"
"net"
"net/http"
)

// TODO: remove when Go 1.23 is released and we do not support 1.19 anymore
func responseControllerFlush(rw http.ResponseWriter) error {
for {
switch t := rw.(type) {
case interface{ FlushError() error }:
return t.FlushError()
case http.Flusher:
t.Flush()
return nil
case interface{ Unwrap() http.ResponseWriter }:
rw = t.Unwrap()
default:
return fmt.Errorf("%w", http.ErrNotSupported)
}
}
}

// TODO: remove when Go 1.23 is released and we do not support 1.19 anymore
func responseControllerHijack(rw http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) {
for {
switch t := rw.(type) {
case http.Hijacker:
return t.Hijack()
case interface{ Unwrap() http.ResponseWriter }:
rw = t.Unwrap()
default:
return nil, nil, fmt.Errorf("%w", http.ErrNotSupported)
}
}
}
17 changes: 17 additions & 0 deletions middleware/responsecontroller_1.20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build go1.20

package middleware

import (
"bufio"
"net"
"net/http"
)

func responseControllerFlush(rw http.ResponseWriter) error {
return http.NewResponseController(rw).Flush()
}

func responseControllerHijack(rw http.ResponseWriter) (net.Conn, *bufio.ReadWriter, error) {
return http.NewResponseController(rw).Hijack()
}
File renamed without changes.
File renamed without changes.

0 comments on commit 2100345

Please sign in to comment.