1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/s3/private/s3_list_parts.h>
#include <aws/s3/private/s3_paginator.h>
#include <aws/s3/private/s3_util.h>
#include <aws/common/ref_count.h>
#include <aws/common/xml_parser.h>
#include <aws/io/uri.h>
#include <aws/http/http.h>
#include <aws/http/request_response.h>
struct aws_s3_operation_data {
struct aws_allocator *allocator;
struct aws_string *key;
struct aws_string *upload_id;
struct aws_ref_count ref_count;
aws_s3_on_part_fn *on_part;
void *user_data;
};
static void s_ref_count_zero_callback(void *arg) {
struct aws_s3_operation_data *operation_data = arg;
if (operation_data->key) {
aws_string_destroy(operation_data->key);
}
if (operation_data->upload_id) {
aws_string_destroy(operation_data->upload_id);
}
aws_mem_release(operation_data->allocator, operation_data);
}
static void s_on_paginator_cleanup(void *user_data) {
struct aws_s3_operation_data *operation_data = user_data;
aws_ref_count_release(&operation_data->ref_count);
}
struct result_wrapper {
struct aws_allocator *allocator;
struct aws_s3_part_info part_info;
};
/* invoked when the ListPartResult/Parts node is iterated. */
static bool s_on_parts_node(struct aws_xml_parser *parser, struct aws_xml_node *node, void *user_data) {
struct result_wrapper *result_wrapper = user_data;
struct aws_s3_part_info *part_info = &result_wrapper->part_info;
/* for each Parts node, get the info from it and send it off as an part we've encountered */
struct aws_byte_cursor node_name;
aws_xml_node_get_name(node, &node_name);
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ETag")) {
return aws_xml_node_as_body(parser, node, &part_info->e_tag) == AWS_OP_SUCCESS;
}
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "LastModified")) {
struct aws_byte_cursor date_cur;
if (aws_xml_node_as_body(parser, node, &date_cur) == AWS_OP_SUCCESS) {
aws_date_time_init_from_str_cursor(&part_info->last_modified, &date_cur, AWS_DATE_FORMAT_ISO_8601);
return true;
}
return false;
}
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Size")) {
struct aws_byte_cursor size_cur;
if (aws_xml_node_as_body(parser, node, &size_cur) == AWS_OP_SUCCESS) {
if (aws_byte_cursor_utf8_parse_u64(size_cur, &part_info->size)) {
return false;
}
return true;
}
}
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "PartNumber")) {
struct aws_byte_cursor part_number_cur;
if (aws_xml_node_as_body(parser, node, &part_number_cur) == AWS_OP_SUCCESS) {
uint64_t part_number = 0;
if (aws_byte_cursor_utf8_parse_u64(part_number_cur, &part_number)) {
return false;
}
if (part_number > UINT32_MAX) {
aws_raise_error(AWS_ERROR_OVERFLOW_DETECTED);
return false;
}
part_info->part_number = (uint32_t)part_number;
return true;
}
}
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumCRC32")) {
return aws_xml_node_as_body(parser, node, &part_info->checksumCRC32) == AWS_OP_SUCCESS;
}
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumCRC32C")) {
return aws_xml_node_as_body(parser, node, &part_info->checksumCRC32C) == AWS_OP_SUCCESS;
}
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumSHA1")) {
return aws_xml_node_as_body(parser, node, &part_info->checksumSHA1) == AWS_OP_SUCCESS;
}
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "ChecksumSHA256")) {
return aws_xml_node_as_body(parser, node, &part_info->checksumSHA256) == AWS_OP_SUCCESS;
}
return true;
}
static bool s_on_list_bucket_result_node_encountered(
struct aws_xml_parser *parser,
struct aws_xml_node *node,
void *user_data) {
struct aws_s3_operation_data *operation_data = user_data;
struct aws_byte_cursor node_name;
aws_xml_node_get_name(node, &node_name);
struct result_wrapper result_wrapper;
AWS_ZERO_STRUCT(result_wrapper);
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Part")) {
result_wrapper.allocator = operation_data->allocator;
/* this will traverse the current Parts node, get the metadata necessary to construct
* an instance of fs_info so we can invoke the callback on it. This happens once per part. */
bool ret_val = aws_xml_node_traverse(parser, node, s_on_parts_node, &result_wrapper) == AWS_OP_SUCCESS;
struct aws_byte_buf trimmed_etag;
AWS_ZERO_STRUCT(trimmed_etag);
if (result_wrapper.part_info.e_tag.len) {
struct aws_string *quoted_etag_str =
aws_string_new_from_cursor(result_wrapper.allocator, &result_wrapper.part_info.e_tag);
replace_quote_entities(result_wrapper.allocator, quoted_etag_str, &trimmed_etag);
result_wrapper.part_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag);
aws_string_destroy(quoted_etag_str);
}
if (ret_val && operation_data->on_part) {
ret_val |= operation_data->on_part(&result_wrapper.part_info, operation_data->user_data);
}
if (trimmed_etag.len) {
aws_byte_buf_clean_up(&trimmed_etag);
}
return ret_val;
}
return true;
}
static int s_construct_next_request_http_message(
struct aws_byte_cursor *continuation_token,
void *user_data,
struct aws_http_message **out_message) {
AWS_PRECONDITION(user_data);
struct aws_s3_operation_data *operation_data = user_data;
struct aws_byte_buf request_path;
struct aws_byte_cursor key_val = aws_byte_cursor_from_string(operation_data->key);
aws_byte_buf_init_copy_from_cursor(&request_path, operation_data->allocator, key_val);
if (operation_data->upload_id) {
struct aws_byte_cursor upload_id = aws_byte_cursor_from_c_str("?uploadId=");
aws_byte_buf_append_dynamic(&request_path, &upload_id);
struct aws_byte_cursor upload_id_val = aws_byte_cursor_from_string(operation_data->upload_id);
aws_byte_buf_append_dynamic(&request_path, &upload_id_val);
}
if (continuation_token) {
struct aws_byte_cursor continuation = aws_byte_cursor_from_c_str("&part-number-marker=");
aws_byte_buf_append_dynamic(&request_path, &continuation);
aws_byte_buf_append_encoding_uri_param(&request_path, continuation_token);
}
struct aws_http_message *list_parts_request = aws_http_message_new_request(operation_data->allocator);
aws_http_message_set_request_path(list_parts_request, aws_byte_cursor_from_buf(&request_path));
aws_byte_buf_clean_up(&request_path);
struct aws_http_header accept_header = {
.name = aws_byte_cursor_from_c_str("accept"),
.value = aws_byte_cursor_from_c_str("application/xml"),
};
aws_http_message_add_header(list_parts_request, accept_header);
aws_http_message_set_request_method(list_parts_request, aws_http_method_get);
*out_message = list_parts_request;
return AWS_OP_SUCCESS;
}
struct aws_s3_paginator *aws_s3_initiate_list_parts(
struct aws_allocator *allocator,
const struct aws_s3_list_parts_params *params) {
AWS_FATAL_PRECONDITION(params);
AWS_FATAL_PRECONDITION(params->client);
AWS_FATAL_PRECONDITION(params->bucket_name.len);
AWS_FATAL_PRECONDITION(params->key.len);
AWS_FATAL_PRECONDITION(params->upload_id.len);
AWS_FATAL_PRECONDITION(params->endpoint.len);
struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data));
operation_data->allocator = allocator;
operation_data->key = aws_string_new_from_cursor(allocator, ¶ms->key);
operation_data->upload_id = aws_string_new_from_cursor(allocator, ¶ms->upload_id);
operation_data->on_part = params->on_part;
operation_data->user_data = params->user_data;
aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback);
struct aws_byte_cursor xml_result_node_name = aws_byte_cursor_from_c_str("ListPartsResult");
const struct aws_byte_cursor continuation_node_name = aws_byte_cursor_from_c_str("NextPartNumberMarker");
struct aws_s3_paginated_operation_params operation_params = {
.next_message = s_construct_next_request_http_message,
.on_result_node_encountered_fn = s_on_list_bucket_result_node_encountered,
.on_paginated_operation_cleanup = s_on_paginator_cleanup,
.result_xml_node_name = &xml_result_node_name,
.continuation_token_node_name = &continuation_node_name,
.user_data = operation_data,
};
struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params);
struct aws_s3_paginator_params paginator_params = {
.client = params->client,
.bucket_name = params->bucket_name,
.endpoint = params->endpoint,
.operation = operation,
.on_page_finished_fn = params->on_list_finished,
.user_data = params->user_data,
};
struct aws_s3_paginator *paginator = aws_s3_initiate_paginator(allocator, &paginator_params);
// transfer control to paginator
aws_s3_paginated_operation_release(operation);
return paginator;
}
struct aws_s3_paginated_operation *aws_s3_list_parts_operation_new(
struct aws_allocator *allocator,
const struct aws_s3_list_parts_params *params) {
AWS_FATAL_PRECONDITION(params);
AWS_FATAL_PRECONDITION(params->key.len);
AWS_FATAL_PRECONDITION(params->upload_id.len);
struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data));
operation_data->allocator = allocator;
operation_data->key = aws_string_new_from_cursor(allocator, ¶ms->key);
operation_data->upload_id = aws_string_new_from_cursor(allocator, ¶ms->upload_id);
operation_data->on_part = params->on_part;
operation_data->user_data = params->user_data;
aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback);
struct aws_byte_cursor xml_result_node_name = aws_byte_cursor_from_c_str("ListPartsResult");
const struct aws_byte_cursor continuation_node_name = aws_byte_cursor_from_c_str("NextPartNumberMarker");
struct aws_s3_paginated_operation_params operation_params = {
.next_message = s_construct_next_request_http_message,
.on_result_node_encountered_fn = s_on_list_bucket_result_node_encountered,
.on_paginated_operation_cleanup = s_on_paginator_cleanup,
.result_xml_node_name = &xml_result_node_name,
.continuation_token_node_name = &continuation_node_name,
.user_data = operation_data,
};
struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params);
return operation;
}
|