aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-s3/source/s3_paginator.c
blob: e42845d87f2dd2c3219454f814e9fc3d1e17f8db (plain) (blame)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/s3/private/s3_paginator.h>
#include <aws/s3/s3_client.h>

#include <aws/common/assert.h>
#include <aws/common/atomics.h>
#include <aws/common/byte_buf.h>
#include <aws/common/mutex.h>
#include <aws/common/ref_count.h>
#include <aws/common/string.h>
#include <aws/common/xml_parser.h>
#include <aws/http/request_response.h>

static const size_t s_dynamic_body_initial_buf_size = 1024;

enum operation_state {
    OS_NOT_STARTED,
    OS_INITIATED,
    OS_COMPLETED,
    OS_ERROR,
};

struct aws_s3_paginated_operation {
    struct aws_allocator *allocator;

    struct aws_string *result_xml_node_name;
    struct aws_string *continuation_xml_node_name;

    aws_s3_next_http_message_fn *next_http_message;
    aws_s3_on_result_node_encountered_fn *on_result_node_encountered;

    aws_s3_on_paginated_operation_cleanup_fn *on_paginated_operation_cleanup;

    void *user_data;

    struct aws_ref_count ref_count;
};

struct aws_s3_paginator {
    struct aws_allocator *allocator;
    struct aws_s3_client *client;

    /** The current, in-flight paginated request to s3. */
    struct aws_atomic_var current_request;

    struct aws_string *bucket_name;
    struct aws_string *endpoint;

    struct aws_s3_paginated_operation *operation;

    struct aws_ref_count ref_count;
    struct {
        struct aws_string *continuation_token;
        enum operation_state operation_state;
        struct aws_mutex lock;
        bool has_more_results;
    } shared_mt_state;

    struct aws_byte_buf result_body;

    aws_s3_on_page_finished_fn *on_page_finished;
    void *user_data;
};

static void s_operation_ref_count_zero_callback(void *arg) {
    struct aws_s3_paginated_operation *operation = arg;

    if (operation->on_paginated_operation_cleanup) {
        operation->on_paginated_operation_cleanup(operation->user_data);
    }

    if (operation->result_xml_node_name) {
        aws_string_destroy(operation->result_xml_node_name);
    }

    if (operation->continuation_xml_node_name) {
        aws_string_destroy(operation->continuation_xml_node_name);
    }

    aws_mem_release(operation->allocator, operation);
}

static void s_paginator_ref_count_zero_callback(void *arg) {
    struct aws_s3_paginator *paginator = arg;

    aws_s3_client_release(paginator->client);
    aws_s3_paginated_operation_release(paginator->operation);

    aws_byte_buf_clean_up(&paginator->result_body);

    struct aws_s3_meta_request *previous_request = aws_atomic_exchange_ptr(&paginator->current_request, NULL);
    if (previous_request != NULL) {
        aws_s3_meta_request_release(previous_request);
    }

    if (paginator->bucket_name) {
        aws_string_destroy(paginator->bucket_name);
    }

    if (paginator->endpoint) {
        aws_string_destroy(paginator->endpoint);
    }

    if (paginator->shared_mt_state.continuation_token) {
        aws_string_destroy(paginator->shared_mt_state.continuation_token);
    }

    aws_mem_release(paginator->allocator, paginator);
}

struct aws_s3_paginator *aws_s3_initiate_paginator(
    struct aws_allocator *allocator,
    const struct aws_s3_paginator_params *params) {
    AWS_FATAL_PRECONDITION(params);
    AWS_FATAL_PRECONDITION(params->client);

    struct aws_s3_paginator *paginator = aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_paginator));
    paginator->allocator = allocator;
    paginator->client = aws_s3_client_acquire(params->client);
    paginator->operation = params->operation;
    paginator->on_page_finished = params->on_page_finished_fn;
    paginator->user_data = params->user_data;

    paginator->bucket_name = aws_string_new_from_cursor(allocator, &params->bucket_name);
    paginator->endpoint = aws_string_new_from_cursor(allocator, &params->endpoint);

    aws_s3_paginated_operation_acquire(params->operation);

    aws_byte_buf_init(&paginator->result_body, allocator, s_dynamic_body_initial_buf_size);
    aws_ref_count_init(&paginator->ref_count, paginator, s_paginator_ref_count_zero_callback);
    aws_mutex_init(&paginator->shared_mt_state.lock);
    aws_atomic_init_ptr(&paginator->current_request, NULL);
    paginator->shared_mt_state.operation_state = OS_NOT_STARTED;

    return paginator;
}

