|
10 | 10 | #include <aws/common/byte_buf.h> |
11 | 11 | #include <aws/common/encoding.h> |
12 | 12 | #include <aws/common/string.h> |
| 13 | +#include <aws/common/uri.h> |
13 | 14 | #include <aws/http/request_response.h> |
14 | 15 | #include <aws/io/async_stream.h> |
15 | 16 | #include <aws/io/stream.h> |
@@ -457,30 +458,63 @@ static const struct aws_byte_cursor s_slash_char = AWS_BYTE_CUR_INIT_FROM_STRING |
457 | 458 | */ |
458 | 459 | struct aws_http_message *aws_s3_get_source_object_size_message_new( |
459 | 460 | struct aws_allocator *allocator, |
460 | | - struct aws_http_message *base_message) { |
461 | | - struct aws_http_message *message = NULL; |
| 461 | + struct aws_http_message *base_message, |
| 462 | + struct aws_uri *source_uri) { |
| 463 | + |
| 464 | + struct aws_http_message *message = aws_http_message_new_request(allocator); |
462 | 465 | struct aws_byte_buf head_object_host_header; |
463 | 466 | AWS_ZERO_STRUCT(head_object_host_header); |
464 | 467 |
|
| 468 | + if (message == NULL) { |
| 469 | + goto error_cleanup; |
| 470 | + } |
| 471 | + |
| 472 | + if (aws_http_message_set_request_method(message, g_head_method)) { |
| 473 | + goto error_cleanup; |
| 474 | + } |
| 475 | + if (source_uri != NULL && source_uri->self_size > 0) { |
| 476 | + /* Parse source host header and path from the provided URI */ |
| 477 | + struct aws_byte_cursor host = *aws_uri_host_name(source_uri); |
| 478 | + struct aws_byte_cursor path = *aws_uri_path(source_uri); |
| 479 | + if (host.len == 0 || path.len == 0) { |
| 480 | + aws_raise_error(AWS_ERROR_INVALID_ARGUMENT); |
| 481 | + goto error_cleanup; |
| 482 | + } |
| 483 | + struct aws_http_header host_header = { |
| 484 | + .name = g_host_header_name, |
| 485 | + .value = host, |
| 486 | + }; |
| 487 | + if (aws_http_message_add_header(message, host_header)) { |
| 488 | + goto error_cleanup; |
| 489 | + } |
| 490 | + |
| 491 | + if (aws_http_message_set_request_path(message, path)) { |
| 492 | + goto error_cleanup; |
| 493 | + } |
| 494 | + return message; |
| 495 | + } |
| 496 | + |
| 497 | + /* Parse the source host header and path from the x-amz-copy-source header and the destination URI */ |
| 498 | + |
465 | 499 | AWS_PRECONDITION(allocator); |
466 | 500 |
|
467 | 501 | /* Find the x-amz-copy-source header, to extract source bucket/key information. */ |
468 | 502 | struct aws_http_headers *headers = aws_http_message_get_headers(base_message); |
469 | 503 | if (!headers) { |
470 | 504 | AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "CopyRequest is missing headers"); |
471 | | - return NULL; |
| 505 | + goto error_cleanup; |
472 | 506 | } |
473 | 507 |
|
474 | 508 | struct aws_byte_cursor source_header; |
475 | 509 | const struct aws_byte_cursor copy_source_header = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("x-amz-copy-source"); |
476 | 510 | if (aws_http_headers_get(headers, copy_source_header, &source_header) != AWS_OP_SUCCESS) { |
477 | 511 | AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "CopyRequest is missing the x-amz-copy-source header"); |
478 | | - return NULL; |
| 512 | + goto error_cleanup; |
479 | 513 | } |
480 | 514 | struct aws_byte_cursor host; |
481 | 515 | if (aws_http_headers_get(headers, g_host_header_name, &host) != AWS_OP_SUCCESS) { |
482 | 516 | AWS_LOGF_ERROR(AWS_LS_S3_GENERAL, "CopyRequest is missing the Host header"); |
483 | | - return NULL; |
| 517 | + goto error_cleanup; |
484 | 518 | } |
485 | 519 |
|
486 | 520 | struct aws_byte_cursor request_path = source_header; |
@@ -529,15 +563,6 @@ struct aws_http_message *aws_s3_get_source_object_size_message_new( |
529 | 563 | goto error_cleanup; |
530 | 564 | } |
531 | 565 |
|
532 | | - message = aws_http_message_new_request(allocator); |
533 | | - if (message == NULL) { |
534 | | - goto error_cleanup; |
535 | | - } |
536 | | - |
537 | | - if (aws_http_message_set_request_method(message, g_head_method)) { |
538 | | - goto error_cleanup; |
539 | | - } |
540 | | - |
541 | 566 | struct aws_http_header host_header = { |
542 | 567 | .name = g_host_header_name, |
543 | 568 | .value = aws_byte_cursor_from_buf(&head_object_host_header), |
|
0 commit comments