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: use readers over closers in http input #11083

Merged
merged 2 commits into from
May 18, 2022
Merged
Changes from 1 commit
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
15 changes: 9 additions & 6 deletions plugins/inputs/http/http.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package http

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -95,9 +97,6 @@ func (h *HTTP) gatherURL(
if err != nil {
return err
}
if body != nil {
defer body.Close()
}
srebhan marked this conversation as resolved.
Show resolved Hide resolved

request, err := http.NewRequest(h.Method, url, body)
if err != nil {
Expand Down Expand Up @@ -175,7 +174,7 @@ func (h *HTTP) gatherURL(
return nil
}

func makeRequestBodyReader(contentEncoding, body string) (io.ReadCloser, error) {
func makeRequestBodyReader(contentEncoding, body string) (io.Reader, error) {
if body == "" {
return nil, nil
}
Expand All @@ -186,10 +185,14 @@ func makeRequestBodyReader(contentEncoding, body string) (io.ReadCloser, error)
if err != nil {
return nil, err
}
return rc, nil
data, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}

return io.NopCloser(reader), nil
return reader, nil
}

func init() {
Expand Down