Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case of nil/http.NoBody request.Body on interceptor tripperware/middleware #45

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the reviewers :

  • We now return an io.ReadCloser interface instead of a concrete copyReadCloser.
  • When the request body is nil, the Interceptor middleware/tripperware segfaults as it tries to read a nil body without checking it.

// 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