diff --git a/shared/systemd/nm-sd-utils-shared.c b/shared/systemd/nm-sd-utils-shared.c index 85fd6a353e..b4e017c94b 100644 --- a/shared/systemd/nm-sd-utils-shared.c +++ b/shared/systemd/nm-sd-utils-shared.c @@ -13,6 +13,7 @@ #include "hexdecoct.h" #include "hostname-util.h" #include "path-util.h" +#include "web-util.h" /*****************************************************************************/ @@ -94,3 +95,40 @@ gboolean nm_sd_hostname_is_valid (const char *s, bool allow_trailing_dot) { return hostname_is_valid (s, allow_trailing_dot); } + +/*****************************************************************************/ + +static gboolean +_http_url_is_valid (const char *url) +{ + if ( !url + || !url[0]) + return FALSE; + + if (NM_STR_HAS_PREFIX (url, "http://")) + url += NM_STRLEN ("http://"); + else if (NM_STR_HAS_PREFIX (url, "https://")) + url += NM_STRLEN ("https://"); + else + return FALSE; + + if (!url[0]) + return FALSE; + + return !NM_STRCHAR_ANY (url, ch, (guchar) ch >= 128u); +} + +gboolean +nm_sd_http_url_is_valid (const char *url) +{ + gboolean v; + + /* http_url_is_valid() is part of our API, as we use it to validate connection + * properties. That means, it's behavior must remain stable (or only change + * with care). + * + * Thus, reimplement it, and make sure that our implementation agrees. */ + v = _http_url_is_valid (url); + nm_assert (v == http_url_is_valid (url)); + return v; +} diff --git a/shared/systemd/nm-sd-utils-shared.h b/shared/systemd/nm-sd-utils-shared.h index 3495a31d19..382db278d1 100644 --- a/shared/systemd/nm-sd-utils-shared.h +++ b/shared/systemd/nm-sd-utils-shared.h @@ -34,4 +34,8 @@ int nm_sd_dns_name_to_wire_format (const char *domain, int nm_sd_dns_name_is_valid (const char *s); gboolean nm_sd_hostname_is_valid(const char *s, bool allow_trailing_dot); +/*****************************************************************************/ + +gboolean nm_sd_http_url_is_valid (const char *url); + #endif /* __NM_SD_UTILS_SHARED_H__ */ diff --git a/shared/systemd/src/shared/web-util.c b/shared/systemd/src/shared/web-util.c index f5a5362bd5..4cff5e271d 100644 --- a/shared/systemd/src/shared/web-util.c +++ b/shared/systemd/src/shared/web-util.c @@ -24,21 +24,25 @@ bool http_etag_is_valid(const char *etag) { } #endif /* NM_IGNORED */ -bool http_url_is_valid(const char *url) { - const char *p; - - if (isempty(url)) - return false; - - p = STARTSWITH_SET(url, "http://", "https://"); - if (!p) - return false; - - if (isempty(p)) - return false; - - return ascii_is_valid(p); -} +/* NM: we use http_url_is_valid() for our own code, and it must not + * change behavior. If a re-import results in a merge-conflict, you must + * ensure that it does not change behavior, and possibly do something + * about that. */ +/**/ bool http_url_is_valid(const char *url) { +/**/ const char *p; +/**/ +/**/ if (isempty(url)) +/**/ return false; +/**/ +/**/ p = STARTSWITH_SET(url, "http://", "https://"); +/**/ if (!p) +/**/ return false; +/**/ +/**/ if (isempty(p)) +/**/ return false; +/**/ +/**/ return ascii_is_valid(p); +/**/ } #if 0 /* NM_IGNORED */ bool documentation_url_is_valid(const char *url) {