void aws_s3_paginator_release(struct aws_s3_paginator *paginator) {
    if (paginator) {
        aws_ref_count_release(&paginator->ref_count);
    }
}

void aws_s3_paginator_acquire(struct aws_s3_paginator *paginator) {
    AWS_FATAL_PRECONDITION(paginator);
    aws_ref_count_acquire(&paginator->ref_count);
}

struct aws_s3_paginated_operation *aws_s3_paginated_operation_new(
    struct aws_allocator *allocator,
    const struct aws_s3_paginated_operation_params *params) {

    struct aws_s3_paginated_operation *operation =
        aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_paginated_operation));
    operation->allocator = allocator;

    operation->result_xml_node_name = aws_string_new_from_cursor(allocator, params->result_xml_node_name);
    operation->continuation_xml_node_name = aws_string_new_from_cursor(allocator, params->continuation_token_node_name);

    operation->next_http_message = params->next_message;
    operation->on_result_node_encountered = params->on_result_node_encountered_fn;
    operation->on_paginated_operation_cleanup = params->on_paginated_operation_cleanup;

    operation->user_data = params->user_data;

    aws_ref_count_init(&operation->ref_count, operation, s_operation_ref_count_zero_callback);

    return operation;
}

void aws_s3_paginated_operation_acquire(struct aws_s3_paginated_operation *operation) {
    AWS_FATAL_PRECONDITION(operation);
    aws_ref_count_acquire(&operation->ref_count);
}

void aws_s3_paginated_operation_release(struct aws_s3_paginated_operation *operation) {
    if (operation) {
        aws_ref_count_release(&operation->ref_count);
    }
}

bool aws_s3_paginator_has_more_results(const struct aws_s3_paginator *paginator) {
    AWS_PRECONDITION(paginator);
    bool has_more_results = false;
    struct aws_s3_paginator *paginator_mut = (struct aws_s3_paginator *)paginator;
    aws_mutex_lock(&paginator_mut->shared_mt_state.lock);
    has_more_results = paginator->shared_mt_state.has_more_results;
    aws_mutex_unlock(&paginator_mut->shared_mt_state.lock);
    AWS_LOGF_INFO(AWS_LS_S3_GENERAL, "has more %d", has_more_results);
    return has_more_results;
}

struct aws_string *s_paginator_get_continuation_token(const struct aws_s3_paginator *paginator) {
    AWS_PRECONDITION(paginator);
    struct aws_string *continuation_token = NULL;
    struct aws_s3_paginator *paginator_mut = (struct aws_s3_paginator *)paginator;
    aws_mutex_lock(&paginator_mut->shared_mt_state.lock);
    if (paginator->shared_mt_state.continuation_token) {
        continuation_token =
            aws_string_clone_or_reuse(paginator->allocator, paginator->shared_mt_state.continuation_token);
    }
    aws_mutex_unlock(&paginator_mut->shared_mt_state.lock);
    return continuation_token;
}

static inline int s_set_paginator_state_if_legal(
    struct aws_s3_paginator *paginator,
    enum operation_state expected,
    enum operation_state state) {
    aws_mutex_lock(&paginator->shared_mt_state.lock);
    if (paginator->shared_mt_state.operation_state != expected) {
        aws_mutex_unlock(&paginator->shared_mt_state.lock);
        return aws_raise_error(AWS_ERROR_INVALID_STATE);
    }
    paginator->shared_mt_state.operation_state = state;
    aws_mutex_unlock(&paginator->shared_mt_state.lock);
    return AWS_OP_SUCCESS;
}

/**
 * On a successful operation, this is an xml document. Just copy the buffers over until we're ready to parse (upon
 * completion) of the response body.
 */
static int s_receive_body_callback(
    struct aws_s3_meta_request *meta_request,
    const struct aws_byte_cursor *body,
    uint64_t range_start,
    void *user_data) {
    (void)range_start;
    (void)meta_request;

    struct aws_s3_paginator *paginator = user_data;

    if (body && body->len) {
        aws_byte_buf_append_dynamic(&paginator->result_body, body);
    }
    return AWS_OP_SUCCESS;
}

