summaryrefslogtreecommitdiffstats
path: root/lib/remote/url.cpp
blob: e87628eed7f1ba5ff47087c57d638415444f9674 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/array.hpp"
#include "base/utility.hpp"
#include "base/objectlock.hpp"
#include "remote/url.hpp"
#include "remote/url-characters.hpp"
#include <boost/tokenizer.hpp>

using namespace icinga;

Url::Url(const String& base_url)
{
	String url = base_url;

	if (url.GetLength() == 0)
		BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL Empty URL."));

	size_t pHelper = String::NPos;
	if (url[0] != '/')
		pHelper = url.Find(":");

	if (pHelper != String::NPos) {
		if (!ParseScheme(url.SubStr(0, pHelper)))
			BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL Scheme."));
		url = url.SubStr(pHelper + 1);
	}

	if (*url.Begin() != '/')
		BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL: '/' expected after scheme."));

	if (url.GetLength() == 1) {
		return;
	}

	if (*(url.Begin() + 1) == '/') {
		pHelper = url.Find("/", 2);

		if (pHelper == String::NPos)
			BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL: Missing '/' after authority."));

		if (!ParseAuthority(url.SubStr(0, pHelper)))
			BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL Authority"));

		url = url.SubStr(pHelper);
	}

	if (*url.Begin() == '/') {
		pHelper = url.FindFirstOf("#?");
		if (!ParsePath(url.SubStr(1, pHelper - 1)))
			BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL Path"));

		if (pHelper != String::NPos)
			url = url.SubStr(pHelper);
	} else
		BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL: Missing path."));

	if (*url.Begin() == '?') {
		pHelper = url.Find("#");
		if (!ParseQuery(url.SubStr(1, pHelper - 1)))
			BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL Query"));

		if (pHelper != String::NPos)
			url = url.SubStr(pHelper);
	}

	if (*url.Begin() == '#') {
		if (!ParseFragment(url.SubStr(1)))
			BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid URL Fragment"));
	}
}

String Url::GetScheme() const
{
	return m_Scheme;
}

String Url::GetAuthority() const
{
	if (m_Host.IsEmpty())
		return "";

	String auth;
	if (!m_Username.IsEmpty()) {
		auth = m_Username;
		if (!m_Password.IsEmpty())
			auth += ":" + m_Password;
		auth += "@";
	}

	auth += m_Host;

	if (!m_Port.IsEmpty())
		auth += ":" + m_Port;

	return auth;
}

String Url::GetUsername() const
{
	return m_Username;
}

String Url::GetPassword() const
{
	return m_Password;
}

String Url::GetHost() const
{
	return m_Host;
}

String Url::GetPort() const
{
	return m_Port;
}

const std::vector<String>& Url::GetPath() const
{
	return m_Path;
}

const std::vector<std::pair<String, String>>& Url::GetQuery() const
{
	return m_Query;
}

String Url::GetFragment() const
{
	return m_Fragment;
}

void Url::SetScheme(const String& scheme)
{
	m_Scheme = scheme;
}

void Url::SetUsername(const String& username)
{
	m_Username = username;
}

void Url::SetPassword(const String& password)
{
	m_Password = password;
}

void Url::SetHost(const String& host)
{
	m_Host = host;
}

void Url::SetPort(const String& port)
{
	m_Port = port;
}

void Url::SetPath(const std::vector<String>& path)
{
	m_Path = path;
}

void Url::SetQuery(const std::vector<std::pair<String, String>>& query)
{
	m_Query = query;
}

void Url::SetArrayFormatUseBrackets(bool useBrackets)
{
	m_ArrayFormatUseBrackets = useBrackets;
}

void Url::AddQueryElement(const String& name, const String& value)
{
	m_Query.emplace_back(name, value);
}

void Url::SetFragment(const String& fragment) {
	m_Fragment = fragment;
}

