@@ -2647,6 +2647,7 @@ Result<std::optional<HostString>> Dict::get(std::string_view name) {
2647
2647
auto name_str = string_view_to_world_string (name);
2648
2648
fastly::fastly_world_string ret;
2649
2649
fastly::fastly_host_error err;
2650
+ uint32_t buf_len{initial_buf_len};
2650
2651
2651
2652
ret.ptr = static_cast <uint8_t *>(cabi_malloc (DICTIONARY_ENTRY_MAX_LEN, 1 ));
2652
2653
if (!convert_result (fastly::dictionary_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ),
@@ -2686,25 +2687,46 @@ Result<ConfigStore> ConfigStore::open(std::string_view name) {
2686
2687
}
2687
2688
2688
2689
Result<std::optional<HostString>> ConfigStore::get (std::string_view name) {
2690
+ return this ->get (name, CONFIG_STORE_INITIAL_BUF_LEN);
2691
+ }
2692
+
2693
+ Result<std::optional<HostString>> ConfigStore::get (std::string_view name, uint32_t initial_buf_len) {
2689
2694
TRACE_CALL ()
2690
2695
Result<std::optional<HostString>> res;
2691
2696
2692
2697
auto name_str = string_view_to_world_string (name);
2693
2698
fastly::fastly_world_string ret;
2694
2699
fastly::fastly_host_error err;
2695
- ret.ptr = static_cast <uint8_t *>(cabi_malloc (CONFIG_STORE_ENTRY_MAX_LEN, 1 ));
2696
- if (!convert_result (fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ),
2700
+ uin32_t buf_len{initial_buf_len};
2701
+
2702
+ ret.ptr = static_cast <uint8_t *>(cabi_malloc (buf_len, 1 ));
2703
+
2704
+ bool succeeded{convert_result (fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ),
2697
2705
name_str.len , reinterpret_cast <char *>(ret.ptr ),
2698
- CONFIG_STORE_ENTRY_MAX_LEN, &ret.len ),
2699
- &err)) {
2706
+ buf_len, &ret.len ),
2707
+ &err)}
2708
+
2709
+ if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2710
+ buf_len = ret.len ;
2711
+ ret.len = 0 ;
2712
+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , initial_buf_len, 1 , new_len));
2713
+ succeeded = convert_result (fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ),
2714
+ name_str.len , reinterpret_cast <char *>(ret.ptr ),
2715
+ buf_len, &ret.len ),
2716
+ &err)}
2717
+ }
2718
+
2719
+ if (!succeeded) {
2700
2720
cabi_free (ret.ptr );
2701
2721
if (error_is_optional_none (err)) {
2702
2722
res.emplace (std::nullopt);
2723
+ } else if (err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2724
+
2703
2725
} else {
2704
2726
res.emplace_err (err);
2705
2727
}
2706
2728
} else {
2707
- ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , CONFIG_STORE_ENTRY_MAX_LEN , 1 , ret.len ));
2729
+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , buf_len , 1 , ret.len ));
2708
2730
res.emplace (make_host_string (ret));
2709
2731
}
2710
2732
0 commit comments