Debian Codegraph @

crt/aws-c-auth/source/signable_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/signable.h>
7 #include <aws/common/string.h>
8 #include <aws/http/request_response.h>
9
10 /*
11 * This is a simple aws_signable wrapper implementation for the aws_http_message struct
12 */
13 struct aws_signable_http_request_impl {
14 struct aws_http_message *request;
15 struct aws_array_list headers;
16 };
17
18 static int s_aws_signable_http_request_get_property(
19 const struct aws_signable *signable,
20 const struct aws_string *name,
21 struct aws_byte_cursor *out_value) {
22
23 struct aws_signable_http_request_impl *impl = signable->impl;
24
25 AWS_ZERO_STRUCT(*out_value);
26
27 /*
28 * uri and method can be queried directly from the wrapper request
29 */
30 if (aws_string_eq(name, g_aws_http_uri_property_name)) {
31 aws_http_message_get_request_path(impl->request, out_value);
32 } else if (aws_string_eq(name, g_aws_http_method_property_name)) {
33 aws_http_message_get_request_method(impl->request, out_value);
34 } else {
35 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
36 }
37
38 return AWS_OP_SUCCESS;
39 }
40
41 static int s_aws_signable_http_request_get_property_list(
42 const struct aws_signable *signable,
43 const struct aws_string *name,
44 struct aws_array_list **out_list) {
45
46 struct aws_signable_http_request_impl *impl = signable->impl;
47
48 *out_list = NULL;
49
50 if (aws_string_eq(name, g_aws_http_headers_property_list_name)) {
51 *out_list = &impl->headers;
52 } else {
53 return aws_raise_error(AWS_ERROR_UNSUPPORTED_OPERATION);
54 }
55
56 return AWS_OP_SUCCESS;
57 }
58
59 static int s_aws_signable_http_request_get_payload_stream(
60 const struct aws_signable *signable,
61 struct aws_input_stream **out_input_stream) {
62
63 struct aws_signable_http_request_impl *impl = signable->impl;
64 *out_input_stream = aws_http_message_get_body_stream(impl->request);
65
66 return AWS_OP_SUCCESS;
67 }
68
69 static void s_aws_signable_http_request_destroy(struct aws_signable *signable) {
70 if (signable == NULL) {
71 return;
72 }
73
74 struct aws_signable_http_request_impl *impl = signable->impl;
75 if (impl == NULL) {
76 return;
77 }
78
79 aws_http_message_release(impl->request);
80 aws_array_list_clean_up(&impl->headers);
81 aws_mem_release(signable->allocator, signable);
82 }
83
84 static struct aws_signable_vtable s_signable_http_request_vtable = {
85 .get_property = s_aws_signable_http_request_get_property,
86 .get_property_list = s_aws_signable_http_request_get_property_list,
87 .get_payload_stream = s_aws_signable_http_request_get_payload_stream,
88 .destroy = s_aws_signable_http_request_destroy,
89 };
90
91 struct aws_signable *aws_signable_new_http_request(struct aws_allocator *allocator, struct aws_http_message *request) {
92
93 struct aws_signable *signable = NULL;
94 struct aws_signable_http_request_impl *impl = NULL;
95 aws_mem_acquire_many(
96 allocator, 2, &signable, sizeof(struct aws_signable), &impl, sizeof(struct aws_signable_http_request_impl));
97
98 AWS_ZERO_STRUCT(*signable);
99 AWS_ZERO_STRUCT(*impl);
100
101 signable->allocator = allocator;
102 signable->vtable = &s_signable_http_request_vtable;
103 signable->impl = impl;
104
105 /*
106 * Copy the headers since they're not different types
107 */
108 size_t header_count = aws_http_message_get_header_count(request);
109 if (aws_array_list_init_dynamic(
110 &impl->headers, allocator, header_count, sizeof(struct aws_signable_property_list_pair))) {
111 goto on_error;
112 }
113
114 for (size_t i = 0; i < header_count; ++i) {
115 struct aws_http_header header;
116 aws_http_message_get_header(request, &header, i);
117
118 struct aws_signable_property_list_pair property = {.name = header.name, .value = header.value};
119 aws_array_list_push_back(&impl->headers, &property);
120 }
121
122 impl->request = aws_http_message_acquire(request);
123
124 return signable;
125
126 on_error:
127
128 aws_signable_destroy(signable);
129
130 return NULL;
131 }