Skip to content

Commit

Permalink
bodyWriter now implements http.Flusher and http.Hijacker
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Jan 20, 2024
1 parent 24a5910 commit 26d6800
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion dump.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package slogecho

import (
"bufio"
"bytes"
"errors"
"io"
"net"
"net/http"
)

var _ http.ResponseWriter = (*bodyWriter)(nil)
var _ http.Flusher = (*bodyWriter)(nil)
var _ http.Hijacker = (*bodyWriter)(nil)

type bodyWriter struct {
http.ResponseWriter
body *bytes.Buffer
maxSize int
bytes int
}

// implements gin.ResponseWriter
// implements http.ResponseWriter
func (w bodyWriter) Write(b []byte) (int, error) {
if w.body != nil {
if w.body.Len()+len(b) > w.maxSize {
Expand All @@ -26,6 +33,22 @@ func (w bodyWriter) Write(b []byte) (int, error) {
return w.ResponseWriter.Write(b)
}

// implements http.Flusher
func (w bodyWriter) Flush() {
if w.ResponseWriter.(http.Flusher) != nil {
w.ResponseWriter.(http.Flusher).Flush()
}
}

// implements http.Hijacker
func (w bodyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if w.ResponseWriter.(http.Hijacker) != nil {
return w.ResponseWriter.(http.Hijacker).Hijack()
}

return nil, nil, errors.New("Hijack not supported")
}

func newBodyWriter(writer http.ResponseWriter, maxSize int, recordBody bool) *bodyWriter {
var body *bytes.Buffer
if recordBody {
Expand Down

0 comments on commit 26d6800

Please sign in to comment.