Summary Files Commits Branches @commit @diff @stats
path: root / uri.c
blob: 912bf8654a0d77662fa97be1f565c81e9b9344f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
// Copyright © 2019-2020 Badwolf Authors <https://hacktivis.me/projects/badwolf>
// SPDX-License-Identifier: BSD-3-Clause

#include "uri.h"

#include <glib.h>   /* g_strcmp0(), g_uri_parse_scheme(), g_strdup_printf */
#include <stdlib.h> /* realpath(), free() */
#include <unistd.h> /* access() */

const gchar *
badwolf_ensure_uri_scheme(const gchar *text, gboolean try_file)
{
	const gchar *fallback = "about:blank";
	char *path            = NULL;

	if(g_strcmp0(text, "") <= 0) return fallback;

	if(g_uri_parse_scheme(text)) return text;

	if(try_file)
	{
		path     = realpath(text, NULL);
		gchar *f = NULL;

		if(path != NULL)
		{
			if(access(path, R_OK) == 0)
			{
				f = g_strdup_printf("file://%s", path);
			}

			free(path);

			return f;
		}
	}

	return g_strdup_printf("http://%s", text);
}