| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/s3/private/s3_list_objects.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 *prefix; |
| 22 |
struct aws_string *delimiter; |
| 23 |
|
| 24 |
struct aws_ref_count ref_count; |
| 25 |
|
| 26 |
aws_s3_on_object_fn *on_object; |
| 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->delimiter) { |
| 35 |
aws_string_destroy(operation_data->delimiter); |
| 36 |
} |
| 37 |
|
| 38 |
if (operation_data->prefix) { |
| 39 |
aws_string_destroy(operation_data->prefix); |
| 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 fs_parser_wrapper { |
| 52 |
struct aws_allocator *allocator; |
| 53 |
struct aws_s3_object_info fs_info; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
static int s_on_contents_node(struct aws_xml_node *node, void *user_data) { |
| 58 |
struct fs_parser_wrapper *fs_wrapper = user_data; |
| 59 |
struct aws_s3_object_info *fs_info = &fs_wrapper->fs_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, &fs_info->e_tag); |
| 66 |
} |
| 67 |
|
| 68 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Key")) { |
| 69 |
return aws_xml_node_as_body(node, &fs_info->key); |
| 70 |
} |
| 71 |
|
| 72 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "LastModified")) { |
| 73 |
struct aws_byte_cursor date_cur; |
| 74 |
if (aws_xml_node_as_body(node, &date_cur) != AWS_OP_SUCCESS) { |
| 75 |
return AWS_OP_ERR; |
| 76 |
} |
| 77 |
|
| 78 |
if (aws_date_time_init_from_str_cursor(&fs_info->last_modified, &date_cur, AWS_DATE_FORMAT_ISO_8601)) { |
| 79 |
return AWS_OP_ERR; |
| 80 |
} |
| 81 |
|
| 82 |
return AWS_OP_SUCCESS; |
| 83 |
} |
| 84 |
|
| 85 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Size")) { |
| 86 |
struct aws_byte_cursor size_cur; |
| 87 |
if (aws_xml_node_as_body(node, &size_cur) != AWS_OP_SUCCESS) { |
| 88 |
return AWS_OP_ERR; |
| 89 |
} |
| 90 |
|
| 91 |
if (aws_byte_cursor_utf8_parse_u64(size_cur, &fs_info->size) != AWS_OP_SUCCESS) { |
| 92 |
return AWS_OP_ERR; |
| 93 |
} |
| 94 |
|
| 95 |
return AWS_OP_SUCCESS; |
| 96 |
} |
| 97 |
|
| 98 |
return AWS_OP_SUCCESS; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
static int s_on_common_prefixes_node(struct aws_xml_node *node, void *user_data) { |
| 103 |
struct fs_parser_wrapper *fs_wrapper = user_data; |
| 104 |
|
| 105 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 106 |
|
| 107 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Prefix")) { |
| 108 |
return aws_xml_node_as_body(node, &fs_wrapper->fs_info.prefix); |
| 109 |
} |
| 110 |
|
| 111 |
return AWS_OP_SUCCESS; |
| 112 |
} |
| 113 |
|
| 114 |
static int s_on_list_bucket_result_node_encountered(struct aws_xml_node *node, void *user_data) { |
| 115 |
struct aws_s3_operation_data *operation_data = user_data; |
| 116 |
|
| 117 |
struct aws_byte_cursor node_name = aws_xml_node_get_name(node); |
| 118 |
|
| 119 |
struct fs_parser_wrapper fs_wrapper; |
| 120 |
AWS_ZERO_STRUCT(fs_wrapper); |
| 121 |
|
| 122 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "Contents")) { |
| 123 |
fs_wrapper.allocator = operation_data->allocator; |
| 124 |
|
| 125 |
|
| 126 |
if (aws_xml_node_traverse(node, s_on_contents_node, &fs_wrapper) != AWS_OP_SUCCESS) { |
| 127 |
return AWS_OP_ERR; |
| 128 |
} |
| 129 |
|
| 130 |
if (operation_data->prefix && !fs_wrapper.fs_info.prefix.len) { |
| 131 |
fs_wrapper.fs_info.prefix = aws_byte_cursor_from_string(operation_data->prefix); |
| 132 |
} |
| 133 |
|
| 134 |
struct aws_byte_buf trimmed_etag; |
| 135 |
aws_byte_buf_init(&trimmed_etag, fs_wrapper.allocator, 20); |
| 136 |
|
| 137 |
if (aws_byte_buf_append_unescaped_xml(fs_wrapper.allocator, fs_wrapper.fs_info.e_tag, &trimmed_etag) == |
| 138 |
AWS_OP_SUCCESS) { |
| 139 |
fs_wrapper.fs_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag); |
| 140 |
} |
| 141 |
|
| 142 |
int ret_val = AWS_OP_SUCCESS; |
| 143 |
if (operation_data->on_object) { |
| 144 |
ret_val = operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data); |
| 145 |
} |
| 146 |
|
| 147 |
aws_byte_buf_clean_up(&trimmed_etag); |
| 148 |
|
| 149 |
return ret_val; |
| 150 |
} |
| 151 |
|
| 152 |
if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "CommonPrefixes")) { |
| 153 |
|
| 154 |
|
| 155 |
if (aws_xml_node_traverse(node, s_on_common_prefixes_node, &fs_wrapper) != AWS_OP_SUCCESS) { |
| 156 |
return AWS_OP_ERR; |
| 157 |
} |
| 158 |
|
| 159 |
int ret_val = AWS_OP_SUCCESS; |
| 160 |
if (operation_data->on_object) { |
| 161 |
ret_val = operation_data->on_object(&fs_wrapper.fs_info, operation_data->user_data); |
| 162 |
} |
| 163 |
return ret_val; |
| 164 |
} |
| 165 |
|
| 166 |
return AWS_OP_SUCCESS; |
| 167 |
} |
| 168 |
|
| 169 |
static int s_construct_next_request_http_message( |
| 170 |
struct aws_byte_cursor *continuation_token, |
| 171 |
void *user_data, |
| 172 |
struct aws_http_message **out_message) { |
| 173 |
AWS_PRECONDITION(user_data); |
| 174 |
|
| 175 |
struct aws_s3_operation_data *operation_data = user_data; |
| 176 |
|
| 177 |
struct aws_byte_cursor s_path_start = aws_byte_cursor_from_c_str("/?list-type=2"); |
| 178 |
struct aws_byte_buf request_path; |
| 179 |
aws_byte_buf_init_copy_from_cursor(&request_path, operation_data->allocator, s_path_start); |
| 180 |
|
| 181 |
if (operation_data->prefix) { |
| 182 |
struct aws_byte_cursor s_prefix = aws_byte_cursor_from_c_str("&prefix="); |
| 183 |
aws_byte_buf_append_dynamic(&request_path, &s_prefix); |
| 184 |
struct aws_byte_cursor s_prefix_val = aws_byte_cursor_from_string(operation_data->prefix); |
| 185 |
aws_byte_buf_append_encoding_uri_param(&request_path, &s_prefix_val); |
| 186 |
} |
| 187 |
|
| 188 |
if (operation_data->delimiter) { |
| 189 |
struct aws_byte_cursor s_delimiter = aws_byte_cursor_from_c_str("&delimiter="); |
| 190 |
aws_byte_buf_append_dynamic(&request_path, &s_delimiter); |
| 191 |
struct aws_byte_cursor s_delimiter_val = aws_byte_cursor_from_string(operation_data->delimiter); |
| 192 |
aws_byte_buf_append_dynamic(&request_path, &s_delimiter_val); |
| 193 |
} |
| 194 |
|
| 195 |
if (continuation_token) { |
| 196 |
struct aws_byte_cursor s_continuation = aws_byte_cursor_from_c_str("&continuation-token="); |
| 197 |
aws_byte_buf_append_dynamic(&request_path, &s_continuation); |
| 198 |
aws_byte_buf_append_encoding_uri_param(&request_path, continuation_token); |
| 199 |
} |
| 200 |
|
| 201 |
struct aws_http_message *list_objects_v2_request = aws_http_message_new_request(operation_data->allocator); |
| 202 |
aws_http_message_set_request_path(list_objects_v2_request, aws_byte_cursor_from_buf(&request_path)); |
| 203 |
|
| 204 |
aws_byte_buf_clean_up(&request_path); |
| 205 |
|
| 206 |
struct aws_http_header accept_header = { |
| 207 |
.name = aws_byte_cursor_from_c_str("accept"), |
| 208 |
.value = aws_byte_cursor_from_c_str("application/xml"), |
| 209 |
}; |
| 210 |
|
| 211 |
aws_http_message_add_header(list_objects_v2_request, accept_header); |
| 212 |
|
| 213 |
aws_http_message_set_request_method(list_objects_v2_request, aws_http_method_get); |
| 214 |
|
| 215 |
*out_message = list_objects_v2_request; |
| 216 |
return AWS_OP_SUCCESS; |
| 217 |
} |
| 218 |
|
| 219 |
struct aws_s3_paginator *aws_s3_initiate_list_objects( |
| 220 |
struct aws_allocator *allocator, |
| 221 |
const struct aws_s3_list_objects_params *params) { |
| 222 |
AWS_FATAL_PRECONDITION(params); |
| 223 |
AWS_FATAL_PRECONDITION(params->client); |
| 224 |
AWS_FATAL_PRECONDITION(params->bucket_name.len); |
| 225 |
AWS_FATAL_PRECONDITION(params->endpoint.len); |
| 226 |
|
| 227 |
struct aws_s3_operation_data *operation_data = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_operation_data)); |
| 228 |
operation_data->allocator = allocator; |
| 229 |
operation_data->delimiter = |
| 230 |
params->delimiter.len > 0 ? aws_string_new_from_cursor(allocator, ¶ms->delimiter) : NULL; |
| 231 |
operation_data->prefix = params->prefix.len > 0 ? aws_string_new_from_cursor(allocator, ¶ms->prefix) : NULL; |
| 232 |
operation_data->on_object = params->on_object; |
| 233 |
operation_data->user_data = params->user_data; |
| 234 |
|
| 235 |
aws_ref_count_init(&operation_data->ref_count, operation_data, s_ref_count_zero_callback); |
| 236 |
|
| 237 |
struct aws_s3_paginated_operation_params operation_params = { |
| 238 |
.operation_name = aws_byte_cursor_from_c_str("ListObjectsV2"), |
| 239 |
.next_message = s_construct_next_request_http_message, |
| 240 |
.on_result_node_encountered_fn = s_on_list_bucket_result_node_encountered, |
| 241 |
.on_paginated_operation_cleanup = s_on_paginator_cleanup, |
| 242 |
.result_xml_node_name = aws_byte_cursor_from_c_str("ListBucketResult"), |
| 243 |
.continuation_token_node_name = aws_byte_cursor_from_c_str("NextContinuationToken"), |
| 244 |
.user_data = operation_data, |
| 245 |
}; |
| 246 |
|
| 247 |
struct aws_s3_paginated_operation *operation = aws_s3_paginated_operation_new(allocator, &operation_params); |
| 248 |
|
| 249 |
struct aws_s3_paginator_params paginator_params = { |
| 250 |
.client = params->client, |
| 251 |
.bucket_name = params->bucket_name, |
| 252 |
.endpoint = params->endpoint, |
| 253 |
.on_page_finished_fn = params->on_list_finished, |
| 254 |
.operation = operation, |
| 255 |
.user_data = params->user_data, |
| 256 |
}; |
| 257 |
|
| 258 |
struct aws_s3_paginator *paginator = aws_s3_initiate_paginator(allocator, &paginator_params); |
| 259 |
|
| 260 |
|
| 261 |
aws_s3_paginated_operation_release(operation); |
| 262 |
|
| 263 |
return paginator; |
| 264 |
} |