Summary Files Commits Branches @commit @diff @stats
path: root / uri.c
blob: bc2bc0122a7875a2e7e78d02b1074a013dccb02d (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
// 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);
}