struct parser_wrapper {
    struct aws_s3_paginated_operation *operation;
    struct aws_string *next_continuation_token;
    bool has_more_results;
};

static bool s_on_result_node_encountered(struct aws_xml_parser *parser, struct aws_xml_node *node, void *user_data) {

    struct parser_wrapper *wrapper = user_data;

    struct aws_byte_cursor node_name;
    aws_xml_node_get_name(node, &node_name);

    struct aws_byte_cursor continuation_name_val =
        aws_byte_cursor_from_string(wrapper->operation->continuation_xml_node_name);
    if (aws_byte_cursor_eq_ignore_case(&node_name, &continuation_name_val)) {
        struct aws_byte_cursor continuation_token_cur;
        bool ret_val = aws_xml_node_as_body(parser, node, &continuation_token_cur) == AWS_OP_SUCCESS;

        if (ret_val) {
            wrapper->next_continuation_token =
                aws_string_new_from_cursor(wrapper->operation->allocator, &continuation_token_cur);
        }

        return ret_val;
    }

    if (aws_byte_cursor_eq_c_str_ignore_case(&node_name, "IsTruncated")) {
        struct aws_byte_cursor truncated_cur;
        bool ret_val = aws_xml_node_as_body(parser, node, &truncated_cur) == AWS_OP_SUCCESS;

        if (ret_val) {
            if (aws_byte_cursor_eq_c_str_ignore_case(&truncated_cur, "true")) {
                wrapper->has_more_results = true;
            }
        }

        return ret_val;
    }

    return wrapper->operation->on_result_node_encountered(parser, node, wrapper->operation->user_data);
}

static bool s_on_root_node_encountered(struct aws_xml_parser *parser, struct aws_xml_node *node, void *user_data) {
    struct parser_wrapper *wrapper = user_data;

    struct aws_byte_cursor node_name;
    aws_xml_node_get_name(node, &node_name);
    struct aws_byte_cursor result_name_val = aws_byte_cursor_from_string(wrapper->operation->result_xml_node_name);
    if (aws_byte_cursor_eq_ignore_case(&node_name, &result_name_val)) {
        return aws_xml_node_traverse(parser, node, s_on_result_node_encountered, wrapper);
    }

    return false;
}

static void s_on_request_finished(
    struct aws_s3_meta_request *meta_request,
    const struct aws_s3_meta_request_result *meta_request_result,
    void *user_data) {
    (void)meta_request;
    struct aws_s3_paginator *paginator = user_data;

    if (meta_request_result->response_status == 200) {
        /* clears previous continuation token */
        aws_mutex_lock(&paginator->shared_mt_state.lock);
        if (paginator->shared_mt_state.continuation_token) {
            aws_string_destroy(paginator->shared_mt_state.continuation_token);
            paginator->shared_mt_state.continuation_token = NULL;
            paginator->shared_mt_state.has_more_results = false;
        }
        aws_mutex_unlock(&paginator->shared_mt_state.lock);

        struct aws_byte_cursor result_body_cursor = aws_byte_cursor_from_buf(&paginator->result_body);
        struct aws_string *continuation_token = NULL;
        bool has_more_results = false;
        aws_s3_paginated_operation_on_response(
            paginator->operation, &result_body_cursor, &continuation_token, &has_more_results);

        aws_mutex_lock(&paginator->shared_mt_state.lock);

        if (paginator->shared_mt_state.continuation_token) {
            aws_string_destroy(paginator->shared_mt_state.continuation_token);
        }

        paginator->shared_mt_state.continuation_token = continuation_token;
        paginator->shared_mt_state.has_more_results = has_more_results;
        aws_mutex_unlock(&paginator->shared_mt_state.lock);

        if (has_more_results) {
            s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_NOT_STARTED);
        } else {
            s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_COMPLETED);
        }

    } else {
        s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_ERROR);
    }

    if (paginator->on_page_finished) {
        paginator->on_page_finished(paginator, meta_request_result->error_code, paginator->user_data);
    }

    /* this ref count was done right before we kicked off the request to keep the paginator object alive. Release it now
     * that the operation has completed. */
    aws_s3_paginator_release(paginator);
}

