cloud-setup: add nmcs_utils_uri_complete_interned() for constructing URI for _ec2_base()

This commit is contained in:
Thomas Haller 2021-07-12 18:16:10 +02:00
parent 78adf4796e
commit c6fefce8a4
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
3 changed files with 17 additions and 16 deletions

View file

@ -665,6 +665,20 @@ nmcs_utils_uri_build_concat_v(const char *base, const char **components, gsize n
return nm_str_buf_finalize(&strbuf, NULL);
}
const char *
nmcs_utils_uri_complete_interned(const char *uri)
{
gs_free char *s = NULL;
if (nm_str_is_empty(uri))
return NULL;
if (NM_STR_HAS_PREFIX(uri, "http://") || NM_STR_HAS_PREFIX(uri, "https://") || strchr(uri, '/'))
return g_intern_string(uri);
s = g_strconcat("http://", uri, NULL);
return g_intern_string(s);
}
/*****************************************************************************/
gboolean

View file

@ -121,6 +121,8 @@ char *nmcs_utils_uri_build_concat_v(const char *base, const char **components, g
#define nmcs_utils_uri_build_concat(base, ...) \
nmcs_utils_uri_build_concat_v(base, ((const char *[]){__VA_ARGS__}), NM_NARG(__VA_ARGS__))
const char *nmcs_utils_uri_complete_interned(const char *uri);
/*****************************************************************************/
gboolean nmcs_setting_ip_replace_ipv4_addresses(NMSettingIPConfig *s_ip,

View file

@ -29,22 +29,7 @@ again:
* This is mainly for testing, it's not usually supposed to be configured.
* Consider this private API! */
base = g_getenv(NMCS_ENV_VARIABLE("NM_CLOUD_SETUP_EC2_HOST"));
if (!nm_str_is_empty(base)) {
if (NM_STR_HAS_PREFIX(base, "http://") || NM_STR_HAS_PREFIX(base, "https://")
|| strchr(base, '/'))
base = g_intern_string(base);
else {
gs_free char *s = NULL;
s = g_strconcat("http://", base, NULL);
base = g_intern_string(s);
}
}
if (nm_str_is_empty(base))
base = NM_EC2_BASE;
nm_assert(!NM_STR_HAS_SUFFIX(base, "/"));
base = nmcs_utils_uri_complete_interned(base) ?: ("" NM_EC2_BASE);
if (!g_atomic_pointer_compare_and_exchange(&base_cached, NULL, base))
goto again;