@@ -1619,6 +1619,8 @@ Result<std::optional<HostString>> HttpReq::http_req_downstream_tls_cipher_openss
1619
1619
auto status = fastly::req_downstream_tls_cipher_openssl_name (reinterpret_cast <char *>(ret.ptr ),
1620
1620
default_size, &ret.len );
1621
1621
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1622
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1623
+ // &ret.len, so we use that to inform our resize.
1622
1624
ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , default_size, 4 , ret.len ));
1623
1625
status = fastly::req_downstream_tls_cipher_openssl_name (reinterpret_cast <char *>(ret.ptr ),
1624
1626
ret.len , &ret.len );
@@ -1650,6 +1652,8 @@ Result<std::optional<HostString>> HttpReq::http_req_downstream_tls_protocol() {
1650
1652
auto status = fastly::req_downstream_tls_protocol (reinterpret_cast <char *>(ret.ptr ), default_size,
1651
1653
&ret.len );
1652
1654
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1655
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1656
+ // &ret.len, so we use that to inform our resize.
1653
1657
ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , default_size, 4 , ret.len ));
1654
1658
status =
1655
1659
fastly::req_downstream_tls_protocol (reinterpret_cast <char *>(ret.ptr ), ret.len , &ret.len );
@@ -1679,6 +1683,8 @@ Result<std::optional<HostBytes>> HttpReq::http_req_downstream_tls_client_hello()
1679
1683
ret.ptr = static_cast <uint8_t *>(cabi_malloc (default_size, 4 ));
1680
1684
auto status = fastly::req_downstream_tls_client_hello (ret.ptr , default_size, &ret.len );
1681
1685
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1686
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1687
+ // &ret.len, so we use that to inform our resize.
1682
1688
ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , default_size, 4 , ret.len ));
1683
1689
status = fastly::req_downstream_tls_client_hello (ret.ptr , ret.len , &ret.len );
1684
1690
}
@@ -1708,6 +1714,8 @@ Result<std::optional<HostBytes>> HttpReq::http_req_downstream_tls_raw_client_cer
1708
1714
ret.ptr = static_cast <uint8_t *>(cabi_malloc (default_size, 4 ));
1709
1715
auto status = fastly::req_downstream_tls_raw_client_certificate (ret.ptr , default_size, &ret.len );
1710
1716
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1717
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1718
+ // &ret.len, so we use that to inform our resize.
1711
1719
ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , default_size, 4 , ret.len ));
1712
1720
status = fastly::req_downstream_tls_raw_client_certificate (ret.ptr , ret.len , &ret.len );
1713
1721
}
@@ -1736,6 +1744,8 @@ Result<std::optional<HostBytes>> HttpReq::http_req_downstream_tls_ja3_md5() {
1736
1744
ret.ptr = static_cast <uint8_t *>(cabi_malloc (default_size, 4 ));
1737
1745
auto status = fastly::req_downstream_tls_ja3_md5 (ret.ptr , &ret.len );
1738
1746
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
1747
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
1748
+ // &ret.len, so we use that to inform our resize.
1739
1749
ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , default_size, 4 , ret.len ));
1740
1750
status = fastly::req_downstream_tls_ja3_md5 (ret.ptr , &ret.len );
1741
1751
}
@@ -2686,25 +2696,47 @@ Result<ConfigStore> ConfigStore::open(std::string_view name) {
2686
2696
}
2687
2697
2688
2698
Result<std::optional<HostString>> ConfigStore::get (std::string_view name) {
2699
+ return this ->get (name, CONFIG_STORE_INITIAL_BUF_LEN);
2700
+ }
2701
+
2702
+ Result<std::optional<HostString>> ConfigStore::get (std::string_view name,
2703
+ uint32_t initial_buf_len) {
2689
2704
TRACE_CALL ()
2690
2705
Result<std::optional<HostString>> res;
2691
2706
2707
+ uint32_t buf_len{initial_buf_len};
2692
2708
auto name_str = string_view_to_world_string (name);
2693
2709
fastly::fastly_world_string ret;
2694
2710
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 ),
2697
- name_str.len , reinterpret_cast <char *>(ret.ptr ),
2698
- CONFIG_STORE_ENTRY_MAX_LEN, &ret.len ),
2699
- &err)) {
2711
+
2712
+ ret.ptr = static_cast <uint8_t *>(cabi_malloc (buf_len, 1 ));
2713
+
2714
+ bool succeeded{convert_result (
2715
+ fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ), name_str.len ,
2716
+ reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2717
+ &err)};
2718
+
2719
+ if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2720
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
2721
+ // &ret.len, so we use that to inform our resize.
2722
+ buf_len = ret.len ;
2723
+ ret.len = 0 ;
2724
+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , initial_buf_len, 1 , buf_len));
2725
+ succeeded = convert_result (
2726
+ fastly::config_store_get (this ->handle , reinterpret_cast <char *>(name_str.ptr ), name_str.len ,
2727
+ reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2728
+ &err);
2729
+ }
2730
+
2731
+ if (!succeeded) {
2700
2732
cabi_free (ret.ptr );
2701
2733
if (error_is_optional_none (err)) {
2702
2734
res.emplace (std::nullopt);
2703
2735
} else {
2704
2736
res.emplace_err (err);
2705
2737
}
2706
2738
} else {
2707
- ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , CONFIG_STORE_ENTRY_MAX_LEN , 1 , ret.len ));
2739
+ ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , buf_len , 1 , ret.len ));
2708
2740
res.emplace (make_host_string (ret));
2709
2741
}
2710
2742
@@ -2842,25 +2874,43 @@ FastlyAsyncTask::Handle ObjectStorePendingDelete::async_handle() const {
2842
2874
}
2843
2875
2844
2876
Result<std::optional<HostBytes>> Secret::plaintext () const {
2877
+ return this ->plaintext (CONFIG_STORE_INITIAL_BUF_LEN);
2878
+ }
2879
+
2880
+ Result<std::optional<HostBytes>> Secret::plaintext (uint32_t initial_buf_len) const {
2845
2881
TRACE_CALL ()
2846
2882
Result<std::optional<HostBytes>> res;
2847
2883
2884
+ uint32_t buf_len{initial_buf_len};
2848
2885
fastly::fastly_world_list_u8 ret;
2849
2886
fastly::fastly_host_error err;
2850
- ret.ptr = static_cast <uint8_t *>(JS_malloc (CONTEXT, DICTIONARY_ENTRY_MAX_LEN));
2851
- if (!convert_result (fastly::secret_store_plaintext (this ->handle ,
2852
- reinterpret_cast <char *>(ret.ptr ),
2853
- DICTIONARY_ENTRY_MAX_LEN, &ret.len ),
2854
- &err)) {
2887
+ ret.ptr = static_cast <uint8_t *>(JS_malloc (CONTEXT, buf_len));
2888
+ bool succeeded{
2889
+ convert_result (fastly::secret_store_plaintext (this ->handle , reinterpret_cast <char *>(ret.ptr ),
2890
+ buf_len, &ret.len ),
2891
+ &err)};
2892
+
2893
+ if (!succeeded && err == FASTLY_HOST_ERROR_BUFFER_LEN) {
2894
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
2895
+ // &ret.len, so we use that to inform our resize.
2896
+ buf_len = ret.len ;
2897
+ ret.len = 0 ;
2898
+ ret.ptr = static_cast <uint8_t *>(JS_realloc (CONTEXT, ret.ptr , initial_buf_len, buf_len));
2899
+ succeeded =
2900
+ convert_result (fastly::secret_store_plaintext (
2901
+ this ->handle , reinterpret_cast <char *>(ret.ptr ), buf_len, &ret.len ),
2902
+ &err);
2903
+ }
2904
+
2905
+ if (!succeeded) {
2855
2906
if (error_is_optional_none (err)) {
2856
2907
res.emplace (std::nullopt);
2857
2908
} else {
2858
2909
JS_free (CONTEXT, ret.ptr );
2859
2910
res.emplace_err (err);
2860
2911
}
2861
2912
} else {
2862
- ret.ptr =
2863
- static_cast <uint8_t *>(JS_realloc (CONTEXT, ret.ptr , DICTIONARY_ENTRY_MAX_LEN, ret.len ));
2913
+ ret.ptr = static_cast <uint8_t *>(JS_realloc (CONTEXT, ret.ptr , buf_len, ret.len ));
2864
2914
res.emplace (make_host_bytes (ret.ptr , ret.len ));
2865
2915
}
2866
2916
@@ -3363,6 +3413,8 @@ Result<HostBytes> CacheHandle::get_user_metadata() {
3363
3413
auto status = fastly::cache_get_user_metadata (handle, reinterpret_cast <char *>(ret.ptr ),
3364
3414
default_size, &ret.len );
3365
3415
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
3416
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
3417
+ // &ret.len, so we use that to inform our resize.
3366
3418
ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , default_size, 4 , ret.len ));
3367
3419
status = fastly::cache_get_user_metadata (handle, reinterpret_cast <char *>(ret.ptr ), ret.len ,
3368
3420
&ret.len );
@@ -4102,6 +4154,8 @@ Result<HostString> DeviceDetection::lookup(std::string_view user_agent) {
4102
4154
reinterpret_cast <char *>(user_agent_str.ptr ), user_agent_str.len ,
4103
4155
reinterpret_cast <char *>(ret.ptr ), default_size, &ret.len );
4104
4156
if (status == FASTLY_HOST_ERROR_BUFFER_LEN) {
4157
+ // NB(@zkat): ERROR_BUFFER_LEN sets the expected length of the buffer to
4158
+ // &ret.len, so we use that to inform our resize.
4105
4159
ret.ptr = static_cast <uint8_t *>(cabi_realloc (ret.ptr , default_size, 4 , ret.len ));
4106
4160
status = fastly::device_detection_lookup (reinterpret_cast <char *>(user_agent_str.ptr ),
4107
4161
user_agent_str.len , reinterpret_cast <char *>(ret.ptr ),
0 commit comments