aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-auth/source/signable_trailer.c
blob: b5742643de55f80fed293efc8de58c05674d43f1 (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
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/auth/signable.h>
#include <aws/common/string.h>
#include <aws/http/request_response.h>

struct aws_signable_trailing_headers_impl {
    struct aws_http_headers *trailing_headers;
    struct aws_array_list headers;
    struct aws_string *previous_signature;
};

static int s_aws_signable_trailing_headers_get_property(
    const struct aws_signable *signable,
    const struct aws_string *name,
    struct aws_byte_cursor *out_value) {

    struct aws_signable_trailing_headers_impl *impl = signable->impl;

    AWS_ZERO_STRUCT(*out_value);

    /*
     * uri and method can be queried directly from the wrapper request
     */
    if (aws_string_eq(name, g_aws_previous_signature_property_name)) {
        *out_value = aws_byte_cursor_from_string(impl->previous_signature);
    } else {
        return AWS_OP_ERR;
    }
    return AWS_OP_SUCCESS;
}

static int s_aws_signable_trailing_headers_get_property_list(
    const struct aws_signable *signable,
    const struct aws_string *name,
    struct aws_array_list **out_list) {
    (void)signable;
    (void)name;
    (void)out_list;

    struct aws_signable_trailing_headers_impl *impl = signable->impl;

    *out_list = NULL;

    if (aws_string_eq(name, g_aws_http_headers_property_list_name)) {
        *out_list = &impl->headers;
    } else {
        return AWS_OP_ERR;
    }

    return AWS_OP_SUCCESS;
}

static int s_aws_signable_trailing_headers_get_payload_stream(
    const struct aws_signable *signable,
    struct aws_input_stream **out_input_stream) {
    (void)signable;
    *out_input_stream = NULL;

    return AWS_OP_SUCCESS;
}

static void s_aws_signable_trailing_headers_destroy(struct aws_signable *signable) {
    if (signable == NULL) {
        return;
    }

    struct aws_signable_trailing_headers_impl *impl = signable->impl;
    if (impl == NULL) {
        return;
    }

    aws_http_headers_release(impl->trailing_headers);
    aws_string_destroy(impl->previous_signature);
    aws_array_list_clean_up(&impl->headers);
    aws_mem_release(signable->allocator, signable);
}

static struct aws_signable_vtable s_signable_trailing_headers_vtable = {
    .get_property = s_aws_signable_trailing_headers_get_property,
    .get_property_list = s_aws_signable_trailing_headers_get_property_list,
    .get_payload_stream = s_aws_signable_trailing_headers_get_payload_stream,
    .destroy = s_aws_signable_trailing_headers_destroy,
};

struct aws_signable *aws_signable_new_trailing_headers(
    struct aws_allocator *allocator,
    struct aws_http_headers *trailing_headers,
    struct aws_byte_cursor previous_signature) {

    struct aws_signable *signable = NULL;
    struct aws_signable_trailing_headers_impl *impl = NULL;
    aws_mem_acquire_many(
        allocator, 2, &signable, sizeof(struct aws_signable), &impl, sizeof(struct aws_signable_trailing_headers_impl));

    AWS_ZERO_STRUCT(*signable);
    AWS_ZERO_STRUCT(*impl);

    /* Keep the headers alive. We're referencing the underlying strings. */
    aws_http_headers_acquire(trailing_headers);
    impl->trailing_headers = trailing_headers;
    signable->allocator = allocator;
    signable->vtable = &s_signable_trailing_headers_vtable;
    signable->impl = impl;

    /*
     * Convert headers list to aws_signable_property_list_pair arraylist since they're not different types.
     */
    size_t header_count = aws_http_headers_count(trailing_headers);
    if (aws_array_list_init_dynamic(
            &impl->headers, allocator, header_count, sizeof(struct aws_signable_property_list_pair))) {
        goto on_error;
    }

    for (size_t i = 0; i < header_count; ++i) {
        struct aws_http_header header;
        aws_http_headers_get_index(trailing_headers, i, &header);

        struct aws_signable_property_list_pair property = {.name = header.name, .value = header.value};
        aws_array_list_push_back(&impl->headers, &property);
    }

    impl->previous_signature = aws_string_new_from_array(allocator, previous_signature.ptr, previous_signature.len);
    if (impl->previous_signature == NULL) {
        goto on_error;
    }

    return signable;

on_error:

    aws_signable_destroy(signable);

    return NULL;
}