Debian Codegraph @

crt/aws-c-s3/source/s3_paginator.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/s3/private/s3_paginator.h>
7 #include <aws/s3/s3_client.h>
8
9 #include <aws/common/assert.h>
10 #include <aws/common/atomics.h>
11 #include <aws/common/byte_buf.h>
12 #include <aws/common/mutex.h>
13 #include <aws/common/ref_count.h>
14 #include <aws/common/string.h>
15 #include <aws/common/xml_parser.h>
16 #include <aws/http/request_response.h>
17
18 static const size_t s_dynamic_body_initial_buf_size = 1024;
19
20 enum operation_state {
21 OS_NOT_STARTED,
22 OS_INITIATED,
23 OS_COMPLETED,
24 OS_ERROR,
25 };
26
27 struct aws_s3_paginated_operation {
28 struct aws_allocator *allocator;
29
30 struct aws_string *operation_name;
31 struct aws_string *result_xml_node_name;
32 struct aws_string *continuation_xml_node_name;
33
34 aws_s3_next_http_message_fn *next_http_message;
35 aws_s3_on_result_node_encountered_fn *on_result_node_encountered;
36
37 aws_s3_on_paginated_operation_cleanup_fn *on_paginated_operation_cleanup;
38
39 void *user_data;
40
41 struct aws_ref_count ref_count;
42 };
43
44 struct aws_s3_paginator {
45 struct aws_allocator *allocator;
46 struct aws_s3_client *client;
47
48 /** The current, in-flight paginated request to s3. */
49 struct aws_atomic_var current_request;
50
51 struct aws_string *bucket_name;
52 struct aws_string *endpoint;
53
54 struct aws_s3_paginated_operation *operation;
55
56 struct aws_ref_count ref_count;
57 struct {
58 struct aws_string *continuation_token;
59 enum operation_state operation_state;
60 struct aws_mutex lock;
61 bool has_more_results;
62 } shared_mt_state;
63
64 struct aws_byte_buf result_body;
65
66 aws_s3_on_page_finished_fn *on_page_finished;
67 void *user_data;
68 };
69
70 static void s_operation_ref_count_zero_callback(void *arg) {
71 struct aws_s3_paginated_operation *operation = arg;
72
73 if (operation->on_paginated_operation_cleanup) {
74 operation->on_paginated_operation_cleanup(operation->user_data);
75 }
76
77 if (operation->operation_name) {
78 aws_string_destroy(operation->operation_name);
79 }
80
81 if (operation->result_xml_node_name) {
82 aws_string_destroy(operation->result_xml_node_name);
83 }
84
85 if (operation->continuation_xml_node_name) {
86 aws_string_destroy(operation->continuation_xml_node_name);
87 }
88
89 aws_mem_release(operation->allocator, operation);
90 }
91
92 static void s_paginator_ref_count_zero_callback(void *arg) {
93 struct aws_s3_paginator *paginator = arg;
94
95 aws_s3_client_release(paginator->client);
96 aws_s3_paginated_operation_release(paginator->operation);
97
98 aws_byte_buf_clean_up(&paginator->result_body);
99
100 struct aws_s3_meta_request *previous_request = aws_atomic_exchange_ptr(&paginator->current_request, NULL);
101 if (previous_request != NULL) {
102 aws_s3_meta_request_release(previous_request);
103 }
104
105 if (paginator->bucket_name) {
106 aws_string_destroy(paginator->bucket_name);
107 }
108
109 if (paginator->endpoint) {
110 aws_string_destroy(paginator->endpoint);
111 }
112
113 if (paginator->shared_mt_state.continuation_token) {
114 aws_string_destroy(paginator->shared_mt_state.continuation_token);
115 }
116
117 aws_mem_release(paginator->allocator, paginator);
118 }
119
120 struct aws_s3_paginator *aws_s3_initiate_paginator(
121 struct aws_allocator *allocator,
122 const struct aws_s3_paginator_params *params) {
123 AWS_FATAL_PRECONDITION(params);
124 AWS_FATAL_PRECONDITION(params->client);
125
126 struct aws_s3_paginator *paginator = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_paginator));
127 paginator->allocator = allocator;
128 paginator->client = aws_s3_client_acquire(params->client);
129 paginator->operation = params->operation;
130 paginator->on_page_finished = params->on_page_finished_fn;
131 paginator->user_data = params->user_data;
132
133 paginator->bucket_name = aws_string_new_from_cursor(allocator, &params->bucket_name);
134 paginator->endpoint = aws_string_new_from_cursor(allocator, &params->endpoint);
135
136 aws_s3_paginated_operation_acquire(params->operation);
137
138 aws_byte_buf_init(&paginator->result_body, allocator, s_dynamic_body_initial_buf_size);
139 aws_ref_count_init(&paginator->ref_count, paginator, s_paginator_ref_count_zero_callback);
140 aws_mutex_init(&paginator->shared_mt_state.lock);
141 aws_atomic_init_ptr(&paginator->current_request, NULL);
142 paginator->shared_mt_state.operation_state = OS_NOT_STARTED;
143
144 return paginator;
145 }
146
147 void aws_s3_paginator_release(struct aws_s3_paginator *paginator) {
148 if (paginator) {
149 aws_ref_count_release(&paginator->ref_count);
150 }
151 }
152
153 void aws_s3_paginator_acquire(struct aws_s3_paginator *paginator) {
154 AWS_FATAL_PRECONDITION(paginator);
155 aws_ref_count_acquire(&paginator->ref_count);
156 }
157
158 struct aws_s3_paginated_operation *aws_s3_paginated_operation_new(
159 struct aws_allocator *allocator,
160 const struct aws_s3_paginated_operation_params *params) {
161
162 struct aws_s3_paginated_operation *operation =
163 aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_paginated_operation));
164 operation->allocator = allocator;
165
166 operation->operation_name = aws_string_new_from_cursor(allocator, &params->operation_name);
167 operation->result_xml_node_name = aws_string_new_from_cursor(allocator, &params->result_xml_node_name);
168 operation->continuation_xml_node_name =
169 aws_string_new_from_cursor(allocator, &params->continuation_token_node_name);
170
171 operation->next_http_message = params->next_message;
172 operation->on_result_node_encountered = params->on_result_node_encountered_fn;
173 operation->on_paginated_operation_cleanup = params->on_paginated_operation_cleanup;
174
175 operation->user_data = params->user_data;
176
177 aws_ref_count_init(&operation->ref_count, operation, s_operation_ref_count_zero_callback);
178
179 return operation;
180 }
181
182 void aws_s3_paginated_operation_acquire(struct aws_s3_paginated_operation *operation) {
183 AWS_FATAL_PRECONDITION(operation);
184 aws_ref_count_acquire(&operation->ref_count);
185 }
186
187 void aws_s3_paginated_operation_release(struct aws_s3_paginated_operation *operation) {
188 if (operation) {
189 aws_ref_count_release(&operation->ref_count);
190 }
191 }
192
193 bool aws_s3_paginator_has_more_results(const struct aws_s3_paginator *paginator) {
194 AWS_PRECONDITION(paginator);
195 bool has_more_results = false;
196 struct aws_s3_paginator *paginator_mut = (struct aws_s3_paginator *)paginator;
197 aws_mutex_lock(&paginator_mut->shared_mt_state.lock);
198 has_more_results = paginator->shared_mt_state.has_more_results;
199 aws_mutex_unlock(&paginator_mut->shared_mt_state.lock);
200 AWS_LOGF_INFO(AWS_LS_S3_GENERAL, "has more %d", has_more_results);
201 return has_more_results;
202 }
203
204 struct aws_string *s_paginator_get_continuation_token(const struct aws_s3_paginator *paginator) {
205 AWS_PRECONDITION(paginator);
206 struct aws_string *continuation_token = NULL;
207 struct aws_s3_paginator *paginator_mut = (struct aws_s3_paginator *)paginator;
208 aws_mutex_lock(&paginator_mut->shared_mt_state.lock);
209 if (paginator->shared_mt_state.continuation_token) {
210 continuation_token =
211 aws_string_clone_or_reuse(paginator->allocator, paginator->shared_mt_state.continuation_token);
212 }
213 aws_mutex_unlock(&paginator_mut->shared_mt_state.lock);
214 return continuation_token;
215 }
216
217 static inline int s_set_paginator_state_if_legal(
218 struct aws_s3_paginator *paginator,
219 enum operation_state expected,
220 enum operation_state state) {
221 aws_mutex_lock(&paginator->shared_mt_state.lock);
222 if (paginator->shared_mt_state.operation_state != expected) {
223 aws_mutex_unlock(&paginator->shared_mt_state.lock);
224 return aws_raise_error(AWS_ERROR_INVALID_STATE);
225 }
226 paginator->shared_mt_state.operation_state = state;
227 aws_mutex_unlock(&paginator->shared_mt_state.lock);
228 return AWS_OP_SUCCESS;
229 }
230
231 /**
232 * On a successful operation, this is an xml document. Just copy the buffers over until we're ready to parse (upon
233 * completion) of the response body.
234 */
235 static int s_receive_body_callback(
236 struct aws_s3_meta_request *meta_request,
237 const struct aws_byte_cursor *body,
238 uint64_t range_start,
239 void *user_data) {
240 (void)range_start;
241 (void)meta_request;
242
243 struct aws_s3_paginator *paginator = user_data;
244
245 if (body && body->len) {
246 aws_byte_buf_append_dynamic(&paginator->result_body, body);
247 }
248 return AWS_OP_SUCCESS;
249 }
250
251 struct parser_wrapper {
252 struct aws_s3_paginated_operation *operation;
253 struct aws_string *next_continuation_token;
254 bool has_more_results;
255 };
256
257 static int s_on_result_node_encountered(struct aws_xml_node *node, void *user_data) {
258
259 struct parser_wrapper *wrapper = user_data;
260
261 struct aws_byte_cursor node_name = aws_xml_node_get_name(node);
262
263 struct aws_byte_cursor continuation_name_val =
264 aws_byte_cursor_from_string(wrapper->operation->continuation_xml_node_name);
265 if (aws_byte_cursor_eq_ignore_case(&node_name, &continuation_name_val)) {
266 struct aws_byte_cursor continuation_token_cur;
267 if (aws_xml_node_as_body(node, &continuation_token_cur) != AWS_OP_SUCCESS) {
268 return AWS_OP_ERR;
269 }
270
271 wrapper->next_continuation_token =
272 aws_string_new_from_cursor(wrapper->operation->allocator, &continuation_token_cur);
273
274 return AWS_OP_SUCCESS;
275 }
276
277 if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "IsTruncated")) {
278 struct aws_byte_cursor truncated_cur;
279 if (aws_xml_node_as_body(node, &truncated_cur) != AWS_OP_SUCCESS) {
280 return AWS_OP_ERR;
281 }
282
283 if (aws_byte_cursor_eq_c_str_ignore_case(&truncated_cur, "true")) {
284 wrapper->has_more_results = true;
285 }
286
287 return AWS_OP_SUCCESS;
288 }
289
290 return wrapper->operation->on_result_node_encountered(node, wrapper->operation->user_data);
291 }
292
293 static int s_on_root_node_encountered(struct aws_xml_node *node, void *user_data) {
294 struct parser_wrapper *wrapper = user_data;
295
296 struct aws_byte_cursor node_name = aws_xml_node_get_name(node);
297 struct aws_byte_cursor result_name_val = aws_byte_cursor_from_string(wrapper->operation->result_xml_node_name);
298 if (aws_byte_cursor_eq_ignore_case(&node_name, &result_name_val)) {
299 return aws_xml_node_traverse(node, s_on_result_node_encountered, wrapper);
300 }
301
302 /* root element not what we expected */
303 return aws_raise_error(AWS_ERROR_INVALID_XML);
304 }
305
306 static void s_on_request_finished(
307 struct aws_s3_meta_request *meta_request,
308 const struct aws_s3_meta_request_result *meta_request_result,
309 void *user_data) {
310 (void)meta_request;
311 struct aws_s3_paginator *paginator = user_data;
312
313 if (meta_request_result->response_status == 200) {
314 /* clears previous continuation token */
315 aws_mutex_lock(&paginator->shared_mt_state.lock);
316 if (paginator->shared_mt_state.continuation_token) {
317 aws_string_destroy(paginator->shared_mt_state.continuation_token);
318 paginator->shared_mt_state.continuation_token = NULL;
319 paginator->shared_mt_state.has_more_results = false;
320 }
321 aws_mutex_unlock(&paginator->shared_mt_state.lock);
322
323 struct aws_byte_cursor result_body_cursor = aws_byte_cursor_from_buf(&paginator->result_body);
324 struct aws_string *continuation_token = NULL;
325 bool has_more_results = false;
326 aws_s3_paginated_operation_on_response(
327 paginator->operation, &result_body_cursor, &continuation_token, &has_more_results);
328
329 aws_mutex_lock(&paginator->shared_mt_state.lock);
330
331 if (paginator->shared_mt_state.continuation_token) {
332 aws_string_destroy(paginator->shared_mt_state.continuation_token);
333 }
334
335 paginator->shared_mt_state.continuation_token = continuation_token;
336 paginator->shared_mt_state.has_more_results = has_more_results;
337 aws_mutex_unlock(&paginator->shared_mt_state.lock);
338
339 if (has_more_results) {
340 s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_NOT_STARTED);
341 } else {
342 s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_COMPLETED);
343 }
344
345 } else {
346 s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_ERROR);
347 }
348
349 if (paginator->on_page_finished) {
350 paginator->on_page_finished(paginator, meta_request_result->error_code, paginator->user_data);
351 }
352
353 /* this ref count was done right before we kicked off the request to keep the paginator object alive. Release it now
354 * that the operation has completed. */
355 aws_s3_paginator_release(paginator);
356 }
357
358 int aws_s3_paginated_operation_on_response(
359 struct aws_s3_paginated_operation *operation,
360 struct aws_byte_cursor *response_body,
361 struct aws_string **continuation_token_out,
362 bool *has_more_results_out) {
363
364 struct parser_wrapper wrapper = {.operation = operation};
365
366 /* we've got a full xml document now and the request succeeded, parse the document and fire all the callbacks
367 * for each object and prefix. All of that happens in these three lines. */
368 struct aws_xml_parser_options parser_options = {
369 .doc = *response_body,
370 .max_depth = 16U,
371 .on_root_encountered = s_on_root_node_encountered,
372 .user_data = &wrapper,
373 };
374 if (aws_xml_parse(operation->allocator, &parser_options) != AWS_OP_SUCCESS) {
375 aws_string_destroy(wrapper.next_continuation_token);
376 *continuation_token_out = NULL;
377 *has_more_results_out = false;
378 return AWS_OP_ERR;
379 }
380
381 *continuation_token_out = wrapper.next_continuation_token;
382 *has_more_results_out = wrapper.has_more_results;
383 return AWS_OP_SUCCESS;
384 }
385
386 int aws_s3_construct_next_paginated_request_http_message(
387 struct aws_s3_paginated_operation *operation,
388 struct aws_byte_cursor *continuation_token,
389 struct aws_http_message **out_message) {
390 return operation->next_http_message(continuation_token, operation->user_data, out_message);
391 }
392
393 int aws_s3_paginator_continue(struct aws_s3_paginator *paginator, const struct aws_signing_config_aws *signing_config) {
394 AWS_PRECONDITION(paginator);
395 AWS_PRECONDITION(signing_config);
396
397 int re_code = AWS_OP_ERR;
398
399 if (s_set_paginator_state_if_legal(paginator, OS_NOT_STARTED, OS_INITIATED)) {
400 return re_code;
401 }
402
403 struct aws_http_message *paginated_request_message = NULL;
404 struct aws_string *continuation_string = NULL;
405 struct aws_byte_buf host_buf;
406 AWS_ZERO_STRUCT(host_buf);
407
408 struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(paginator->bucket_name);
409 struct aws_byte_cursor period_cur = aws_byte_cursor_from_c_str(".");
410 struct aws_byte_cursor endpoint_val = aws_byte_cursor_from_string(paginator->endpoint);
411 if (aws_byte_buf_init_copy_from_cursor(&host_buf, paginator->allocator, host_cur) ||
412 aws_byte_buf_append_dynamic(&host_buf, &period_cur) || aws_byte_buf_append_dynamic(&host_buf, &endpoint_val)) {
413 goto done;
414 }
415
416 struct aws_http_header host_header = {
417 .name = aws_byte_cursor_from_c_str("host"),
418 .value = aws_byte_cursor_from_buf(&host_buf),
419 };
420
421 continuation_string = s_paginator_get_continuation_token(paginator);
422 struct aws_byte_cursor continuation_cursor;
423 AWS_ZERO_STRUCT(continuation_cursor);
424 struct aws_byte_cursor *continuation = NULL;
425 if (continuation_string) {
426 continuation_cursor = aws_byte_cursor_from_string(continuation_string);
427 continuation = &continuation_cursor;
428 }
429 if (paginator->operation->next_http_message(
430 continuation, paginator->operation->user_data, &paginated_request_message)) {
431 goto done;
432 }
433
434 if (aws_http_message_add_header(paginated_request_message, host_header)) {
435 goto done;
436 }
437
438 struct aws_s3_meta_request_options request_options = {
439 .user_data = paginator,
440 .signing_config = (struct aws_signing_config_aws *)signing_config,
441 .type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
442 .operation_name = aws_byte_cursor_from_string(paginator->operation->operation_name),
443 .body_callback = s_receive_body_callback,
444 .finish_callback = s_on_request_finished,
445 .message = paginated_request_message,
446 };
447
448 /* re-use the current buffer. */
449 aws_byte_buf_reset(&paginator->result_body, false);
450
451 /* we're kicking off an asynchronous request. ref-count the paginator to keep it alive until we finish. */
452 aws_s3_paginator_acquire(paginator);
453
454 struct aws_s3_meta_request *previous_request = aws_atomic_exchange_ptr(&paginator->current_request, NULL);
455 if (previous_request != NULL) {
456 /* release request from previous page */
457 aws_s3_meta_request_release(previous_request);
458 }
459
460 struct aws_s3_meta_request *new_request = aws_s3_client_make_meta_request(paginator->client, &request_options);
461 aws_atomic_store_ptr(&paginator->current_request, new_request);
462
463 if (new_request == NULL) {
464 s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_ERROR);
465 goto done;
466 }
467
468 re_code = AWS_OP_SUCCESS;
469 done:
470 aws_http_message_release(paginated_request_message);
471 aws_string_destroy(continuation_string);
472 aws_byte_buf_clean_up(&host_buf);
473 return re_code;
474 }