diff options
author | Lennart Poettering <lennart@poettering.net> | 2015-01-19 20:45:27 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-01-20 15:06:58 +0100 |
commit | a2e0337875addaf08225fbf9b231435ba12a88b5 (patch) | |
tree | 2d8db845ca71eef64593bc564931d9f21fc1fe9b | |
parent | 6c8f2e7d78b1fe280588dc91beae90cdf36fcd49 (diff) |
util: make http url validity checks more generic, and move them to util.c
-rw-r--r-- | src/core/load-fragment.c | 2 | ||||
-rw-r--r-- | src/import/import-raw.c | 15 | ||||
-rw-r--r-- | src/import/import-raw.h | 2 | ||||
-rw-r--r-- | src/import/import.c | 2 | ||||
-rw-r--r-- | src/shared/util.c | 42 | ||||
-rw-r--r-- | src/shared/util.h | 3 | ||||
-rw-r--r-- | src/test/test-util.c | 18 |
7 files changed, 45 insertions, 39 deletions
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c index 242b684ddf..516731aba2 100644 --- a/src/core/load-fragment.c +++ b/src/core/load-fragment.c @@ -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, diff --git a/src/import/import-raw.c b/src/import/import-raw.c index 80fdbb7781..486157aa96 100644 --- a/src/import/import-raw.c +++ b/src/import/import-raw.c @@ -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); -} diff --git a/src/import/import-raw.h b/src/import/import-raw.h index a423ec0187..17f7a1a8ee 100644 --- a/src/import/import-raw.h +++ b/src/import/import-raw.h @@ -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); diff --git a/src/import/import.c b/src/import/import.c index af8d0ec42b..9b10de5559 100644 --- a/src/import/import.c +++ b/src/import/import.c @@ -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; } diff --git a/src/shared/util.c b/src/shared/util.c index fd54023660..5157b94a34 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -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]) - return true; + if (isempty(url)) + return false; - if (startswith(url, "https://") && url[8]) - return true; + p = startswith(url, "http://"); + if (!p) + p = startswith(url, "https://"); + if (!p) + return false; - if (startswith(url, "file:") && url[5]) - return true; + if (isempty(p)) + return false; - if (startswith(url, "info:") && url[5]) - return true; + return ascii_is_valid(p); +} - if (startswith(url, "man:") && url[4]) +bool documentation_url_is_valid(const char *url) { + const char *p; + + if (isempty(url)) + return false; + + if (http_url_is_valid(url)) return true; - return false; + p = startswith(url, "file:/"); + if (!p) + p = startswith(url, "info:"); + if (!p) + p = startswith(url, "man:"); + + if (isempty(p)) + return false; + + return ascii_is_valid(p); } bool in_initrd(void) { diff --git a/src/shared/util.h b/src/shared/util.h index 2e662c9d2d..d40c0b0378 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -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); diff --git a/src/test/test-util.c b/src/test/test-util.c index 0c0d2f67f8..8471d310b2 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -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(!is_valid_documentation_url("foo:")); - assert_se(!is_valid_documentation_url("info:")); - assert_se(!is_valid_documentation_url("")); + 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(!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) { |