11import { Router } from "itty-router" ;
22import { BlobUnknownError , ManifestUnknownError } from "./v2-errors" ;
33import { InternalError , ServerError } from "./errors" ;
4- import { errorString , jsonHeaders , wrap } from "./utils" ;
4+ import { errorString , getStreamSize , jsonHeaders , wrap } from "./utils" ;
55import { hexToDigest } from "./user" ;
66import { ManifestTagsListTooBigError } from "./v2-responses" ;
77import { Env } from ".." ;
@@ -418,19 +418,15 @@ v2Router.get("/:name+/blobs/uploads/:uuid", async (req, env: Env) => {
418418v2Router . patch ( "/:name+/blobs/uploads/:uuid" , async ( req , env : Env ) => {
419419 const { name, uuid } = req . params ;
420420 const contentRange = req . headers . get ( "Content-Range" ) ;
421- const [ start , end ] = contentRange ?. split ( "-" ) ?? [ undefined , undefined ] ;
421+ const rangeMatch = contentRange ?. match ( / (?: b y t e s \s + ) ? ( \d + ) - ( \d + ) / ) ;
422+ const [ start , end ] = rangeMatch ? [ rangeMatch [ 1 ] , rangeMatch [ 2 ] ] : [ undefined , undefined ] ;
422423
423424 if ( req . body == null ) {
424425 return new Response ( null , { status : 400 } ) ;
425426 }
426427
427- let contentLengthString = req . headers . get ( "Content-Length" ) ;
428- let stream = req . body ;
429- if ( ! contentLengthString ) {
430- const blob = await req . blob ( ) ;
431- contentLengthString = `${ blob . size } ` ;
432- stream = blob . stream ( ) ;
433- }
428+ const streamSize = getStreamSize ( req . headers ) ;
429+ const stream = req . body ;
434430
435431 const url = new URL ( req . url ) ;
436432 const [ res , err ] = await wrap < UploadObject | RegistryError , Error > (
@@ -439,7 +435,7 @@ v2Router.patch("/:name+/blobs/uploads/:uuid", async (req, env: Env) => {
439435 uuid ,
440436 url . pathname + "?" + url . searchParams . toString ( ) ,
441437 stream ,
442- + contentLengthString ,
438+ streamSize ,
443439 end !== undefined && start !== undefined ? [ + start , + end ] : undefined ,
444440 ) ,
445441 ) ;
@@ -457,8 +453,7 @@ v2Router.patch("/:name+/blobs/uploads/:uuid", async (req, env: Env) => {
457453 status : 202 ,
458454 headers : {
459455 "Location" : res . location ,
460- // Note that the HTTP Range header byte ranges are inclusive and that will be honored, even in non-standard use cases.
461- "Range" : `${ res . range . join ( "-" ) } ` ,
456+ "Range" : `0-${ res . range [ 1 ] } ` , // Ensure correct Range format (0-N)
462457 "Docker-Upload-UUID" : res . id ,
463458 } ,
464459 } ) ;
0 commit comments