| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/s3/private/s3_list_parts.h> |
| 7 |
#include <aws/s3/private/s3_paginator.h> |
| 8 |
#include <aws/s3/private/s3_util.h> |
| 9 |
|
| 10 |
#include <aws/common/ref_count.h> |
| 11 |
#include <aws/common/xml_parser.h> |
| 12 |
|
| 13 |
#include <aws/io/uri.h> |
| 14 |
|
| 15 |
#include <aws/http/http.h> |
| 16 |
#include <aws/http/request_response.h> |
| 17 |
|
| 18 |
struct aws_s3_operation_data { |
| 19 |
struct aws_allocator *allocator; |
| 20 |
|
| 21 |
struct aws_string *key; |
| 22 |
struct aws_string *upload_id; |
| 23 |
|
| 24 |
struct aws_ref_count ref_count; |
| 25 |
|
| 26 |
aws_s3_on_part_fn *on_part; |
| 27 |
|
| 28 |
void *user_data; |
| 29 |
}; |
| 30 |
|
| 31 |
static void s_ref_count_zero_callback(void *arg) { |
| 32 |
struct aws_s3_operation_data *operation_data = arg; |
| 33 |
|
| 34 |
if (operation_data->key) { |
| 35 |
aws_string_destroy(operation_data->key); |
| 36 |
} |
| 37 |
|
| 38 |
if (operation_data->upload_id) { |
| 39 |
aws_string_destroy(operation_data->upload_id); |
| 40 |
} |
| 41 |
|
| 42 |
aws_mem_release(operation_data->allocator, operation_data); |
| 43 |
} |
| 44 |
|
| 45 |
static void s_on_paginator_cleanup(void *user_data) { |
| 46 |
struct aws_s3_operation_data *operation_data = user_data; |
| 47 |
|
| 48 |
aws_ref_count_release(&operation_data->ref_count); |
| 49 |
} |
| 50 |
|
| 51 |
struct result_wrapper { |
| 52 |
struct aws_allocator *allocator; |
| 53 |
struct aws_s3_part_info part_info; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
static int s_xml_on_Part_child(struct aws_xml_node *node, void *user_data) { |
| 58 |
struct result_wrapper *result_wrapper = user_data; |
| 59 |
struct aws_s3_part_info *part_info = &result_wrapper->part_info; |
| 60 |
|
| 61 |
|
| 62 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 63 |
|
| 64 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ETag")) { |
| 65 |
return aws_xml_node_as_body(node, &part_info->e_tag); |
| 66 |
} |
| 67 |
|
| 68 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "LastModified")) { |
| 69 |
struct aws_byte_cursor date_cur; |
| 70 |
if (aws_xml_node_as_body(node, &date_cur) != AWS_OP_SUCCESS) { |
| 71 |
return AWS_OP_ERR; |
| 72 |
} |
| 73 |
|
| 74 |
if (aws_date_time_init_from_str_cursor(&part_info->last_modified, &date_cur, AWS_DATE_FORMAT_ISO_8601)) { |
| 75 |
return AWS_OP_ERR; |
| 76 |
} |
| 77 |
|
| 78 |
return AWS_OP_SUCCESS; |
| 79 |
} |
| 80 |
|
| 81 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Size")) { |
| 82 |
struct aws_byte_cursor size_cur; |
| 83 |
|
| 84 |
if (aws_xml_node_as_body(node, &size_cur) != AWS_OP_SUCCESS) { |
| 85 |
return AWS_OP_ERR; |
| 86 |
} |
| 87 |
if (aws_byte_cursor_utf8_parse_u64(size_cur, &part_info->size) != AWS_OP_SUCCESS) { |
| 88 |
return AWS_OP_ERR; |
| 89 |
} |
| 90 |
return AWS_OP_SUCCESS; |
| 91 |
} |
| 92 |
|
| 93 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "PartNumber")) { |
| 94 |
struct aws_byte_cursor part_number_cur; |
| 95 |
|
| 96 |
if (aws_xml_node_as_body(node, &part_number_cur) != AWS_OP_SUCCESS) { |
| 97 |
return AWS_OP_ERR; |
| 98 |
} |
| 99 |
|
| 100 |
uint64_t part_number = 0; |
| 101 |
if (aws_byte_cursor_utf8_parse_u64(part_number_cur, &part_number) != AWS_OP_SUCCESS) { |
| 102 |
return AWS_OP_ERR; |
| 103 |
} |
| 104 |
if (part_number > UINT32_MAX) { |
| 105 |
return aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED); |
| 106 |
} |
| 107 |
part_info->part_number = (uint32_t)part_number; |
| 108 |
return AWS_OP_SUCCESS; |
| 109 |
} |
| 110 |
|
| 111 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumCRC32")) { |
| 112 |
return aws_xml_node_as_body(node, &part_info->checksumCRC32); |
| 113 |
} |
| 114 |
|
| 115 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumCRC32C")) { |
| 116 |
return aws_xml_node_as_body(node, &part_info->checksumCRC32C); |
| 117 |
} |
| 118 |
|
| 119 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumSHA1")) { |
| 120 |
return aws_xml_node_as_body(node, &part_info->checksumSHA1); |
| 121 |
} |
| 122 |
|
| 123 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumSHA256")) { |
| 124 |
return aws_xml_node_as_body(node, &part_info->checksumSHA256); |
| 125 |
} |
| 126 |
|
| 127 |
return AWS_OP_SUCCESS; |
| 128 |
} |
| 129 |
|
| 130 |
static int s_xml_on_ListPartsResult_child(struct aws_xml_node *node, void *user_data) { |
| 131 |
struct aws_s3_operation_data *operation_data = user_data; |
| 132 |
|
| 133 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 134 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Part")) { |
| 135 |
struct result_wrapper result_wrapper = { |
| 136 |
.allocator = operation_data->allocator, |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if (aws_xml_node_traverse(node, s_xml_on_Part_child, &result_wrapper) != AWS_OP_SUCCESS) { |
| 142 |
return AWS_OP_ERR; |
| 143 |
} |
| 144 |
|
| 145 |
struct aws_byte_buf trimmed_etag; |
| 146 |
aws_byte_buf_init(&trimmed_etag, result_wrapper.allocator, 20); |
| 147 |
|
| 148 |
if (aws_byte_buf_append_unescaped_xml( |
| 149 |
result_wrapper.allocator, result_wrapper.part_info.e_tag, &trimmed_etag) == AWS_OP_SUCCESS) { |
| 150 |
result_wrapper.part_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag); |
| 151 |
} |
| 152 |
|
| 153 |
int ret_val = AWS_OP_SUCCESS; |
| 154 |
if (operation_data->on_part) { |
| 155 |
ret_val = operation_data->on_part(&result_wrapper.part_info, operation_data->user_data); |
| 156 |
} |
| 157 |
|
| 158 |
aws_byte_buf_clean_up(&trimmed_etag); |
| 159 |
|
| 160 |
return ret_val; |
| 161 |
} |
| 162 |
|
| 163 |
return AWS_OP_SUCCESS; |
| 164 |
} |
| 165 |
|
| 166 |
static int s_construct_next_request_http_message( |
| 167 |
struct aws_byte_cursor *continuation_token, |
| 168 |
void *user_data, |
| 169 |
struct aws_http_message **out_message) { |
| 170 |
|
| 171 |
AWS_PRECONDITION(user_data); |
| 172 |
|
| 173 |
struct aws_s3_operation_data *operation_data = user_data; |
| 174 |
|
| 175 |
struct aws_byte_buf request_path; |
| 176 |
struct aws_byte_cursor key_val = aws_byte_cursor_from_string(operation_data->key); |
| 177 |
aws_byte_buf_init_copy_from_cursor(&request_path, operation_data->allocator, key_val); |
| 178 |
|
| 179 |
if (operation_data->upload_id) { |
| 180 |
struct aws_byte_cursor upload_id = aws_byte_cursor_from_c_str("?uploadId="); |
| 181 |
aws_byte_buf_append_dynamic(&request_path, &upload_id); |
| 182 |
struct aws_byte_cursor upload_id_val = aws_byte_cursor_from_string(operation_data->upload_id); |
| 183 |
aws_byte_buf_append_dynamic(&request_path, &upload_id_val); |
| 184 |
} |
| 185 |
|
| 186 |
if (continuation_token) { |
| 187 |
struct aws_byte_cursor continuation = aws_byte_cursor_from_c_str("&part-number-marker="); |
| 188 |
aws_byte_buf_append_dynamic(&request_path, &continuation); |
| 189 |
aws_byte_buf_append_encoding_uri_param(&request_path, continuation_token); |
| 190 |
} |
| 191 |
|
| 192 |
struct aws_http_message *list_parts_request = aws_http_message_new_request(operation_data->allocator); |
| 193 |
aws_http_message_set_request_path(list_parts_request, aws_byte_cursor_from_buf(&request_path)); |
| 194 |
|
| 195 |
aws_byte_buf_clean_up(&request_path); |
| 196 |
|
| 197 |
struct aws_http_header accept_header = { |
| 198 |
.name = aws_byte_cursor_from_c_str("accept"), |
| 199 |
.value = aws_byte_cursor_from_c_str("application/xml"), |
| 200 |
}; |
| 201 |
|
| 202 |
aws_http_message_add_header(list_parts_request, accept_header); |
| 203 |
|
| 204 |
aws_http_message_set_request_method(list_parts_request, aws_http_method_get); |
| 205 |
|
| 206 |
*out_message = list_parts_request; |
| 207 |
return AWS_OP_SUCCESS; |
| 208 |
} |
| 209 |
|
| 210 |
struct aws_s3_paginated_operation *aws_s3_list_parts_operation_new( |
| 211 |
struct aws_allocator *allocator, |
| 212 |
const struct aws_s3_list_parts_params *params) { |
| 213 |
AWS_FATAL_PRECONDITION(params); |
| 214 |
AWS_FATAL_PRECONDITION(params->key.len); |
| 215 |
AWS_FATAL_PRECONDITION(params->upload_id.len); |
| 216 |
|
| 217 |
struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data)); |
| 218 |
operation_data->allocator = allocator; |
| 219 |
operation_data->key = aws_string_new_from_cursor(allocator, ¶ms->key); |
| 220 |
operation_data->upload_id = aws_string_new_from_cursor(allocator, ¶ms->upload_id); |
| 221 |
operation_data->on_part = params->on_part; |
| 222 |
operation_data->user_data = params->user_data; |
| 223 |
|
| 224 |
aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback); |
| 225 |
|
| 226 |
struct aws_s3_paginated_operation_params operation_params = { |
| 227 |
.operation_name = aws_byte_cursor_from_c_str("ListParts"), |
| 228 |
.next_message = s_construct_next_request_http_message, |
| 229 |
.on_result_node_encountered_fn = s_xml_on_ListPartsResult_child, |
| 230 |
.on_paginated_operation_cleanup = s_on_paginator_cleanup, |
| 231 |
.result_xml_node_name = aws_byte_cursor_from_c_str("ListPartsResult"), |
| 232 |
.continuation_token_node_name = aws_byte_cursor_from_c_str("NextPartNumberMarker"), |
| 233 |
.user_data = operation_data, |
| 234 |
}; |
| 235 |
|
| 236 |
struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params); |
| 237 |
|
| 238 |
return operation; |
| 239 |
} |