Skip to content

Commit

Permalink
martian/proxyutil: use http.NoBody
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed Oct 5, 2023
1 parent 5c12190 commit 737a653
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions internal/martian/proxyutil/proxyutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Package proxyutil provides functionality for building proxies.
package proxyutil

import (
"bytes"
"fmt"
"io"
"net/http"
Expand All @@ -32,26 +31,25 @@ import (
// If body is nil, an empty byte.Buffer will be provided to be consistent with
// the guarantees provided by http.Transport and http.Client.
func NewResponse(code int, body io.Reader, req *http.Request) *http.Response {
if body == nil {
body = &bytes.Buffer{}
}

rc, ok := body.(io.ReadCloser)
if !ok {
rc = io.NopCloser(body)
}

res := &http.Response{
StatusCode: code,
Status: fmt.Sprintf("%d %s", code, http.StatusText(code)),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{},
Body: rc,
Body: http.NoBody,
Request: req,
}

if body != nil {
rc, ok := body.(io.ReadCloser)
if !ok {
rc = io.NopCloser(body)
}
res.Body = rc
}

if req != nil {
res.Close = req.Close
res.Proto = req.Proto
Expand Down

0 comments on commit 737a653

Please sign in to comment.