Files @ 6e1af765cdc0
Branch filter:

Location: libtransport.git/spectrum_manager/src/APIServer.cpp

Jan Kaluza
AdminInterface: Support labels for commands
  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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include "APIServer.h"
#include "methods.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <pthread.h>
#include <fstream>
#include <string>
#include <cerrno>

#include "rapidjson/rapidjson.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

#include <boost/tokenizer.hpp>
using boost::tokenizer;
using boost::escaped_list_separator;

#define ALLOW_ONLY_ADMIN() 	if (!session->admin) { \
		send_ack(conn, true, "Only administrators can do this API call."); \
		return; \
	}

static std::string get_http_var(const struct http_message *hm, const char *name) {
	char data[4096];
	data[0] = '\0';

	mg_get_http_var(&hm->body, name, data, sizeof(data));
	if (data[0] != '\0') {
		return data;
	}

	mg_get_http_var(&hm->query_string, name, data, sizeof(data));
	if (data[0] != '\0') {
		return data;
	}

	return "";
}

static int has_prefix(const struct mg_str *uri, const char *prefix) {
	size_t prefix_len = strlen(prefix);
	return uri->len >= prefix_len && memcmp(uri->p, prefix, prefix_len) == 0;
}

APIServer::APIServer(ManagerConfig *config, StorageBackend *storage) {
	m_config = config;
	m_storage = storage;
}

APIServer::~APIServer() {
}

void APIServer::send_json(struct mg_connection *conn, const Document &d) {
	StringBuffer buffer;
	Writer<StringBuffer> writer(buffer);
	d.Accept(writer);
	std::string json(buffer.GetString());

	std::cout << "Sending JSON:\n";
	std::cout << json << "\n";
	mg_printf(conn,
			"HTTP/1.1 200 OK\r\n"
			"Content-Type: text/json\r\n"
			"Content-Length: %d\r\n"
			"\r\n"
			"%s",
			(int) json.size(), json.c_str());
}

void APIServer::send_ack(struct mg_connection *conn, bool error, const std::string &message) {
	Document json;
	json.SetObject();
	json.AddMember("error", error, json.GetAllocator());
	json.AddMember("message", message.c_str(), json.GetAllocator());

	send_json(conn, json);
}

void APIServer::serve_instances(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	// rapidjson stores const char* pointer to status, so we have to keep
	// the std::string stored out of BOOST_FOREACH scope, otherwise the
	// const char * returned by c_str() would be invalid during send_json.
	std::vector<std::string> statuses;
	std::vector<std::string> usernames;
	std::vector<std::string> list = show_list(m_config, false);

	Document json;
	json.SetObject();
	json.AddMember("error", 0, json.GetAllocator());

	Value instances(kArrayType);
	BOOST_FOREACH(std::string &id, list) {
		Value instance;
		instance.SetObject();
		instance.AddMember("id", id.c_str(), json.GetAllocator());

		std::string name = get_config(m_config, id, "identity.name");
		if (name.empty() || name == "Spectrum 2 Transport") {
			instance.AddMember("name", id.c_str(), json.GetAllocator());
		}
		else {
			statuses.push_back(name);
			instance.AddMember("name", statuses.back().c_str(), json.GetAllocator());
		}

		std::string status = server->send_command(id, "status");
		if (status.empty()) {
			status = "Cannot get the instance status.";
		}

		statuses.push_back(status);
		instance.AddMember("status", statuses.back().c_str(), json.GetAllocator());

		bool running = true;
		if (status.find("Running") == std::string::npos) {
			running = false;
		}
		instance.AddMember("running", running, json.GetAllocator());

		UserInfo info;
		m_storage->getUser(session->user, info);
		std::string username = "";
		int type = (int) TYPE_STRING;
		m_storage->getUserSetting(info.id, id, type, username);

		usernames.push_back(username);
		instance.AddMember("registered", !username.empty(), json.GetAllocator());
		instance.AddMember("username", usernames.back().c_str(), json.GetAllocator());

		usernames.push_back(get_config(m_config, id, "service.frontend"));
		instance.AddMember("frontend", usernames.back().c_str(), json.GetAllocator());

		instances.PushBack(instance, json.GetAllocator());
	}

	json.AddMember("instances", instances, json.GetAllocator());
	send_json(conn, json);
}

