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 panic resulting from empty byte reader #164

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
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
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