Summary Files Commits Branches @commit @diff @stats
path: root / uri.c
diff options
context:
space:
mode:
Commit06b34a53c2c7af02fbc673d53b5c13f40c2933a6 (patch)
Author
 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
2019-12-16 01:47:12 +0100
Committer
 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
2019-12-16 01:47:12 +0100
Filesf0eb17efd68371a50e2bf670d13cece977b3d7bb /uri.c
goodwolf-06b34a53c2c7af02fbc673d53b5c13f40c2933a6.tar
goodwolf-06b34a53c2c7af02fbc673d53b5c13f40c2933a6.tar.gz
goodwolf-06b34a53c2c7af02fbc673d53b5c13f40c2933a6.zip
Parent4e144dd01af4855e02ed394c75e8c04c4dae710b (diff)
Add tests on badwolf_ensure_uri_scheme
1 files changed, 39 insertions, 0 deletions
-rw-r--r--uri.c39
diff --git a/uri.c b/uri.c
new file mode 100644
index 0000000..bc2bc01
--- /dev/null
+++ b/uri.c
@@ -0,0 +1,39 @@
+// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
+// Copyright © 2019 Haelwenn (lanodan) Monnier <contact@hacktivis.me>
+// SPDX-License-Identifier: BSD-3-Clause
+
+#include "uri.h"
+
+#include <glib/gprintf.h> /* g_fprintf() */
+#include <libsoup/soup.h> /* soup* */
+#include <unistd.h> /* access() */
+
+const gchar *
+badwolf_ensure_uri_scheme(const gchar *text, gboolean try_file)
+{
+ const gchar *fallback = "about:blank";
+ SoupURI *uri;
+
+ uri = soup_uri_new(text);
+ if(!uri) return fallback;
+
+ //g_fprintf(stderr, "uri->scheme = %s, uri->path = %s\n", uri->scheme, uri->path);
+
+ if(!uri->path)
+ {
+ soup_uri_free(uri);
+ return fallback;
+ }
+
+ if(uri->scheme)
+ {
+ soup_uri_free(uri);
+ return text;
+ }
+
+ soup_uri_free(uri);
+
+ if(try_file && (access(text, R_OK) == 0)) return g_strdup_printf("file://%s", text);
+
+ return g_strdup_printf("http://%s", text);
+}