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
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/string.h>
#include <aws/crt/Api.h>
#include <aws/crt/endpoints/RuleEngine.h>
#include <aws/sdkutils/endpoints_rule_engine.h>
#include <aws/sdkutils/partitions.h>
namespace Aws
{
namespace Crt
{
namespace Endpoints
{
RequestContext::RequestContext(Allocator *allocator) noexcept : m_allocator(allocator)
{
m_requestContext = aws_endpoints_request_context_new(allocator);
}
RequestContext::~RequestContext()
{
m_requestContext = aws_endpoints_request_context_release(m_requestContext);
}
bool RequestContext::AddString(const ByteCursor &name, const ByteCursor &value)
{
return AWS_OP_SUCCESS !=
aws_endpoints_request_context_add_string(m_allocator, m_requestContext, name, value);
}
bool RequestContext::AddBoolean(const ByteCursor &name, bool value)
{
return AWS_OP_SUCCESS !=
aws_endpoints_request_context_add_boolean(m_allocator, m_requestContext, name, value);
}
ResolutionOutcome::ResolutionOutcome(aws_endpoints_resolved_endpoint *impl) : m_resolvedEndpoint(impl) {}
ResolutionOutcome::ResolutionOutcome(ResolutionOutcome &&toMove) noexcept
: m_resolvedEndpoint(toMove.m_resolvedEndpoint)
{
toMove.m_resolvedEndpoint = nullptr;
}
ResolutionOutcome &ResolutionOutcome::operator=(ResolutionOutcome &&toMove)
{
if (&toMove != this)
{
*this = ResolutionOutcome(std::move(toMove));
}
return *this;
}
ResolutionOutcome::~ResolutionOutcome() { aws_endpoints_resolved_endpoint_release(m_resolvedEndpoint); }
bool ResolutionOutcome::IsEndpoint() const noexcept
{
return AWS_ENDPOINTS_RESOLVED_ENDPOINT == aws_endpoints_resolved_endpoint_get_type(m_resolvedEndpoint);
}
bool ResolutionOutcome::IsError() const noexcept
{
return AWS_ENDPOINTS_RESOLVED_ERROR == aws_endpoints_resolved_endpoint_get_type(m_resolvedEndpoint);
}
Optional<StringView> ResolutionOutcome::GetUrl() const
{
ByteCursor url;
if (aws_endpoints_resolved_endpoint_get_url(m_resolvedEndpoint, &url))
{
return Optional<StringView>();
}
return Optional<StringView>(ByteCursorToStringView(url));
}
inline StringView CrtStringToStringView(const aws_string *s)
{
ByteCursor key = aws_byte_cursor_from_string(s);
return ByteCursorToStringView(key);
}
Optional<UnorderedMap<StringView, Vector<StringView>>> ResolutionOutcome::GetHeaders() const
{
const aws_hash_table *resolved_headers = nullptr;
if (aws_endpoints_resolved_endpoint_get_headers(m_resolvedEndpoint, &resolved_headers))
{
return Optional<UnorderedMap<StringView, Vector<StringView>>>();
}
UnorderedMap<StringView, Vector<StringView>> headers;
for (struct aws_hash_iter iter = aws_hash_iter_begin(resolved_headers); !aws_hash_iter_done(&iter);
aws_hash_iter_next(&iter))
{
ByteCursor key = aws_byte_cursor_from_string((const aws_string *)iter.element.key);
const aws_array_list *array = (const aws_array_list *)iter.element.value;
headers.emplace(std::make_pair(
ByteCursorToStringView(key),
ArrayListToVector<aws_string *, StringView>(array, CrtStringToStringView)));
}
return Optional<UnorderedMap<StringView, Vector<StringView>>>(headers);
}
Optional<StringView> ResolutionOutcome::GetProperties() const
{
ByteCursor properties;
if (aws_endpoints_resolved_endpoint_get_properties(m_resolvedEndpoint, &properties))
{
return Optional<StringView>();
}
return Optional<StringView>(ByteCursorToStringView(properties));
}
Optional<StringView> ResolutionOutcome::GetError() const
{
ByteCursor error;
if (aws_endpoints_resolved_endpoint_get_error(m_resolvedEndpoint, &error))
{
return Optional<StringView>();
}
return Optional<StringView>(ByteCursorToStringView(error));
}
RuleEngine::RuleEngine(
const ByteCursor &rulesetCursor,
const ByteCursor &partitionsCursor,
Allocator *allocator) noexcept
: m_ruleEngine(nullptr)
{
auto ruleset = aws_endpoints_ruleset_new_from_string(allocator, rulesetCursor);
auto partitions = aws_partitions_config_new_from_string(allocator, partitionsCursor);
if (ruleset != NULL && partitions != NULL)
{
m_ruleEngine = aws_endpoints_rule_engine_new(allocator, ruleset, partitions);
}
if (ruleset != NULL)
{
aws_endpoints_ruleset_release(ruleset);
}
if (partitions != NULL)
{
aws_partitions_config_release(partitions);
}
}
RuleEngine::~RuleEngine() { m_ruleEngine = aws_endpoints_rule_engine_release(m_ruleEngine); }
Optional<ResolutionOutcome> RuleEngine::Resolve(const RequestContext &context) const
{
aws_endpoints_resolved_endpoint *resolved = NULL;
if (aws_endpoints_rule_engine_resolve(m_ruleEngine, context.GetNativeHandle(), &resolved))
{
return Optional<ResolutionOutcome>();
}
return Optional<ResolutionOutcome>(ResolutionOutcome(resolved));
}
} // namespace Endpoints
} // namespace Crt
} // namespace Aws
|