RuleEngine.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/string.h>
  6. #include <aws/crt/Api.h>
  7. #include <aws/crt/endpoints/RuleEngine.h>
  8. #include <aws/sdkutils/endpoints_rule_engine.h>
  9. #include <aws/sdkutils/partitions.h>
  10. namespace Aws
  11. {
  12. namespace Crt
  13. {
  14. namespace Endpoints
  15. {
  16. RequestContext::RequestContext(Allocator *allocator) noexcept : m_allocator(allocator)
  17. {
  18. m_requestContext = aws_endpoints_request_context_new(allocator);
  19. }
  20. RequestContext::~RequestContext()
  21. {
  22. m_requestContext = aws_endpoints_request_context_release(m_requestContext);
  23. }
  24. bool RequestContext::AddString(const ByteCursor &name, const ByteCursor &value)
  25. {
  26. return AWS_OP_SUCCESS !=
  27. aws_endpoints_request_context_add_string(m_allocator, m_requestContext, name, value);
  28. }
  29. bool RequestContext::AddBoolean(const ByteCursor &name, bool value)
  30. {
  31. return AWS_OP_SUCCESS !=
  32. aws_endpoints_request_context_add_boolean(m_allocator, m_requestContext, name, value);
  33. }
  34. ResolutionOutcome::ResolutionOutcome(aws_endpoints_resolved_endpoint *impl) : m_resolvedEndpoint(impl) {}
  35. ResolutionOutcome::ResolutionOutcome(ResolutionOutcome &&toMove) noexcept
  36. : m_resolvedEndpoint(toMove.m_resolvedEndpoint)
  37. {
  38. toMove.m_resolvedEndpoint = nullptr;
  39. }
  40. ResolutionOutcome &ResolutionOutcome::operator=(ResolutionOutcome &&toMove)
  41. {
  42. if (&toMove != this)
  43. {
  44. *this = ResolutionOutcome(std::move(toMove));
  45. }
  46. return *this;
  47. }
  48. ResolutionOutcome::~ResolutionOutcome() { aws_endpoints_resolved_endpoint_release(m_resolvedEndpoint); }
  49. bool ResolutionOutcome::IsEndpoint() const noexcept
  50. {
  51. return AWS_ENDPOINTS_RESOLVED_ENDPOINT == aws_endpoints_resolved_endpoint_get_type(m_resolvedEndpoint);
  52. }
  53. bool ResolutionOutcome::IsError() const noexcept
  54. {
  55. return AWS_ENDPOINTS_RESOLVED_ERROR == aws_endpoints_resolved_endpoint_get_type(m_resolvedEndpoint);
  56. }
  57. Optional<StringView> ResolutionOutcome::GetUrl() const
  58. {
  59. ByteCursor url;
  60. if (aws_endpoints_resolved_endpoint_get_url(m_resolvedEndpoint, &url))
  61. {
  62. return Optional<StringView>();
  63. }
  64. return Optional<StringView>(ByteCursorToStringView(url));
  65. }
  66. inline StringView CrtStringToStringView(const aws_string *s)
  67. {
  68. ByteCursor key = aws_byte_cursor_from_string(s);
  69. return ByteCursorToStringView(key);
  70. }
  71. Optional<UnorderedMap<StringView, Vector<StringView>>> ResolutionOutcome::GetHeaders() const
  72. {
  73. const aws_hash_table *resolved_headers = nullptr;
  74. if (aws_endpoints_resolved_endpoint_get_headers(m_resolvedEndpoint, &resolved_headers))
  75. {
  76. return Optional<UnorderedMap<StringView, Vector<StringView>>>();
  77. }
  78. UnorderedMap<StringView, Vector<StringView>> headers;
  79. for (struct aws_hash_iter iter = aws_hash_iter_begin(resolved_headers); !aws_hash_iter_done(&iter);
  80. aws_hash_iter_next(&iter))
  81. {
  82. ByteCursor key = aws_byte_cursor_from_string((const aws_string *)iter.element.key);
  83. const aws_array_list *array = (const aws_array_list *)iter.element.value;
  84. headers.emplace(std::make_pair(
  85. ByteCursorToStringView(key),
  86. ArrayListToVector<aws_string *, StringView>(array, CrtStringToStringView)));
  87. }
  88. return Optional<UnorderedMap<StringView, Vector<StringView>>>(headers);
  89. }
  90. Optional<StringView> ResolutionOutcome::GetProperties() const
  91. {
  92. ByteCursor properties;
  93. if (aws_endpoints_resolved_endpoint_get_properties(m_resolvedEndpoint, &properties))
  94. {
  95. return Optional<StringView>();
  96. }
  97. return Optional<StringView>(ByteCursorToStringView(properties));
  98. }
  99. Optional<StringView> ResolutionOutcome::GetError() const
  100. {
  101. ByteCursor error;
  102. if (aws_endpoints_resolved_endpoint_get_error(m_resolvedEndpoint, &error))
  103. {
  104. return Optional<StringView>();
  105. }
  106. return Optional<StringView>(ByteCursorToStringView(error));
  107. }
  108. RuleEngine::RuleEngine(
  109. const ByteCursor &rulesetCursor,
  110. const ByteCursor &partitionsCursor,
  111. Allocator *allocator) noexcept
  112. : m_ruleEngine(nullptr)
  113. {
  114. auto ruleset = aws_endpoints_ruleset_new_from_string(allocator, rulesetCursor);
  115. auto partitions = aws_partitions_config_new_from_string(allocator, partitionsCursor);
  116. if (ruleset != NULL && partitions != NULL)
  117. {
  118. m_ruleEngine = aws_endpoints_rule_engine_new(allocator, ruleset, partitions);
  119. }
  120. if (ruleset != NULL)
  121. {
  122. aws_endpoints_ruleset_release(ruleset);
  123. }
  124. if (partitions != NULL)
  125. {
  126. aws_partitions_config_release(partitions);
  127. }
  128. }
  129. RuleEngine::~RuleEngine() { m_ruleEngine = aws_endpoints_rule_engine_release(m_ruleEngine); }
  130. Optional<ResolutionOutcome> RuleEngine::Resolve(const RequestContext &context) const
  131. {
  132. aws_endpoints_resolved_endpoint *resolved = NULL;
  133. if (aws_endpoints_rule_engine_resolve(m_ruleEngine, context.GetNativeHandle(), &resolved))
  134. {
  135. return Optional<ResolutionOutcome>();
  136. }
  137. return Optional<ResolutionOutcome>(ResolutionOutcome(resolved));
  138. }
  139. } // namespace Endpoints
  140. } // namespace Crt
  141. } // namespace Aws