void APIServer::serve_instances_start(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	ALLOW_ONLY_ADMIN();

	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);
	start_instances(m_config, instance);
	std::string response = get_response();

	// TODO: So far it needs some time to reload Spectrum 2, so just sleep here.
	sleep(1);

	send_ack(conn, response.find("OK") == std::string::npos, response);
}

void APIServer::serve_instances_stop(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	ALLOW_ONLY_ADMIN();

	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);
	stop_instances(m_config, instance);
	std::string response = get_response();
	send_ack(conn, response.find("OK") == std::string::npos, response);
}

void APIServer::serve_instances_unregister(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);

	UserInfo info;
	m_storage->getUser(session->user, info);

	std::string username = "";
	int type = (int) TYPE_STRING;
	m_storage->getUserSetting(info.id, instance, type, username);

	if (username.empty() && session->admin) {
		username = get_http_var(hm, "command_arg0");
	}

	if (!username.empty()) {
		std::string response = server->send_command(instance, "unregister " + username);
		if (!response.empty()) {
			username = "";
			m_storage->updateUserSetting(info.id, instance, username);
			send_ack(conn, false, response);
		}
		else {
			send_ack(conn, true, "Unknown error.");
		}
	}
	else {
		send_ack(conn, true, "You are not registered to this Spectrum 2 instance.");
	}
}

void APIServer::serve_instances_register(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);

	UserInfo info;
	m_storage->getUser(session->user, info);

	std::string username = "";
	int type = (int) TYPE_STRING;
	m_storage->getUserSetting(info.id, instance, type, username);

	std::string jid = get_http_var(hm, "jid");
	std::string uin = get_http_var(hm, "uin");
	std::string password = get_http_var(hm, "password");

	// For some networks like IRC, there is no registration.
	// We detect such networks according to registration_fields and use
	// "unknown" uin for them.
	if (uin.empty() || password.empty()) {
		std::string response = server->send_command(instance, "registration_fields");
		std::vector<std::string> fields;
		boost::split(fields, response, boost::is_any_of("\n"));
		if (fields.size() == 1) {
			uin = "unknown";
			password = "unknown";
		}
	}

	if (jid.empty() || uin.empty() || password.empty()) {
		send_ack(conn, true, "Insufficient data.");
	}
	else {
		// Check if the frontend wants to use OAuth2 (Slack for example).
		std::string response = server->send_command(instance, "get_oauth2_url " + jid + " " + uin + " " + password);
		if (!response.empty() && response.find("Error:") != 0) {
			Document json;
			json.SetObject();
			json.AddMember("error", false, json.GetAllocator());
			json.AddMember("oauth2_url", response.c_str(), json.GetAllocator());
			send_json(conn, json);
		}
		else {
			response = server->send_command(instance, "register " + jid + " " + uin + " " + password);
			if (!response.empty()) {
				std::string value = jid;
				int type = (int) TYPE_STRING;
				m_storage->updateUserSetting(info.id, instance, value);
				send_ack(conn, false, response);
			}
			else {
				send_ack(conn, true, response);
				return;
			}
		}
	}
}



