Debian Codegraph @

crt/aws-c-auth/source/sigv4_http_request.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/auth/private/sigv4_http_request.h>
7
8 #include <aws/auth/credentials.h>
9 #include <aws/auth/signable.h>
10 #include <aws/auth/signing.h>
11 #include <aws/auth/signing_result.h>
12 #include <aws/common/condition_variable.h>
13 #include <aws/common/mutex.h>
14 #include <aws/common/string.h>
15 #include <aws/http/request_response.h>
16 #include <aws/io/uri.h>
17
18 #if defined(_MSC_VER)
19 # pragma warning(disable : 4204)
20 #endif /* _MSC_VER */
21
22 #define DEFAULT_QUERY_PARAM_COUNT 10
23
24 /*
25 * Uses the signing result to rebuild the request's URI. If the signing was not done via
26 * query params, then this ends up doing nothing.
27 */
28 static int s_build_request_uri(
29 struct aws_allocator *allocator,
30 struct aws_http_message *request,
31 const struct aws_signing_result *signing_result) {
32
33 /* first let's see if we need to do anything at all */
34 struct aws_array_list *result_param_list = NULL;
35 aws_signing_result_get_property_list(
36 signing_result, g_aws_http_query_params_property_list_name, &result_param_list);
37 if (result_param_list == NULL) {
38 return AWS_OP_SUCCESS;
39 }
40
41 /*
42 * There are query params to apply. Use the following algorithm:
43 *
44 * (1) Take the old uri and parse it into a URI structure
45 * (2) Make a new URI builder and add the old URI's components to it
46 * (3) Add the signing query params to the builder
47 * (4) Use the builder to make a new URI
48 */
49 int result = AWS_OP_ERR;
50 size_t signed_query_param_count = aws_array_list_length(result_param_list);
51
52 struct aws_uri old_uri;
53 AWS_ZERO_STRUCT(old_uri);
54
55 struct aws_uri new_uri;
56 AWS_ZERO_STRUCT(new_uri);
57
58 struct aws_uri_builder_options new_uri_builder;
59 AWS_ZERO_STRUCT(new_uri_builder);
60
61 struct aws_array_list query_params;
62 AWS_ZERO_STRUCT(query_params);
63
64 struct aws_byte_cursor old_path;
65 aws_http_message_get_request_path(request, &old_path);
66
67 /* start with the old uri and parse it */
68 if (aws_uri_init_parse(&old_uri, allocator, &old_path)) {
69 goto done;
70 }
71
72 /* pull out the old query params */
73 if (aws_array_list_init_dynamic(
74 &query_params, allocator, DEFAULT_QUERY_PARAM_COUNT, sizeof(struct aws_uri_param))) {
75 goto done;
76 }
77
78 if (aws_uri_query_string_params(&old_uri, &query_params)) {
79 goto done;
80 }
81
82 /* initialize a builder for the new uri matching the old uri */
83 new_uri_builder.host_name = old_uri.host_name;
84 new_uri_builder.path = old_uri.path;
85 new_uri_builder.port = old_uri.port;
86 new_uri_builder.scheme = old_uri.scheme;
87 new_uri_builder.query_params = &query_params;
88
89 /* and now add any signing query params */
90 for (size_t i = 0; i < signed_query_param_count; ++i) {
91 struct aws_signing_result_property source_param;
92 if (aws_array_list_get_at(result_param_list, &source_param, i)) {
93 goto done;
94 }
95
96 struct aws_uri_param signed_param;
97 signed_param.key = aws_byte_cursor_from_string(source_param.name);
98 signed_param.value = aws_byte_cursor_from_string(source_param.value);
99
100 aws_array_list_push_back(&query_params, &signed_param);
101 }
102
103 /* create the new uri */
104 if (aws_uri_init_from_builder_options(&new_uri, allocator, &new_uri_builder)) {
105 goto done;
106 }
107
108 /* copy the full string */
109 struct aws_byte_cursor new_uri_cursor = aws_byte_cursor_from_buf(&new_uri.uri_str);
110 if (aws_http_message_set_request_path(request, new_uri_cursor)) {
111 goto done;
112 }
113
114 result = AWS_OP_SUCCESS;
115
116 done:
117
118 aws_array_list_clean_up(&query_params);
119
120 aws_uri_clean_up(&new_uri);
121 aws_uri_clean_up(&old_uri);
122
123 return result;
124 }
125
126 /*
127 * Takes a mutable http request and adds all the additional query params and/or headers generated by the
128 * signing process.
129 */
130 int aws_apply_signing_result_to_http_request(
131 struct aws_http_message *request,
132 struct aws_allocator *allocator,
133 const struct aws_signing_result *result) {
134
135 /* uri/query params */
136 if (s_build_request_uri(allocator, request, result)) {
137 return AWS_OP_ERR;
138 }
139
140 /* headers */
141 size_t signing_header_count = 0;
142 struct aws_array_list *result_header_list = NULL;
143 aws_signing_result_get_property_list(result, g_aws_http_headers_property_list_name, &result_header_list);
144 if (result_header_list != NULL) {
145 signing_header_count = aws_array_list_length(result_header_list);
146 }
147
148 for (size_t i = 0; i < signing_header_count; ++i) {
149 struct aws_signing_result_property source_header;
150 AWS_ZERO_STRUCT(source_header);
151
152 if (aws_array_list_get_at(result_header_list, &source_header, i)) {
153 return AWS_OP_ERR;
154 }
155
156 if (source_header.name == NULL || source_header.value == NULL) {
157 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
158 }
159
160 struct aws_http_header dest_header = {
161 .name = aws_byte_cursor_from_string(source_header.name),
162 .value = aws_byte_cursor_from_string(source_header.value),
163 };
164 aws_http_message_add_header(request, dest_header);
165 }
166
167 return AWS_OP_SUCCESS;
168 }