chiark / gitweb /
util: make http url validity checks more generic, and move them to util.c
authorLennart Poettering <lennart@poettering.net>
Mon, 19 Jan 2015 19:45:27 +0000 (20:45 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 20 Jan 2015 14:06:58 +0000 (15:06 +0100)
src/core/load-fragment.c
src/import/import-raw.c
src/import/import-raw.h
src/import/import.c
src/shared/util.c
src/shared/util.h
src/test/test-util.c

index 242b684..516731a 100644 (file)
@@ -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,
index 80fdbb7..486157a 100644 (file)
@@ -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);
-}
index a423ec0..17f7a1a 100644 (file)
@@ -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);
index af8d0ec..9b10de5 100644 (file)
@@ -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;
         }
index fd54023..5157b94 100644 (file)
@@ -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) {
index 2e662c9..d40c0b0 100644 (file)
@@ -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);
 
index 0c0d2f6..8471d31 100644 (file)
@@ -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) {