// void APIServer::serve_instances_register_form(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
// 	std::string uri(hm->uri.p, hm->uri.len);
// 	std::string instance = uri.substr(uri.rfind("/") + 1);
// 
// 	std::string response = server->send_command(instance, "registration_fields");
// 	std::vector<std::string> fields;
// 	boost::split(fields, response, boost::is_any_of("\n"));
// 
// 	if (fields.empty()) {
// 		fields.push_back("Jabber ID");
// 		fields.push_back("3rd-party network username");
// 		fields.push_back("3rd-party network password");
// 	}
// 
// 	Document json;
// 	json.SetObject();
// 	json.AddMember("error", 0, json.GetAllocator());
// 	json.AddMember("username_label", fields[0].c_str(), json.GetAllocator());
// 	json.AddMember("legacy_username_label", fields.size() >= 2 ? fields[1].c_str() : "", json.GetAllocator());
// 	json.AddMember("password_label", fields.size() >= 3 ? fields[2].c_str() : "", json.GetAllocator());
// 	send_json(conn, json);
// }

void APIServer::serve_instances_commands(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);

	UserInfo info;
	m_storage->getUser(session->user, info);

	std::string username = "";
	int type = (int) TYPE_STRING;
	m_storage->getUserSetting(info.id, instance, type, username);

	std::string response = server->send_command(instance, "commands");

	std::vector<std::string> commands;
	boost::split(commands, response, boost::is_any_of("\n"));

	Document json;
	json.SetObject();
	json.AddMember("error", 0, json.GetAllocator());

	std::vector<std::vector<std::string> > tmp;
	Value cmds(kArrayType);
	BOOST_FOREACH(const std::string &command, commands) {
		escaped_list_separator<char> els('\\', ' ', '\"');
		tokenizer<escaped_list_separator<char> > tok(command, els);

		std::vector<std::string> tokens;
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
			tokens.push_back(*beg);
		}

		if (tokens.size() != 11) {
			continue;
		}

		if (!session->admin && tokens[6] == "Admin") {
			continue;
		}

		// Skip command which needs registered users.
		if (!session->admin && username.empty() && tokens[8] == "User") {
			continue;
		}


		// Skip 'register' command when user is registered.
		if (!session->admin && !username.empty() && tokens[0] == "register") {
			continue;
		}

		tmp.push_back(tokens);

		Value cmd;
		cmd.SetObject();
		cmd.AddMember("name", tokens[0].c_str(), json.GetAllocator());
		cmd.AddMember("desc", tokens[2].c_str(), json.GetAllocator());
		cmd.AddMember("category", tokens[4].c_str(), json.GetAllocator());
		cmd.AddMember("context", tokens[8].c_str(), json.GetAllocator());
		cmd.AddMember("label", tokens[10].c_str(), json.GetAllocator());
		cmds.PushBack(cmd, json.GetAllocator());
	}

	json.AddMember("commands", cmds, json.GetAllocator());
	send_json(conn, json);
}

void APIServer::serve_instances_variables(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);

	UserInfo info;
	m_storage->getUser(session->user, info);

	std::string username = "";
	int type = (int) TYPE_STRING;
	m_storage->getUserSetting(info.id, instance, type, username);

	std::string response = server->send_command(instance, "variables");

	std::vector<std::string> commands;
	boost::split(commands, response, boost::is_any_of("\n"));

	Document json;
	json.SetObject();
	json.AddMember("error", 0, json.GetAllocator());

	std::vector<std::vector<std::string> > tmp;
	Value cmds(kArrayType);
	BOOST_FOREACH(const std::string &command, commands) {
		escaped_list_separator<char> els('\\', ' ', '\"');
		tokenizer<escaped_list_separator<char> > tok(command, els);

		std::vector<std::string> tokens;
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
			tokens.push_back(*beg);
		}

		if (tokens.size() != 13) {
			continue;
		}

		if (!session->admin && tokens[10] == "Admin") {
			continue;
		}

		tmp.push_back(tokens);

		Value cmd;
		cmd.SetObject();
		cmd.AddMember("name", tokens[0].c_str(), json.GetAllocator());
		cmd.AddMember("desc", tokens[2].c_str(), json.GetAllocator());
		cmd.AddMember("value", tokens[4].c_str(), json.GetAllocator());
		cmd.AddMember("read_only", tokens[6].c_str(), json.GetAllocator());
		cmd.AddMember("category", tokens[8].c_str(), json.GetAllocator());
		cmd.AddMember("context", tokens[12].c_str(), json.GetAllocator());
		cmds.PushBack(cmd, json.GetAllocator());
	}

	json.AddMember("variables", cmds, json.GetAllocator());
	send_json(conn, json);
}


