Skip to content

Commit 518be1c

Browse files
committed
Fix bug in setting request body
Go documentation says `Use of GetBody still requires setting Body`. This change ensures the body is always set in addition to GetBody. This fixes a bug where sometimes the body is nil. Signed-off-by: Derek McGowan <[email protected]>
1 parent c90a3d4 commit 518be1c

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

remotes/docker/pusher.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,16 @@ func (p dockerPusher) Push(ctx context.Context, desc ocispec.Descriptor) (conten
230230

231231
pr, pw := io.Pipe()
232232
respC := make(chan *http.Response, 1)
233+
body := ioutil.NopCloser(pr)
233234

234235
req.body = func() (io.ReadCloser, error) {
235-
return ioutil.NopCloser(pr), nil
236+
if body == nil {
237+
return nil, errors.New("cannot reuse body, request must be retried")
238+
}
239+
// Only use the body once since pipe cannot be seeked
240+
ob := body
241+
body = nil
242+
return ob, nil
236243
}
237244
req.size = desc.Size
238245

remotes/docker/resolver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,11 @@ func (r *request) do(ctx context.Context) (*http.Response, error) {
495495
}
496496
req.Header = r.header
497497
if r.body != nil {
498+
body, err := r.body()
499+
if err != nil {
500+
return nil, err
501+
}
502+
req.Body = body
498503
req.GetBody = r.body
499504
if r.size > 0 {
500505
req.ContentLength = r.size

0 commit comments

Comments
 (0)