util: make http url validity checks more generic, and move them to util.c
This commit is contained in:
@@ -2298,7 +2298,7 @@ int config_parse_documentation(const char *unit,
|
||||
|
||||
for (a = b = u->documentation; a && *a; a++) {
|
||||
|
||||
if (is_valid_documentation_url(*a))
|
||||
if (documentation_url_is_valid(*a))
|
||||
*(b++) = *a;
|
||||
else {
|
||||
log_syntax(unit, LOG_ERR, filename, line, EINVAL,
|
||||
|
||||
+2
-13
@@ -28,9 +28,9 @@
|
||||
#include "utf8.h"
|
||||
#include "curl-util.h"
|
||||
#include "qcow2-util.h"
|
||||
#include "import-raw.h"
|
||||
#include "strv.h"
|
||||
#include "copy.h"
|
||||
#include "import-raw.h"
|
||||
|
||||
typedef struct RawImportFile RawImportFile;
|
||||
|
||||
@@ -904,7 +904,7 @@ int raw_import_pull(RawImport *import, const char *url, const char *local, bool
|
||||
int r;
|
||||
|
||||
assert(import);
|
||||
assert(raw_url_is_valid(url));
|
||||
assert(http_url_is_valid(url));
|
||||
assert(!local || machine_name_is_valid(local));
|
||||
|
||||
if (hashmap_get(import->files, url))
|
||||
@@ -949,14 +949,3 @@ int raw_import_pull(RawImport *import, const char *url, const char *local, bool
|
||||
f = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool raw_url_is_valid(const char *url) {
|
||||
if (isempty(url))
|
||||
return false;
|
||||
|
||||
if (!startswith(url, "http://") &&
|
||||
!startswith(url, "https://"))
|
||||
return false;
|
||||
|
||||
return ascii_is_valid(url);
|
||||
}
|
||||
|
||||
@@ -33,5 +33,3 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(RawImport*, raw_import_unref);
|
||||
|
||||
int raw_import_pull(RawImport *import, const char *url, const char *local, bool force_local);
|
||||
int raw_import_cancel(RawImport *import, const char *name);
|
||||
|
||||
bool raw_url_is_valid(const char *url);
|
||||
|
||||
+1
-1
@@ -90,7 +90,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
|
||||
int r;
|
||||
|
||||
url = argv[1];
|
||||
if (!raw_url_is_valid(url)) {
|
||||
if (!http_url_is_valid(url)) {
|
||||
log_error("URL '%s' is not valid.", url);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
+32
-14
@@ -5458,25 +5458,43 @@ int getenv_for_pid(pid_t pid, const char *field, char **_value) {
|
||||
return r;
|
||||
}
|
||||
|
||||
bool is_valid_documentation_url(const char *url) {
|
||||
assert(url);
|
||||
bool http_url_is_valid(const char *url) {
|
||||
const char *p;
|
||||
|
||||
if (startswith(url, "http://") && url[7])
|
||||
if (isempty(url))
|
||||
return false;
|
||||
|
||||
p = startswith(url, "http://");
|
||||
if (!p)
|
||||
p = startswith(url, "https://");
|
||||
if (!p)
|
||||
return false;
|
||||
|
||||
if (isempty(p))
|
||||
return false;
|
||||
|
||||
return ascii_is_valid(p);
|
||||
}
|
||||
|
||||
bool documentation_url_is_valid(const char *url) {
|
||||
const char *p;
|
||||
|
||||
if (isempty(url))
|
||||
return false;
|
||||
|
||||
if (http_url_is_valid(url))
|
||||
return true;
|
||||
|
||||
if (startswith(url, "https://") && url[8])
|
||||
return true;
|
||||
p = startswith(url, "file:/");
|
||||
if (!p)
|
||||
p = startswith(url, "info:");
|
||||
if (!p)
|
||||
p = startswith(url, "man:");
|
||||
|
||||
if (startswith(url, "file:") && url[5])
|
||||
return true;
|
||||
if (isempty(p))
|
||||
return false;
|
||||
|
||||
if (startswith(url, "info:") && url[5])
|
||||
return true;
|
||||
|
||||
if (startswith(url, "man:") && url[4])
|
||||
return true;
|
||||
|
||||
return false;
|
||||
return ascii_is_valid(p);
|
||||
}
|
||||
|
||||
bool in_initrd(void) {
|
||||
|
||||
+2
-1
@@ -651,7 +651,8 @@ int setrlimit_closest(int resource, const struct rlimit *rlim);
|
||||
|
||||
int getenv_for_pid(pid_t pid, const char *field, char **_value);
|
||||
|
||||
bool is_valid_documentation_url(const char *url) _pure_;
|
||||
bool http_url_is_valid(const char *url) _pure_;
|
||||
bool documentation_url_is_valid(const char *url) _pure_;
|
||||
|
||||
bool in_initrd(void);
|
||||
|
||||
|
||||
@@ -918,15 +918,15 @@ static void test_files_same(void) {
|
||||
}
|
||||
|
||||
static void test_is_valid_documentation_url(void) {
|
||||
assert_se(is_valid_documentation_url("http://www.freedesktop.org/wiki/Software/systemd"));
|
||||
assert_se(is_valid_documentation_url("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
|
||||
assert_se(is_valid_documentation_url("file:foo"));
|
||||
assert_se(is_valid_documentation_url("man:systemd.special(7)"));
|
||||
assert_se(is_valid_documentation_url("info:bar"));
|
||||
assert_se(documentation_url_is_valid("http://www.freedesktop.org/wiki/Software/systemd"));
|
||||
assert_se(documentation_url_is_valid("https://www.kernel.org/doc/Documentation/binfmt_misc.txt"));
|
||||
assert_se(documentation_url_is_valid("file:/foo/foo"));
|
||||
assert_se(documentation_url_is_valid("man:systemd.special(7)"));
|
||||
assert_se(documentation_url_is_valid("info:bar"));
|
||||
|
||||
assert_se(!is_valid_documentation_url("foo:"));
|
||||
assert_se(!is_valid_documentation_url("info:"));
|
||||
assert_se(!is_valid_documentation_url(""));
|
||||
assert_se(!documentation_url_is_valid("foo:"));
|
||||
assert_se(!documentation_url_is_valid("info:"));
|
||||
assert_se(!documentation_url_is_valid(""));
|
||||
}
|
||||
|
||||
static void test_file_in_same_dir(void) {
|
||||
|
||||
Reference in New Issue
Block a user