void APIServer::serve_instances_command_args(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);
	std::string command = get_http_var(hm, "command");
	boost::trim(command);

	std::string response = server->send_command(instance, "commands");

	bool found = false;
	bool userContext = false;
	std::vector<std::string> commands;
	boost::split(commands, response, boost::is_any_of("\n"));
	BOOST_FOREACH(const std::string &cmd, commands) {
		escaped_list_separator<char> els('\\', ' ', '\"');
		tokenizer<escaped_list_separator<char> > tok(cmd, els);

		std::vector<std::string> tokens;
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
			tokens.push_back(*beg);
		}

		if (tokens.size() != 11) {
			continue;
		}

		std::cout << tokens[0] << " " << command << "\n";
		if (tokens[0] != command) {
			continue;
		}

		if (!session->admin && tokens[6] == "Admin") {
			send_ack(conn, false, "Only admin is able to query this command.");
			return;
		}

		if (tokens[8] == "User") {
			userContext = true;
		}

		found = true;
		break;
	}

	if (!found) {
		command = "unknown";
	}

	response = server->send_command(instance, "args " + command);
	if (response.find("Error:") == 0) {
		send_ack(conn, false, response);
		return;
	}

	

	std::vector<std::string> args;
	boost::split(args, response, boost::is_any_of("\n"));

	Document json;
	json.SetObject();
	json.AddMember("error", 0, json.GetAllocator());

	std::vector<std::vector<std::string> > tmp;
	Value argList(kArrayType);

	if (userContext && session->admin) {
		Value arg;
		arg.SetObject();
		arg.AddMember("name", "username", json.GetAllocator());
		arg.AddMember("label", "Username", json.GetAllocator());
		arg.AddMember("example", "", json.GetAllocator());
		argList.PushBack(arg, json.GetAllocator());
	}

	BOOST_FOREACH(const std::string &argument, args) {
		escaped_list_separator<char> els('\\', ' ', '\"');
		tokenizer<escaped_list_separator<char> > tok(argument, els);

		std::vector<std::string> tokens;
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
			tokens.push_back(*beg);
		}

		if (tokens.size() != 5) {
			continue;
		}

		tmp.push_back(tokens);

		Value arg;
		arg.SetObject();
		arg.AddMember("name", tokens[0].c_str(), json.GetAllocator());
		arg.AddMember("label", tokens[2].c_str(), json.GetAllocator());
		arg.AddMember("example", tokens[4].c_str(), json.GetAllocator());
		argList.PushBack(arg, json.GetAllocator());
	}

	json.AddMember("args", argList, json.GetAllocator());
	send_json(conn, json);
}