String Url::Format(bool onlyPathAndQuery, bool printCredentials) const
{
	String url;

	if (!onlyPathAndQuery) {
		if (!m_Scheme.IsEmpty())
			url += m_Scheme + ":";

		if (printCredentials && !GetAuthority().IsEmpty())
			url += "//" + GetAuthority();
		else if (!GetHost().IsEmpty())
			url += "//" + GetHost() + (!GetPort().IsEmpty() ? ":" + GetPort() : "");
	}

	if (m_Path.empty())
		url += "/";
	else {
		for (const String& segment : m_Path) {
			url += "/";
			url += Utility::EscapeString(segment, ACPATHSEGMENT_ENCODE, false);
		}
	}

	String param;
	if (!m_Query.empty()) {
		typedef std::pair<String, std::vector<String> > kv_pair;

		for (const auto& kv : m_Query) {
			String key = Utility::EscapeString(kv.first, ACQUERY_ENCODE, false);
			if (param.IsEmpty())
				param = "?";
			else
				param += "&";

			param += key;
			param += kv.second.IsEmpty() ?
				String() : "=" + Utility::EscapeString(kv.second, ACQUERY_ENCODE, false);
		}
	}

	url += param;

	if (!m_Fragment.IsEmpty())
		url += "#" + Utility::EscapeString(m_Fragment, ACFRAGMENT_ENCODE, false);

	return url;
}

bool Url::ParseScheme(const String& scheme)
{
	m_Scheme = scheme;

	if (scheme.FindFirstOf(ALPHA) != 0)
		return false;

	return (ValidateToken(scheme, ACSCHEME));
}

bool Url::ParseAuthority(const String& authority)
{
	String auth = authority.SubStr(2);
	size_t pos = auth.Find("@");
	if (pos != String::NPos && pos != 0) {
		if (!Url::ParseUserinfo(auth.SubStr(0, pos)))
			return false;
		auth = auth.SubStr(pos+1);
	}

	pos = auth.Find(":");
	if (pos != String::NPos) {
		if (pos == 0 || pos == auth.GetLength() - 1 || !Url::ParsePort(auth.SubStr(pos+1)))
			return false;
	}

	m_Host = auth.SubStr(0, pos);
	return ValidateToken(m_Host, ACHOST);
}

bool Url::ParseUserinfo(const String& userinfo)
{
	size_t pos = userinfo.Find(":");
	m_Username = userinfo.SubStr(0, pos);
	if (!ValidateToken(m_Username, ACUSERINFO))
		return false;
	m_Username = Utility::UnescapeString(m_Username);
	if (pos != String::NPos && pos != userinfo.GetLength() - 1) {
		m_Password = userinfo.SubStr(pos+1);
		if (!ValidateToken(m_Username, ACUSERINFO))
			return false;
		m_Password = Utility::UnescapeString(m_Password);
	} else
		m_Password = "";

	return true;
}

bool Url::ParsePort(const String& port)
{
	m_Port = Utility::UnescapeString(port);
	if (!ValidateToken(m_Port, ACPORT))
		return false;
	return true;
}

bool Url::ParsePath(const String& path)
{
	const std::string& pathStr = path;
	boost::char_separator<char> sep("/");
	boost::tokenizer<boost::char_separator<char> > tokens(pathStr, sep);

	for (const String& token : tokens) {
		if (token.IsEmpty())
			continue;

		if (!ValidateToken(token, ACPATHSEGMENT))
			return false;

		m_Path.emplace_back(Utility::UnescapeString(token));
	}

	return true;
}

bool Url::ParseQuery(const String& query)
{
	/* Tokenizer does not like String AT ALL */
	const std::string& queryStr = query;
	boost::char_separator<char> sep("&");
	boost::tokenizer<boost::char_separator<char> > tokens(queryStr, sep);

	for (const String& token : tokens) {
		size_t pHelper = token.Find("=");

		if (pHelper == 0)
			// /?foo=bar&=bar == invalid
			return false;

		String key = token.SubStr(0, pHelper);
		String value = Empty;

		if (pHelper != String::NPos && pHelper != token.GetLength() - 1)
			value = token.SubStr(pHelper+1);

		if (!ValidateToken(value, ACQUERY))
			return false;

		value = Utility::UnescapeString(value);

		pHelper = key.Find("[]");

		if (pHelper == 0 || (pHelper != String::NPos && pHelper != key.GetLength()-2))
			return false;

		key = key.SubStr(0, pHelper);

		if (!ValidateToken(key, ACQUERY))
			return false;

		m_Query.emplace_back(Utility::UnescapeString(key), std::move(value));
	}

	return true;
}

bool Url::ParseFragment(const String& fragment)
{
	m_Fragment = Utility::UnescapeString(fragment);

	return ValidateToken(fragment, ACFRAGMENT);
}

bool Url::ValidateToken(const String& token, const String& symbols)
{
	for (const char ch : token) {
		if (symbols.FindFirstOf(ch) == String::NPos)
			return false;
	}

	return true;
}