From fac69afd2d7c17e16d3ffd8ef265f39d512c21ed Mon Sep 17 00:00:00 2001 From: George Bateman Date: Wed, 13 Jun 2018 23:38:10 +0100 Subject: [PATCH] Make URL detection more robust --- src/main.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 0d34ac5..7f9731d 100644 --- a/src/main.c +++ b/src/main.c @@ -352,6 +352,21 @@ void vb_input_update_style(Client *c) } } +/** + * Tests if a path is likely intended to be an URI (given that it's not a file + * path or containing "://"). + */ +gboolean vb_plausible_uri(const char *path) { + if (strchr(path, ' ')) return FALSE; + if (strchr(path, '.')) return TRUE; + const char *i, *j; + if ((i = strstr(path, "localhost")) && + (i == path || i[-1] == '/' || i[-1] == '@') && + (i[9] == 0 || i[9] == '/' || i[9] == ':')) return TRUE; + return ((i = strchr(path, '[')) && (j = strchr(i, ':')) && strchr(j, ']')) + ? TRUE : FALSE; +} + /** * Load the a uri given in Arg. This function handles also shortcuts and local * file paths. @@ -382,11 +397,9 @@ gboolean vb_load_uri(Client *c, const Arg *arg) rp = realpath(path, NULL); uri = g_strconcat("file://", rp, NULL); free(rp); - } else if (strchr(path, ' ') || !(strchr(path, '.') || - (strchr(path, '[') && strchr(path, ':') && strchr(path, ']')) || - strstr(path, "localhost"))) { + } else if (!vb_plausible_uri(path)) { /* use a shortcut if path contains spaces or doesn't contain typical - * characters ('.', [:] for IPv6 addresses, 'localhost') */ + * tokens ('.', [:] for IPv6 addresses, 'localhost') */ uri = shortcut_get_uri(c->config.shortcuts, path); } -- 2.20.1