@@ -417,44 +417,52 @@ void aws_s3_add_user_agent_header(struct aws_allocator *allocator, struct aws_ht
417417 aws_byte_buf_clean_up (& user_agent_buffer );
418418}
419419
420- int aws_s3_parse_content_range_response_header (
421- struct aws_allocator * allocator ,
422- struct aws_http_headers * response_headers ,
420+ int aws_s3_parse_content_range_cursor (
421+ struct aws_byte_cursor content_range_cursor ,
423422 uint64_t * out_range_start ,
424423 uint64_t * out_range_end ,
425424 uint64_t * out_object_size ) {
426- AWS_PRECONDITION (allocator );
427- AWS_PRECONDITION (response_headers );
428425
429- struct aws_byte_cursor content_range_header_value ;
426+ /* Expected Format of header is: "bytes StartByte-EndByte/TotalObjectSize" */
430427
431- if (aws_http_headers_get (response_headers , g_content_range_header_name , & content_range_header_value )) {
432- aws_raise_error (AWS_ERROR_S3_MISSING_CONTENT_RANGE_HEADER );
433- return AWS_OP_ERR ;
428+ /* Check if it starts with "bytes " */
429+ struct aws_byte_cursor bytes_prefix = aws_byte_cursor_from_c_str ("bytes " );
430+ if (!aws_byte_cursor_starts_with (& content_range_cursor , & bytes_prefix )) {
431+ return aws_raise_error (AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER );
434432 }
435433
436- int result = AWS_OP_ERR ;
434+ /* Skip past "bytes " */
435+ aws_byte_cursor_advance (& content_range_cursor , bytes_prefix .len );
436+
437+ /* Parse range start */
438+ struct aws_byte_cursor range_start_cursor ;
439+ if (!aws_byte_cursor_next_split (& content_range_cursor , '-' , & range_start_cursor )) {
440+ return aws_raise_error (AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER );
441+ }
437442
438443 uint64_t range_start = 0 ;
439- uint64_t range_end = 0 ;
440- uint64_t object_size = 0 ;
444+ if (aws_byte_cursor_utf8_parse_u64 (range_start_cursor , & range_start )) {
445+ return aws_raise_error (AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER );
446+ }
441447
442- struct aws_string * content_range_header_value_str =
443- aws_string_new_from_cursor (allocator , & content_range_header_value );
448+ /* Parse range end */
449+ struct aws_byte_cursor range_end_cursor ;
450+ if (!aws_byte_cursor_next_split (& content_range_cursor , '/' , & range_end_cursor )) {
451+ return aws_raise_error (AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER );
452+ }
444453
445- /* Expected Format of header is: "bytes StartByte-EndByte/TotalObjectSize" */
446- int num_fields_found = sscanf (
447- (const char * )content_range_header_value_str -> bytes ,
448- "bytes %" PRIu64 "-%" PRIu64 "/%" PRIu64 ,
449- & range_start ,
450- & range_end ,
451- & object_size );
454+ uint64_t range_end = 0 ;
455+ if (aws_byte_cursor_utf8_parse_u64 (range_end_cursor , & range_end )) {
456+ return aws_raise_error (AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER );
457+ }
452458
453- if (num_fields_found < 3 ) {
454- aws_raise_error (AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER );
455- goto clean_up ;
459+ /* Parse object size (remaining part) */
460+ uint64_t object_size = 0 ;
461+ if (aws_byte_cursor_utf8_parse_u64 (content_range_cursor , & object_size )) {
462+ return aws_raise_error (AWS_ERROR_S3_INVALID_CONTENT_RANGE_HEADER );
456463 }
457464
465+ /* Set output values */
458466 if (out_range_start != NULL ) {
459467 * out_range_start = range_start ;
460468 }
@@ -467,13 +475,23 @@ int aws_s3_parse_content_range_response_header(
467475 * out_object_size = object_size ;
468476 }
469477
470- result = AWS_OP_SUCCESS ;
478+ return AWS_OP_SUCCESS ;
479+ }
471480
472- clean_up :
473- aws_string_destroy (content_range_header_value_str );
474- content_range_header_value_str = NULL ;
481+ int aws_s3_parse_content_range_response_header (
482+ struct aws_http_headers * response_headers ,
483+ uint64_t * out_range_start ,
484+ uint64_t * out_range_end ,
485+ uint64_t * out_object_size ) {
486+ AWS_ERROR_PRECONDITION (response_headers );
487+ struct aws_byte_cursor content_range_header_value ;
475488
476- return result ;
489+ if (aws_http_headers_get (response_headers , g_content_range_header_name , & content_range_header_value )) {
490+ aws_raise_error (AWS_ERROR_S3_MISSING_CONTENT_RANGE_HEADER );
491+ return AWS_OP_ERR ;
492+ }
493+ return aws_s3_parse_content_range_cursor (
494+ content_range_header_value , out_range_start , out_range_end , out_object_size );
477495}
478496
479497int aws_s3_parse_content_length_response_header (
0 commit comments