systemd: add nm_sd_http_url_is_valid() to access internal http_url_is_valid()

This commit is contained in:
Thomas Haller 2020-04-24 09:35:12 +02:00
parent ce282fa3f7
commit 468c2e01ab
3 changed files with 61 additions and 15 deletions

View file

@ -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;
}

View file

@ -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__ */

View file

@ -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) {