endpoints_util.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/common/json.h>
  6. #include <aws/common/logging.h>
  7. #include <aws/common/string.h>
  8. #include <aws/sdkutils/private/endpoints_util.h>
  9. #include <aws/sdkutils/sdkutils.h>
  10. #include <inttypes.h>
  11. #ifdef _MSC_VER /* Disable sscanf warnings on windows. */
  12. # pragma warning(disable : 4204)
  13. # pragma warning(disable : 4706)
  14. # pragma warning(disable : 4996)
  15. #endif
  16. /* 4 octets of 3 chars max + 3 separators + null terminator */
  17. #define AWS_IPV4_STR_LEN 16
  18. #define IP_CHAR_FMT "%03" SCNu16
  19. /* arbitrary max length of a region. curent longest region name is 16 chars */
  20. #define AWS_REGION_LEN 50
  21. bool aws_is_ipv4(struct aws_byte_cursor host) {
  22. if (host.len > AWS_IPV4_STR_LEN - 1) {
  23. return false;
  24. }
  25. char copy[AWS_IPV4_STR_LEN] = {0};
  26. memcpy(copy, host.ptr, host.len);
  27. uint16_t octet[4] = {0};
  28. char remainder[2] = {0};
  29. if (4 != sscanf(
  30. copy,
  31. IP_CHAR_FMT "." IP_CHAR_FMT "." IP_CHAR_FMT "." IP_CHAR_FMT "%1s",
  32. &octet[0],
  33. &octet[1],
  34. &octet[2],
  35. &octet[3],
  36. remainder)) {
  37. return false;
  38. }
  39. for (size_t i = 0; i < 4; ++i) {
  40. if (octet[i] > 255) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. static bool s_starts_with(struct aws_byte_cursor cur, uint8_t ch) {
  47. return cur.len > 0 && cur.ptr[0] == ch;
  48. }
  49. static bool s_ends_with(struct aws_byte_cursor cur, uint8_t ch) {
  50. return cur.len > 0 && cur.ptr[cur.len - 1] == ch;
  51. }
  52. static bool s_is_ipv6_char(uint8_t value) {
  53. return aws_isxdigit(value) || value == ':';
  54. }
  55. /* actual encoding is %25, but % is omitted for simplicity, since split removes it */
  56. static struct aws_byte_cursor s_percent_uri_enc = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("25");
  57. /*
  58. * IPv6 format:
  59. * 8 groups of 4 hex chars separated by colons (:)
  60. * leading 0s in each group can be skipped
  61. * 2 or more consecutive zero groups can be replaced by double colon (::),
  62. * but only once.
  63. * ipv6 literal can be scoped by to zone by appending % followed by zone name
  64. * ( does not look like there is length reqs on zone name length. this
  65. * implementation enforces that its > 1 )
  66. * ipv6 can be embedded in url, in which case it must be wrapped inside []
  67. * and % be uri encoded as %25.
  68. * Implementation is fairly trivial and just iterates through the string
  69. * keeping track of the spec above.
  70. */
  71. bool aws_is_ipv6(struct aws_byte_cursor host, bool is_uri_encoded) {
  72. if (host.len == 0) {
  73. return false;
  74. }
  75. if (is_uri_encoded) {
  76. if (!s_starts_with(host, '[') || !s_ends_with(host, ']')) {
  77. return false;
  78. }
  79. aws_byte_cursor_advance(&host, 1);
  80. --host.len;
  81. }
  82. struct aws_byte_cursor substr = {0};
  83. /* first split is required ipv6 part */
  84. bool is_split = aws_byte_cursor_next_split(&host, '%', &substr);
  85. AWS_ASSERT(is_split); /* function is guaranteed to return at least one split */
  86. if (!is_split || substr.len == 0 || (s_starts_with(substr, ':') || s_ends_with(substr, ':')) ||
  87. !aws_byte_cursor_satisfies_pred(&substr, s_is_ipv6_char)) {
  88. return false;
  89. }
  90. uint8_t group_count = 0;
  91. bool has_double_colon = false;
  92. struct aws_byte_cursor group = {0};
  93. while (aws_byte_cursor_next_split(&substr, ':', &group)) {
  94. ++group_count;
  95. if (group_count > 8 || /* too many groups */
  96. group.len > 4 || /* too many chars in group */
  97. (has_double_colon && group.len == 0)) { /* only one double colon allowed */
  98. return false;
  99. }
  100. has_double_colon = has_double_colon || group.len == 0;
  101. }
  102. /* second split is optional zone part */
  103. if (aws_byte_cursor_next_split(&host, '%', &substr)) {
  104. if ((is_uri_encoded &&
  105. (substr.len < 3 ||
  106. !aws_byte_cursor_starts_with(&substr, &s_percent_uri_enc))) || /* encoding for % + 1 extra char */
  107. (!is_uri_encoded && substr.len == 0) || /* at least 1 char */
  108. !aws_byte_cursor_satisfies_pred(&substr, aws_isalnum)) {
  109. return false;
  110. }
  111. }
  112. return has_double_colon ? group_count < 7 : group_count == 8;
  113. }
  114. static char s_known_countries[][3] = {{"us"}, {"eu"}, {"ap"}, {"sa"}, {"ca"}, {"me"}, {"af"}};
  115. struct aws_byte_cursor aws_map_region_to_partition(struct aws_byte_cursor region) {
  116. if (region.len > AWS_REGION_LEN - 1) {
  117. return aws_byte_cursor_from_c_str("");
  118. }
  119. char copy[AWS_REGION_LEN] = {0};
  120. memcpy(copy, region.ptr, region.len);
  121. char country[3] = {0};
  122. char location[31] = {0};
  123. uint8_t num = 0;
  124. if (3 == sscanf(copy, "%2[^-]-%30[^-]-%03" SCNu8, country, location, &num)) {
  125. if (location[0] != 0 && num > 0) {
  126. for (size_t i = 0; i < sizeof(s_known_countries) / sizeof(s_known_countries[0]); ++i) {
  127. if (0 == strncmp(s_known_countries[i], country, 3)) {
  128. return aws_byte_cursor_from_c_str("aws");
  129. }
  130. }
  131. if (0 == strncmp("cn", country, 3)) {
  132. return aws_byte_cursor_from_c_str("aws-cn");
  133. }
  134. }
  135. }
  136. if (2 == sscanf(copy, "us-gov-%30[^-]-%03" SCNu8, location, &num)) {
  137. if (location[0] != 0 && num > 0) {
  138. return aws_byte_cursor_from_c_str("aws-us-gov");
  139. }
  140. }
  141. if (2 == sscanf(copy, "us-iso-%30[^-]-%03" SCNu8, location, &num)) {
  142. if (location[0] != 0 && num > 0) {
  143. return aws_byte_cursor_from_c_str("aws-iso");
  144. }
  145. }
  146. if (2 == sscanf(copy, "us-isob-%30[^-]-%03" SCNu8, location, &num)) {
  147. if (location[0] != 0 && num > 0) {
  148. return aws_byte_cursor_from_c_str("aws-iso-b");
  149. }
  150. }
  151. return aws_byte_cursor_from_c_str("");
  152. }
  153. bool aws_is_valid_host_label(struct aws_byte_cursor label, bool allow_subdomains) {
  154. bool next_must_be_alnum = true;
  155. size_t subdomain_count = 0;
  156. for (size_t i = 0; i < label.len; ++i) {
  157. if (label.ptr[i] == '.') {
  158. if (!allow_subdomains || subdomain_count == 0) {
  159. return false;
  160. }
  161. if (!aws_isalnum(label.ptr[i - 1])) {
  162. return false;
  163. }
  164. next_must_be_alnum = true;
  165. subdomain_count = 0;
  166. continue;
  167. }
  168. if (next_must_be_alnum && !aws_isalnum(label.ptr[i])) {
  169. return false;
  170. } else if (label.ptr[i] != '-' && !aws_isalnum(label.ptr[i])) {
  171. return false;
  172. }
  173. next_must_be_alnum = false;
  174. ++subdomain_count;
  175. if (subdomain_count > 63) {
  176. return false;
  177. }
  178. }
  179. return aws_isalnum(label.ptr[label.len - 1]);
  180. }
  181. struct aws_byte_cursor s_path_slash = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/");
  182. int aws_byte_buf_init_from_normalized_uri_path(
  183. struct aws_allocator *allocator,
  184. struct aws_byte_cursor path,
  185. struct aws_byte_buf *out_normalized_path) {
  186. /* Normalized path is just regular path that ensures that path starts and ends with slash */
  187. if (aws_byte_buf_init(out_normalized_path, allocator, path.len + 2)) {
  188. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed init buffer for parseUrl return.");
  189. goto on_error;
  190. }
  191. if (path.len == 0) {
  192. if (aws_byte_buf_append(out_normalized_path, &s_path_slash)) {
  193. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to add path to object.");
  194. goto on_error;
  195. }
  196. return AWS_OP_SUCCESS;
  197. }
  198. if (path.ptr[0] != '/') {
  199. if (aws_byte_buf_append_dynamic(out_normalized_path, &s_path_slash)) {
  200. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to append slash to normalized path.");
  201. goto on_error;
  202. }
  203. }
  204. if (aws_byte_buf_append_dynamic(out_normalized_path, &path)) {
  205. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to append path to normalized path.");
  206. goto on_error;
  207. }
  208. if (out_normalized_path->buffer[out_normalized_path->len - 1] != '/') {
  209. if (aws_byte_buf_append_dynamic(out_normalized_path, &s_path_slash)) {
  210. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to append slash to normalized path.");
  211. goto on_error;
  212. }
  213. }
  214. return AWS_OP_SUCCESS;
  215. on_error:
  216. aws_byte_buf_clean_up(out_normalized_path);
  217. return AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED;
  218. }
  219. struct aws_string *aws_string_new_from_json(struct aws_allocator *allocator, const struct aws_json_value *value) {
  220. struct aws_byte_buf json_blob;
  221. if (aws_byte_buf_init(&json_blob, allocator, 0)) {
  222. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to init buffer for json conversion.");
  223. goto on_error;
  224. }
  225. if (aws_byte_buf_append_json_string(value, &json_blob)) {
  226. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to convert json to string.");
  227. goto on_error;
  228. }
  229. struct aws_string *ret = aws_string_new_from_buf(allocator, &json_blob);
  230. aws_byte_buf_clean_up(&json_blob);
  231. return ret;
  232. on_error:
  233. aws_byte_buf_clean_up(&json_blob);
  234. aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
  235. return NULL;
  236. }
  237. bool aws_endpoints_byte_cursor_eq(const void *a, const void *b) {
  238. const struct aws_byte_cursor *a_cur = a;
  239. const struct aws_byte_cursor *b_cur = b;
  240. return aws_byte_cursor_eq(a_cur, b_cur);
  241. }
  242. void aws_array_list_deep_clean_up(struct aws_array_list *array, aws_array_callback_clean_up_fn on_clean_up_element) {
  243. for (size_t idx = 0; idx < aws_array_list_length(array); ++idx) {
  244. void *element = NULL;
  245. aws_array_list_get_at_ptr(array, &element, idx);
  246. AWS_ASSERT(element);
  247. on_clean_up_element(element);
  248. }
  249. aws_array_list_clean_up(array);
  250. }
  251. /* TODO: this can be moved into common */
  252. static bool s_split_on_first_delim(
  253. struct aws_byte_cursor input,
  254. char split_on,
  255. struct aws_byte_cursor *out_split,
  256. struct aws_byte_cursor *out_rest) {
  257. AWS_PRECONDITION(aws_byte_cursor_is_valid(&input));
  258. uint8_t *delim = memchr(input.ptr, split_on, input.len);
  259. if (delim != NULL) {
  260. out_split->ptr = input.ptr;
  261. out_split->len = delim - input.ptr;
  262. out_rest->ptr = delim;
  263. out_rest->len = input.len - (delim - input.ptr);
  264. return true;
  265. }
  266. *out_split = input;
  267. out_rest->ptr = NULL;
  268. out_rest->len = 0;
  269. return false;
  270. }
  271. static int s_buf_append_and_update_quote_count(
  272. struct aws_byte_buf *buf,
  273. struct aws_byte_cursor to_append,
  274. size_t *quote_count,
  275. bool is_json) {
  276. /* Dont count quotes if its not json. escaped quotes will be replaced with
  277. regular quotes when ruleset json is parsed, which will lead to incorrect
  278. results for when templates should be resolved in regular strings.
  279. Note: in json blobs escaped quotes are preserved and bellow approach works. */
  280. if (is_json) {
  281. for (size_t idx = 0; idx < to_append.len; ++idx) {
  282. if (to_append.ptr[idx] == '"' && !(idx > 0 && to_append.ptr[idx - 1] == '\\')) {
  283. ++*quote_count;
  284. }
  285. }
  286. }
  287. return aws_byte_buf_append_dynamic(buf, &to_append);
  288. }
  289. static struct aws_byte_cursor escaped_closing_curly = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("}}");
  290. static struct aws_byte_cursor escaped_opening_curly = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("{{");
  291. /*
  292. * Small helper to deal with escapes correctly in strings that occur before
  293. * template opening curly. General flow for resolving is to look for opening and
  294. * then closing curly. This function correctly appends any escaped closing
  295. * curlies and errors out if closing is not escaped (i.e. its unmatched).
  296. */
  297. int s_append_template_prefix_to_buffer(
  298. struct aws_byte_buf *out_buf,
  299. struct aws_byte_cursor prefix,
  300. size_t *quote_count,
  301. bool is_json) {
  302. struct aws_byte_cursor split = {0};
  303. struct aws_byte_cursor rest = {0};
  304. while (s_split_on_first_delim(prefix, '}', &split, &rest)) {
  305. if (s_buf_append_and_update_quote_count(out_buf, split, quote_count, is_json)) {
  306. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
  307. goto on_error;
  308. }
  309. if (*quote_count % 2 == 0) {
  310. if (aws_byte_buf_append_byte_dynamic(out_buf, '}')) {
  311. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
  312. goto on_error;
  313. }
  314. aws_byte_cursor_advance(&rest, 1);
  315. prefix = rest;
  316. continue;
  317. }
  318. if (aws_byte_cursor_starts_with(&rest, &escaped_closing_curly)) {
  319. if (aws_byte_buf_append_byte_dynamic(out_buf, '}')) {
  320. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
  321. goto on_error;
  322. }
  323. aws_byte_cursor_advance(&rest, 2);
  324. } else {
  325. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Unmatched or unescaped closing curly.");
  326. goto on_error;
  327. }
  328. prefix = rest;
  329. }
  330. if (s_buf_append_and_update_quote_count(out_buf, split, quote_count, is_json)) {
  331. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
  332. goto on_error;
  333. }
  334. return AWS_OP_SUCCESS;
  335. on_error:
  336. return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
  337. }
  338. int aws_byte_buf_init_from_resolved_templated_string(
  339. struct aws_allocator *allocator,
  340. struct aws_byte_buf *out_buf,
  341. struct aws_byte_cursor string,
  342. aws_endpoints_template_resolve_fn resolve_callback,
  343. void *user_data,
  344. bool is_json) {
  345. AWS_PRECONDITION(allocator);
  346. struct aws_owning_cursor resolved_template;
  347. AWS_ZERO_STRUCT(resolved_template);
  348. if (aws_byte_buf_init(out_buf, allocator, string.len)) {
  349. return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
  350. }
  351. size_t quote_count = is_json ? 0 : 1;
  352. struct aws_byte_cursor split = {0};
  353. struct aws_byte_cursor rest = {0};
  354. while (s_split_on_first_delim(string, '{', &split, &rest)) {
  355. if (s_append_template_prefix_to_buffer(out_buf, split, &quote_count, is_json)) {
  356. AWS_LOGF_ERROR(
  357. AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to buffer while evaluating templated sting.");
  358. goto on_error;
  359. }
  360. if (quote_count % 2 == 0) {
  361. if (aws_byte_buf_append_byte_dynamic(out_buf, '{')) {
  362. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
  363. goto on_error;
  364. }
  365. aws_byte_cursor_advance(&rest, 1);
  366. string = rest;
  367. continue;
  368. }
  369. if (aws_byte_cursor_starts_with(&rest, &escaped_opening_curly)) {
  370. if (aws_byte_buf_append_byte_dynamic(out_buf, '{')) {
  371. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
  372. goto on_error;
  373. }
  374. aws_byte_cursor_advance(&rest, 2);
  375. string = rest;
  376. continue;
  377. }
  378. aws_byte_cursor_advance(&rest, 1);
  379. struct aws_byte_cursor after_closing = {0};
  380. if (!s_split_on_first_delim(rest, '}', &split, &after_closing)) {
  381. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Unmatched closing curly.");
  382. goto on_error;
  383. }
  384. aws_byte_cursor_advance(&after_closing, 1);
  385. string = after_closing;
  386. if (resolve_callback(split, user_data, &resolved_template)) {
  387. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to resolve template.");
  388. goto on_error;
  389. }
  390. if (s_buf_append_and_update_quote_count(out_buf, resolved_template.cur, &quote_count, is_json)) {
  391. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append resolved value.");
  392. goto on_error;
  393. }
  394. aws_owning_cursor_clean_up(&resolved_template);
  395. }
  396. if (s_buf_append_and_update_quote_count(out_buf, split, &quote_count, is_json)) {
  397. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_GENERAL, "Failed to append to resolved template buffer.");
  398. goto on_error;
  399. }
  400. return AWS_OP_SUCCESS;
  401. on_error:
  402. aws_byte_buf_clean_up(out_buf);
  403. aws_owning_cursor_clean_up(&resolved_template);
  404. return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
  405. }
  406. int aws_path_through_json(
  407. struct aws_allocator *allocator,
  408. const struct aws_json_value *root,
  409. struct aws_byte_cursor path,
  410. const struct aws_json_value **out_value) {
  411. struct aws_array_list path_segments;
  412. if (aws_array_list_init_dynamic(&path_segments, allocator, 10, sizeof(struct aws_byte_cursor)) ||
  413. aws_byte_cursor_split_on_char(&path, '.', &path_segments)) {
  414. goto on_error;
  415. }
  416. *out_value = root;
  417. for (size_t idx = 0; idx < aws_array_list_length(&path_segments); ++idx) {
  418. struct aws_byte_cursor path_el_cur;
  419. if (aws_array_list_get_at(&path_segments, &path_el_cur, idx)) {
  420. AWS_LOGF_ERROR(AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Failed to get path element");
  421. goto on_error;
  422. }
  423. struct aws_byte_cursor element_cur = {0};
  424. aws_byte_cursor_next_split(&path_el_cur, '[', &element_cur);
  425. struct aws_byte_cursor index_cur = {0};
  426. bool has_index = aws_byte_cursor_next_split(&path_el_cur, '[', &index_cur) &&
  427. aws_byte_cursor_next_split(&path_el_cur, ']', &index_cur);
  428. if (element_cur.len > 0) {
  429. *out_value = aws_json_value_get_from_object(*out_value, element_cur);
  430. if (NULL == *out_value) {
  431. AWS_LOGF_ERROR(
  432. AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE, "Invalid path. " PRInSTR ".", AWS_BYTE_CURSOR_PRI(element_cur));
  433. goto on_error;
  434. }
  435. }
  436. if (has_index) {
  437. uint64_t index;
  438. if (aws_byte_cursor_utf8_parse_u64(index_cur, &index)) {
  439. AWS_LOGF_ERROR(
  440. AWS_LS_SDKUTILS_ENDPOINTS_RESOLVE,
  441. "Failed to parse index: " PRInSTR,
  442. AWS_BYTE_CURSOR_PRI(index_cur));
  443. goto on_error;
  444. }
  445. *out_value = aws_json_get_array_element(*out_value, (size_t)index);
  446. if (NULL == *out_value) {
  447. aws_reset_error();
  448. goto on_success;
  449. }
  450. }
  451. }
  452. on_success:
  453. aws_array_list_clean_up(&path_segments);
  454. return AWS_OP_SUCCESS;
  455. on_error:
  456. aws_array_list_clean_up(&path_segments);
  457. *out_value = NULL;
  458. return aws_raise_error(AWS_ERROR_SDKUTILS_ENDPOINTS_RESOLVE_FAILED);
  459. }
  460. struct aws_owning_cursor aws_endpoints_owning_cursor_create(
  461. struct aws_allocator *allocator,
  462. const struct aws_string *str) {
  463. struct aws_string *clone = aws_string_clone_or_reuse(allocator, str);
  464. struct aws_owning_cursor ret = {.string = clone, .cur = aws_byte_cursor_from_string(clone)};
  465. return ret;
  466. }
  467. struct aws_owning_cursor aws_endpoints_owning_cursor_from_string(struct aws_string *str) {
  468. struct aws_owning_cursor ret = {.string = str, .cur = aws_byte_cursor_from_string(str)};
  469. return ret;
  470. }
  471. struct aws_owning_cursor aws_endpoints_owning_cursor_from_cursor(
  472. struct aws_allocator *allocator,
  473. const struct aws_byte_cursor cur) {
  474. struct aws_string *clone = aws_string_new_from_cursor(allocator, &cur);
  475. struct aws_owning_cursor ret = {.string = clone, .cur = aws_byte_cursor_from_string(clone)};
  476. return ret;
  477. }
  478. struct aws_owning_cursor aws_endpoints_non_owning_cursor_create(struct aws_byte_cursor cur) {
  479. struct aws_owning_cursor ret = {.string = NULL, .cur = cur};
  480. return ret;
  481. }
  482. void aws_owning_cursor_clean_up(struct aws_owning_cursor *cursor) {
  483. aws_string_destroy(cursor->string);
  484. cursor->string = NULL;
  485. cursor->cur.ptr = NULL;
  486. cursor->cur.len = 0;
  487. }