void APIServer::serve_instances_execute(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	std::string uri(hm->uri.p, hm->uri.len);
	std::string instance = uri.substr(uri.rfind("/") + 1);
	std::string command = get_http_var(hm, "command");
	boost::trim(command);

	std::string response = server->send_command(instance, "commands");

	bool found = false;
	bool userContext = false;
	std::vector<std::string> commands;
	boost::split(commands, response, boost::is_any_of("\n"));
	BOOST_FOREACH(const std::string &cmd, commands) {
		escaped_list_separator<char> els('\\', ' ', '\"');
		tokenizer<escaped_list_separator<char> > tok(cmd, els);

		std::vector<std::string> tokens;
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
			tokens.push_back(*beg);
		}

		if (tokens.size() != 11) {
			continue;
		}

		std::cout << tokens[0] << " " << command << "\n";
		if (tokens[0] != command) {
			continue;
		}

		if (!session->admin && tokens[6] == "Admin") {
			send_ack(conn, false, "Only admin is able to execute.");
			return;
		}

		if (tokens[8] == "User") {
			userContext = true;
		}

		found = true;
		break;
	}

	if (!found) {
		command = "unknown";
	}

	UserInfo info;
	m_storage->getUser(session->user, info);
	std::string username = "";
	int type = (int) TYPE_STRING;
	m_storage->getUserSetting(info.id, instance, type, username);

	if (userContext && !session->admin) {
		if (username.empty()) {
			send_ack(conn, false, "Error: You are not registered to this transport instance.");
			return;
		}

		command += " " + username;
	}

	for (int i = 0; i < 10; ++i) {
		std::string var = get_http_var(hm, std::string(std::string("command_arg") + boost::lexical_cast<std::string>(i)).c_str());
		if (!var.empty()) {
			command += " " + var;
		}
	}

	response = server->send_command(instance, command);
	boost::replace_all(response, "\n", "<br/>");
	if (response.find("Error:") == 0) {
		send_ack(conn, false, response);
	}
	else {
		send_ack(conn, true, response);
	}
}

void APIServer::serve_users(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	ALLOW_ONLY_ADMIN();

	Document json;
	json.SetObject();
	json.AddMember("error", 0, json.GetAllocator());

	std::vector<std::string> list;
	m_storage->getUsers(list);

	Value users(kArrayType);
	BOOST_FOREACH(std::string &id, list) {
		Value user;
		user.SetObject();
		user.AddMember("username", id.c_str(), json.GetAllocator());
		users.PushBack(user, json.GetAllocator());
	}

	json.AddMember("users", users, json.GetAllocator());
	send_json(conn, json);
}

void APIServer::serve_users_add(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	std::string user = get_http_var(hm, "username");
	std::string password = get_http_var(hm, "password");

	if (!user.empty() && !password.empty()) {
		if (m_storage) {
			UserInfo dummy;
			bool registered = m_storage->getUser(user, dummy);
			if (!registered) {
				UserInfo info;
				info.jid = user;
				info.password = password;
				m_storage->setUser(info);
			}
			else {
				send_ack(conn, true, "This user is already registered");
				return;
			}
		}
		else {
			send_ack(conn, false, "Storage backend is not configured.");
		}
	}
	else {
		send_ack(conn, true, "Username or password has not been provided.");
		return;
	}
	send_ack(conn, false, "");
}

void APIServer::serve_users_remove(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) {
	ALLOW_ONLY_ADMIN();

	std::string uri(hm->uri.p, hm->uri.len);
	std::string user = uri.substr(uri.rfind("/") + 1);

	if (!m_storage) {
		return;
	}

	UserInfo info;
	m_storage->getUser(user, info);
	m_storage->removeUser(info.id);

	send_ack(conn, false, "");
}

void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm) {
	if (has_prefix(&hm->uri, "/api/v1/instances/start/")) {
		serve_instances_start(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/instances/stop/")) {
		serve_instances_stop(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/instances/unregister/")) {
		serve_instances_unregister(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/instances/register/")) {
		serve_instances_register(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/instances/commands/")) {
		serve_instances_commands(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/instances/variables/")) {
		serve_instances_variables(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/instances/command_args/")) {
		serve_instances_command_args(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/instances/execute/")) {
		serve_instances_execute(server, sess, conn, hm);
	}
	else if (has_prefix(&hm->uri, "/api/v1/users/remove/")) {
		serve_users_remove(server, sess, conn, hm);
	}
	else if (mg_vcmp(&hm->uri, "/api/v1/users/add") == 0) {
		serve_users_add(server, sess, conn, hm);
	}
	else if (mg_vcmp(&hm->uri, "/api/v1/users") == 0) {
		serve_users(server, sess, conn, hm);
	}
	else if (mg_vcmp(&hm->uri, "/api/v1/instances") == 0) {
		serve_instances(server, sess, conn, hm);
	}
}