Skip to content

Commit e0f3b30

Browse files
authored
Fix container range bug (#34795)
Fix #34792 and add new tests
1 parent 719b151 commit e0f3b30

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

routers/api/packages/container/container.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func PostBlobsUploads(ctx *context.Context) {
320320

321321
// https://github.com/opencontainers/distribution-spec/blob/main/spec.md#pushing-a-blob-in-chunks
322322
func GetBlobsUpload(ctx *context.Context) {
323+
image := ctx.PathParam("image")
323324
uuid := ctx.PathParam("uuid")
324325

325326
upload, err := packages_model.GetBlobUploadByID(ctx, uuid)
@@ -334,6 +335,7 @@ func GetBlobsUpload(ctx *context.Context) {
334335

335336
// FIXME: undefined behavior when the uploaded content is empty: https://github.com/opencontainers/distribution-spec/issues/578
336337
respHeaders := &containerHeaders{
338+
Location: fmt.Sprintf("/v2/%s/%s/blobs/uploads/%s", ctx.Package.Owner.LowerName, image, upload.ID),
337339
UploadUUID: upload.ID,
338340
Status: http.StatusNoContent,
339341
}
@@ -386,7 +388,7 @@ func PatchBlobsUpload(ctx *context.Context) {
386388
UploadUUID: uploader.ID,
387389
Status: http.StatusAccepted,
388390
}
389-
if contentRange != "" {
391+
if uploader.Size() > 0 {
390392
respHeaders.Range = fmt.Sprintf("0-%d", uploader.Size()-1)
391393
}
392394
setResponseHeaders(ctx.Resp, respHeaders)

tests/integration/api_packages_container_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,22 @@ func TestPackageContainer(t *testing.T) {
297297
SetHeader("Content-Range", "1-10")
298298
MakeRequest(t, req, http.StatusRequestedRangeNotSatisfiable)
299299

300-
contentRange := fmt.Sprintf("0-%d", len(blobContent)-1)
301-
req.SetHeader("Content-Range", contentRange)
300+
// first patch without Content-Range
301+
req = NewRequestWithBody(t, "PATCH", setting.AppURL+uploadURL[1:], bytes.NewReader(blobContent[:1])).
302+
AddTokenAuth(userToken)
303+
resp = MakeRequest(t, req, http.StatusAccepted)
304+
assert.NotEmpty(t, resp.Header().Get("Location"))
305+
assert.Equal(t, "0-0", resp.Header().Get("Range"))
306+
307+
// then send remaining content with Content-Range
308+
req = NewRequestWithBody(t, "PATCH", setting.AppURL+uploadURL[1:], bytes.NewReader(blobContent[1:])).
309+
SetHeader("Content-Range", fmt.Sprintf("1-%d", len(blobContent)-1)).
310+
AddTokenAuth(userToken)
302311
resp = MakeRequest(t, req, http.StatusAccepted)
303312

313+
contentRange := fmt.Sprintf("0-%d", len(blobContent)-1)
304314
assert.Equal(t, uuid, resp.Header().Get("Docker-Upload-Uuid"))
315+
assert.NotEmpty(t, resp.Header().Get("Location"))
305316
assert.Equal(t, contentRange, resp.Header().Get("Range"))
306317

307318
uploadURL = resp.Header().Get("Location")
@@ -311,6 +322,7 @@ func TestPackageContainer(t *testing.T) {
311322
resp = MakeRequest(t, req, http.StatusNoContent)
312323

313324
assert.Equal(t, uuid, resp.Header().Get("Docker-Upload-Uuid"))
325+
assert.Equal(t, uploadURL, resp.Header().Get("Location"))
314326
assert.Equal(t, contentRange, resp.Header().Get("Range"))
315327

316328
pbu, err = packages_model.GetBlobUploadByID(db.DefaultContext, uuid)

0 commit comments

Comments
 (0)