Skip to content

Commit

Permalink
Fix panic resulting from empty byte reader (#164)
Browse files Browse the repository at this point in the history
Always use nil client request body when GET requests have no body.
  • Loading branch information
gammazero authored Sep 19, 2023
1 parent da68a89 commit 9fd82c3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
4 changes: 2 additions & 2 deletions delegated_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
unknownSchema = unknownProtocol
)

type findFunc func(ctx context.Context, method, source string, req *url.URL, body []byte, encrypted bool) (int, []byte)
type findFunc func(ctx context.Context, method, source string, req *url.URL, encrypted bool) (int, []byte)

func NewDelegatedTranslator(backend findFunc) (http.Handler, error) {
finder := delegatedTranslator{backend}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (dt *delegatedTranslator) find(w http.ResponseWriter, r *http.Request, encr

// Translate URL by mapping `/providers/{CID}` to `/cid/{CID}`.
uri := r.URL.JoinPath("../cid", cidUrlParam)
rcode, resp := dt.be(r.Context(), http.MethodGet, findMethodDelegated, uri, []byte{}, encrypted)
rcode, resp := dt.be(r.Context(), http.MethodGet, findMethodDelegated, uri, encrypted)
if rcode != http.StatusOK {
http.Error(w, "", rcode)
return
Expand Down
10 changes: 3 additions & 7 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (s *server) find(w http.ResponseWriter, r *http.Request, mh multihash.Multi
}
// In a case where the request has no `Accept` header at all, be forgiving and respond with
// JSON.
rcode, resp := s.doFind(r.Context(), r.Method, findMethodOrig, r.URL, nil, encrypted)
rcode, resp := s.doFind(r.Context(), r.Method, findMethodOrig, r.URL, encrypted)
if rcode != http.StatusOK {
http.Error(w, "", rcode)
return
Expand All @@ -182,7 +182,7 @@ func (s *server) find(w http.ResponseWriter, r *http.Request, mh multihash.Multi
}
}

func (s *server) doFind(ctx context.Context, method, source string, req *url.URL, body []byte, encrypted bool) (int, []byte) {
func (s *server) doFind(ctx context.Context, method, source string, req *url.URL, encrypted bool) (int, []byte) {
start := time.Now()
latencyTags := []tag.Mutator{tag.Insert(metrics.Method, method)}
loadTags := []tag.Mutator{tag.Insert(metrics.Method, source)}
Expand Down Expand Up @@ -225,11 +225,7 @@ func (s *server) doFind(ctx context.Context, method, source string, req *url.URL
endpoint.Scheme = b.URL().Scheme
log := log.With("backend", endpoint.Host)

var bodyReader *bytes.Reader
if len(body) != 0 {
bodyReader = bytes.NewReader(body)
}
req, err := http.NewRequestWithContext(cctx, method, endpoint.String(), bodyReader)
req, err := http.NewRequestWithContext(cctx, method, endpoint.String(), nil)
if err != nil {
log.Warnw("Failed to construct backend query", "err", err)
return nil, err
Expand Down

0 comments on commit 9fd82c3

Please sign in to comment.