Skip to content

Commit

Permalink
Fix case of nil/http.NoBody request.Body on interceptor tripperware/m…
Browse files Browse the repository at this point in the history
…iddleware (#45)

* no copy if src is nil on NewCopyReadCloser

* Update copy_read_closer.go
  • Loading branch information
tle-dieu authored Jan 18, 2024
1 parent 34ad78a commit 1fa12b4
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions interceptor/copy_read_closer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"io/ioutil"
"net/http"
)

// io.Reader with Read method reset offset when EOF
Expand Down Expand Up @@ -42,7 +43,12 @@ type copyReadCloser struct {
// Second read after EOF
// copyBuffered --> copy BufReader simple buffer with fix size
// when BufReader is EOF offset is reset to read again
func NewCopyReadCloser(src io.ReadCloser) *copyReadCloser {
func NewCopyReadCloser(src io.ReadCloser) io.ReadCloser {
// No copying needed on nil or http.NoBody.
if src == nil || src == http.NoBody {
return src
}

buf := &bytes.Buffer{}
tr := &copyReadCloser{
copyTemp: buf,
Expand All @@ -56,7 +62,7 @@ func NewCopyReadCloser(src io.ReadCloser) *copyReadCloser {
return tr
}

func (tr *copyReadCloser)Read(p []byte) (n int, err error) {
func (tr *copyReadCloser) Read(p []byte) (n int, err error) {
n, err = tr.ReadCloser.Read(p)
if err == io.EOF {
if tr.copy == nil {
Expand Down

0 comments on commit 1fa12b4

Please sign in to comment.