summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKA/Sources/URL.cxx
blob: 189aba8eda2741578d2207bbe03f5c357f1d4c59 (plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -------------------------------------------

	Copyright ZKA Technologies.

------------------------------------------- */

#include <CFKit/URL.hxx>
#include <KernelKit/DebugOutput.hxx>
#include <NewKit/Utils.hxx>

/// BUGS: 0

namespace Kernel
{
	URL::URL(StringView& strUrl)
		: fUrlView(strUrl, false)
	{
	}

	URL::~URL() = default;

	/// @brief internal and reserved protocols by kernel.
	constexpr const Char* kURLProtocols[] = {
		"file", // Filesystem protocol
		"zup",	// ZKA update protocol
		"oscc", // Open System Configuration Connectivity.
		"odbc", // ODBC connectivity.
		"https", // HTTPS layer driver (HTTPS.sys). 
	};

	constexpr const int kUrlOutSz	 = 1; //! such as: ://
	constexpr const int kProtosCount = 5;
	constexpr const int kRangeSz	 = 4096;

	ErrorOr<StringView> url_extract_location(const Char* url)
	{
		if (!url || *url == 0 || rt_string_len(url, kRangeSz) > kRangeSz)
			return ErrorOr<StringView>{-1};

		StringView view(rt_string_len(url));

		SizeT i			   = 0;
		bool  scheme_found = false;

		for (; i < rt_string_len(url); ++i)
		{
			if (!scheme_found)
			{
				for (int y = 0; kProtosCount; ++y)
				{
					if (rt_string_in_string(view.CData(), kURLProtocols[y]))
					{
						i += rt_string_len(kURLProtocols[y]) + kUrlOutSz;
						scheme_found = true;

						break;
					}
				}
			}

			view.Data()[i] = url[i];
		}

		return ErrorOr<StringView>(view);
	}

	ErrorOr<StringView> url_extract_protocol(const Char* url)
	{
		if (!url || *url == 0 || rt_string_len(url, kRangeSz) > kRangeSz)
			return ErrorOr<StringView>{-1};

		ErrorOr<StringView> view{-1};

		return view;
	}

	Ref<ErrorOr<StringView>> URL::Location() noexcept
	{
		const Char* src = fUrlView.Leak().CData();
		auto		loc = url_extract_location(src);

		if (!loc)
			return {};

		return Ref<ErrorOr<StringView>>(loc);
	}

	Ref<ErrorOr<StringView>> URL::Protocol() noexcept
	{
		const Char* src = fUrlView.Leak().CData();
		auto		loc = url_extract_protocol(src);

		if (!loc)
			return {};

		return Ref<ErrorOr<StringView>>(loc);
	}
} // namespace Kernel