int aws_s3_paginated_operation_on_response(
    struct aws_s3_paginated_operation *operation,
    struct aws_byte_cursor *response_body,
    struct aws_string **continuation_token_out,
    bool *has_more_results_out) {

    struct aws_xml_parser_options parser_options = {
        .doc = *response_body,
        .max_depth = 16U,
    };

    struct parser_wrapper wrapper = {.operation = operation};

    /* we've got a full xml document now and the request succeeded, parse the document and fire all the callbacks
     * for each object and prefix. All of that happens in these three lines. */
    struct aws_xml_parser *parser = aws_xml_parser_new(operation->allocator, &parser_options);
    int error_code = aws_xml_parser_parse(parser, s_on_root_node_encountered, &wrapper);
    aws_xml_parser_destroy(parser);

    if (error_code == AWS_OP_SUCCESS) {
        *continuation_token_out = wrapper.next_continuation_token;
        *has_more_results_out = wrapper.has_more_results;
    }

    return error_code;
}

int aws_s3_construct_next_paginated_request_http_message(
    struct aws_s3_paginated_operation *operation,
    struct aws_byte_cursor *continuation_token,
    struct aws_http_message **out_message) {
    return operation->next_http_message(continuation_token, operation->user_data, out_message);
}

int aws_s3_paginator_continue(struct aws_s3_paginator *paginator, const struct aws_signing_config_aws *signing_config) {
    AWS_PRECONDITION(paginator);
    AWS_PRECONDITION(signing_config);

    int re_code = AWS_OP_ERR;

    if (s_set_paginator_state_if_legal(paginator, OS_NOT_STARTED, OS_INITIATED)) {
        return re_code;
    }

    struct aws_http_message *paginated_request_message = NULL;
    struct aws_string *continuation_string = NULL;
    struct aws_byte_buf host_buf;
    AWS_ZERO_STRUCT(host_buf);

    struct aws_byte_cursor host_cur = aws_byte_cursor_from_string(paginator->bucket_name);
    struct aws_byte_cursor period_cur = aws_byte_cursor_from_c_str(".");
    struct aws_byte_cursor endpoint_val = aws_byte_cursor_from_string(paginator->endpoint);
    if (aws_byte_buf_init_copy_from_cursor(&host_buf, paginator->allocator, host_cur) ||
        aws_byte_buf_append_dynamic(&host_buf, &period_cur) || aws_byte_buf_append_dynamic(&host_buf, &endpoint_val)) {
        goto done;
    }

    struct aws_http_header host_header = {
        .name = aws_byte_cursor_from_c_str("host"),
        .value = aws_byte_cursor_from_buf(&host_buf),
    };

    continuation_string = s_paginator_get_continuation_token(paginator);
    struct aws_byte_cursor continuation_cursor;
    AWS_ZERO_STRUCT(continuation_cursor);
    struct aws_byte_cursor *continuation = NULL;
    if (continuation_string) {
        continuation_cursor = aws_byte_cursor_from_string(continuation_string);
        continuation = &continuation_cursor;
    }
    if (paginator->operation->next_http_message(
            continuation, paginator->operation->user_data, &paginated_request_message)) {
        goto done;
    }

    if (aws_http_message_add_header(paginated_request_message, host_header)) {
        goto done;
    }

    struct aws_s3_meta_request_options request_options = {
        .user_data = paginator,
        .signing_config = (struct aws_signing_config_aws *)signing_config,
        .type = AWS_S3_META_REQUEST_TYPE_DEFAULT,
        .body_callback = s_receive_body_callback,
        .finish_callback = s_on_request_finished,
        .message = paginated_request_message,
    };

    /* re-use the current buffer. */
    aws_byte_buf_reset(&paginator->result_body, false);

    /* we're kicking off an asynchronous request. ref-count the paginator to keep it alive until we finish. */
    aws_s3_paginator_acquire(paginator);

    struct aws_s3_meta_request *previous_request = aws_atomic_exchange_ptr(&paginator->current_request, NULL);
    if (previous_request != NULL) {
        /* release request from previous page */
        aws_s3_meta_request_release(previous_request);
    }

    struct aws_s3_meta_request *new_request = aws_s3_client_make_meta_request(paginator->client, &request_options);
    aws_atomic_store_ptr(&paginator->current_request, new_request);

    if (new_request == NULL) {
        s_set_paginator_state_if_legal(paginator, OS_INITIATED, OS_ERROR);
        goto done;
    }

    re_code = AWS_OP_SUCCESS;
done:
    aws_http_message_release(paginated_request_message);
    aws_string_destroy(continuation_string);
    aws_byte_buf_clean_up(&host_buf);
    return re_code;
}