summaryrefslogtreecommitdiffstats
path: root/src/uscxml/server/HTTPServer.cpp
blob: cee8325a0ac3b7c58e048ee82b6107166435dab8 (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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/**
 *  @file
 *  @author     2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#include "uscxml/config.h"

#ifdef _WIN32
#define NOMINMAX
#include <winsock2.h>
#include <windows.h>
#endif

#include "HTTPServer.h"
#include "uscxml/util/DOM.h"

#include <string>

extern "C" {
#include <event2/dns.h>
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/keyvalq_struct.h>
#include <event2/http_struct.h>
#include <event2/thread.h>
}

#include "uscxml/interpreter/Logging.h"
#include <boost/algorithm/string.hpp>

#ifndef _WIN32
#include <netinet/in.h>                 // for INADDR_ANY
#include <stdint.h>                     // for uint16_t
#include <stdlib.h>                     // for NULL, free
#include <unistd.h>                     // for gethostname
//#include <netdb.h>
//#include <arpa/inet.h>
#else
#ifdef HTTPS_ENABLED
#define EVENT__HAVE_OPENSSL
#endif
#endif

#ifdef HTTPS_ENABLED
extern "C" {
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <event2/bufferevent_ssl.h>
}
#endif

#include "uscxml/util/Convenience.h"         // for toStr

#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif

namespace uscxml {

static void dummyCallback(evutil_socket_t fd, short what, void *arg) {
	// see comments in BasicDelayedEventQueue::run
	timeval tv;
	tv.tv_sec = 365 * 24 * 3600;
	tv.tv_usec = 0;
	event *ev = *(event **)arg;
	evtimer_add(ev, &tv);
}

HTTPServer::HTTPServer(unsigned short port, unsigned short wsPort, SSLConfig* sslConf) {
	_port = port;
	_base = event_base_new();
	_http = evhttp_new(_base);
	_evws = evws_new(_base);
	_thread = NULL;
	_httpHandle = NULL;

	timeval tv;
	tv.tv_sec = 365 * 24 * 3600;
	tv.tv_usec = 0;
	_dummyEvent = evtimer_new(_base, dummyCallback, &_dummyEvent);
	evtimer_add(_dummyEvent, &tv);

#ifdef _WIN32
	_wsHandle = NULL;
#else
	_wsHandle = 0;
#endif

	determineAddress();

	unsigned int allowedMethods =
	    EVHTTP_REQ_GET |
	    EVHTTP_REQ_POST |
	    EVHTTP_REQ_HEAD |
	    EVHTTP_REQ_PUT |
	    EVHTTP_REQ_DELETE |
	    EVHTTP_REQ_OPTIONS |
	    EVHTTP_REQ_TRACE |
	    EVHTTP_REQ_CONNECT |
	    EVHTTP_REQ_PATCH;

	evhttp_set_allowed_methods(_http, allowedMethods); // allow all methods

	if (_port > 0) {
		_httpHandle = evhttp_bind_socket_with_handle(_http, NULL, _port);
		if (_httpHandle) {
			LOGD(USCXML_INFO) << "HTTP server listening on tcp/" << _port << std::endl;;
		} else {
			LOGD(USCXML_ERROR) << "HTTP server cannot bind to tcp/" << _port << std::endl;
		}
	}

	_wsPort = wsPort;
	if (_wsPort > 0) {
		_wsHandle = evws_bind_socket(_evws, _wsPort);
		if (_wsHandle) {
			LOGD(USCXML_INFO) << "WebSocket server listening on tcp/" << _wsPort << std::endl;
		} else {
			LOGD(USCXML_ERROR) << "WebSocket server cannot bind to tcp/" << _wsPort << std::endl;
		}
	}

#ifdef HTTPS_ENABLED
	// have another look here https://github.com/ppelleti/https-example/blob/master/https-server.c

	if (!sslConf) {
		_sslPort = 0;
	} else {
		_sslPort = sslConf->port;

		SSL_library_init ();
		SSL_load_error_strings ();
		ERR_load_crypto_strings();
		OpenSSL_add_all_algorithms ();

		SSL_CTX *ctx = SSL_CTX_new (SSLv23_server_method ());
		SSL_CTX_set_options (ctx,
		                     SSL_OP_SINGLE_DH_USE |
		                     SSL_OP_SINGLE_ECDH_USE |
		                     SSL_OP_NO_SSLv2);

		EC_KEY *ecdh = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
		if (! ecdh) {
			LOGD(USCXML_ERROR) << ("EC_KEY_new_by_curve_name") << ERR_error_string(ERR_get_error(), NULL);
			goto FAIL_SSL_SETUP;
		}
		if (1 != SSL_CTX_set_tmp_ecdh (ctx, ecdh)) {
			LOGD(USCXML_ERROR) << ("SSL_CTX_set_tmp_ecdh") << ERR_error_string(ERR_get_error(), NULL);
			goto FAIL_SSL_SETUP;
		}

		if (1 != SSL_CTX_use_certificate_chain_file(ctx, sslConf->publicKey.c_str())) {
			LOGD(USCXML_ERROR) <<  ("SSL_CTX_use_certificate_chain_file") << ERR_error_string(ERR_get_error(), NULL);
			goto FAIL_SSL_SETUP;
		}

		if (1 != SSL_CTX_use_PrivateKey_file(ctx, sslConf->privateKey.c_str(), SSL_FILETYPE_PEM)) {
			LOGD(USCXML_ERROR) <<  ("SSL_CTX_use_PrivateKey_file") << ERR_error_string(ERR_get_error(), NULL);
			goto FAIL_SSL_SETUP;
		}

		if (1 != SSL_CTX_check_private_key(ctx)) {
			LOGD(USCXML_ERROR) <<  ("SSL_CTX_check_private_key") << ERR_error_string(ERR_get_error(), NULL);
			goto FAIL_SSL_SETUP;
		}

		_https = evhttp_new (_base);
		if (! _https) {
			LOGD(USCXML_ERROR) << ("Could not create evhttp for https");
			goto FAIL_SSL_SETUP;
		}

		/* This is the magic that lets evhttp use SSL. */
		evhttp_set_bevcb(_https, sslBufferEventCallback, ctx);

		/* This is the callback that gets called when a request comes in. */
		evhttp_set_gencb(_https, httpRecvReqCallback, NULL);

		if (_sslPort > 0) {
			_sslHandle = evhttp_bind_socket_with_handle(_https, "0.0.0.0", _sslPort);
			if (_sslHandle) {
				LOGD(USCXML_INFO) << "HTTPS server listening on tcp/" << _sslPort << std::endl;
			} else {
				LOGD(USCXML_ERROR) << "HTTPS server cannot bind to tcp/" << _sslPort << std::endl;

			}
		}


	}

FAIL_SSL_SETUP:
	;
#endif

//	evhttp_set_timeout(_http, 5);

	// generic http callbacks
	evhttp_set_gencb(_http, HTTPServer::httpRecvReqCallback, NULL);
	evws_set_gencb(_evws, HTTPServer::wsRecvReqCallback, NULL);
}

HTTPServer::~HTTPServer() {
	_isRunning = false;
	_thread->join();
	delete _thread;
}

HTTPServer* HTTPServer::_instance = NULL;
std::recursive_mutex HTTPServer::_instanceMutex;

HTTPServer* HTTPServer::getInstance(unsigned short port, unsigned short wsPort, SSLConfig* sslConf) {
//	std::lock_guard<std::recursive_mutex> lock(_instanceMutex);
	if (_instance == NULL) {
#ifdef _WIN32
		WSADATA wsaData;
		WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
#ifndef _WIN32
		evthread_use_pthreads();
#else
		evthread_use_windows_threads();
#endif
		_instance = new HTTPServer(port, wsPort, sslConf);

		// only start if we have something to do!
#ifdef HTTPS_ENABLED
		if (_instance->_httpHandle || _instance->_wsHandle || _instance->_sslHandle)
#else
		if (_instance->_httpHandle || _instance->_wsHandle)
#endif
			_instance->start();
	}
	return _instance;
}

/**
 * This callback is registered for all data received on websockets
 */
void HTTPServer::wsRecvReqCallback(struct evws_connection *conn, struct evws_frame *frame, void *callbackData) {
	WSFrame wsFrame;
	wsFrame.evwsConn = conn;

	struct evws_header *header;
	for (header = conn->headers.tqh_first; header; header = header->next.tqe_next) {
		wsFrame.data.compound["header"].compound[header->key] = Data(header->value, Data::VERBATIM);
	}

	switch (frame->opcode) {
	case EVWS_CONTINUATION_FRAME:
		wsFrame.data.compound["type"] = Data("continuation", Data::VERBATIM);
		wsFrame.data.compound["content"] = Data(std::string(frame->data, frame->size), Data::VERBATIM);
		break;
	case EVWS_TEXT_FRAME:
		wsFrame.data.compound["type"] = Data("text", Data::VERBATIM);
		wsFrame.data.compound["content"] = Data(std::string(frame->data, frame->size), Data::VERBATIM);
		break;
	case EVWS_BINARY_FRAME:
		wsFrame.data.compound["type"] = Data("binary", Data::VERBATIM);
		wsFrame.data.compound["content"] = Data(frame->data, frame->size, "application/octet-stream");
		break;
	case EVWS_CONNECTION_CLOSE:
		wsFrame.data.compound["type"] = Data("close", Data::VERBATIM);
		break;
	case EVWS_PING:
		wsFrame.data.compound["type"] = Data("ping", Data::VERBATIM);
		break;
	case EVWS_PONG:
		wsFrame.data.compound["type"] = Data("ping", Data::VERBATIM);
		break;
	}

	wsFrame.data.compound["uri"] = Data(HTTPServer::getBaseURL(WebSockets) + conn->uri, Data::VERBATIM);
	wsFrame.data.compound["path"] = Data(conn->uri, Data::VERBATIM);

	// try with the handler registered for path first
	bool answered = false;
	if (callbackData != NULL)
		answered = ((WebSocketServlet*)callbackData)->requestFromWS(conn, wsFrame);

	if (!answered)
		HTTPServer::getInstance()->processByMatchingServlet(conn, wsFrame);

}

/**
 * This callback is registered for all HTTP requests
 */
void HTTPServer::httpRecvReqCallback(struct evhttp_request *req, void *callbackData) {
	std::stringstream raw;

	struct evkeyvalq *headers;
	headers = evhttp_request_get_input_headers(req);

#if 0
	// first of all, see whether this is a websocket request
	const char* wsUpgrade = evhttp_find_header(headers, "Upgrade");
	const char* wsConnection = evhttp_find_header(headers, "Connection");
	if (wsUpgrade && wsConnection) {
		if (iequals(wsUpgrade, "websocket") && iequals(wsConnection, "Upgrade")) {
			// this is a websocket request! .. but we do not know how to decouple from evhttp
		}
	}
#endif

	evhttp_request_own(req);
	Request request;
	request.evhttpReq = req;

	switch (evhttp_request_get_command(req)) {
	case EVHTTP_REQ_GET:
		request.data.compound["type"] = Data("get", Data::VERBATIM);
		break;
	case EVHTTP_REQ_POST:
		request.data.compound["type"] = Data("post", Data::VERBATIM);
		break;
	case EVHTTP_REQ_HEAD:
		request.data.compound["type"] = Data("head", Data::VERBATIM);
		break;
	case EVHTTP_REQ_PUT:
		request.data.compound["type"] = Data("put", Data::VERBATIM);
		break;
	case EVHTTP_REQ_DELETE:
		request.data.compound["type"] = Data("delete", Data::VERBATIM);
		break;
	case EVHTTP_REQ_OPTIONS:
		request.data.compound["type"] = Data("options", Data::VERBATIM);
		break;
	case EVHTTP_REQ_TRACE:
		request.data.compound["type"] = Data("trace", Data::VERBATIM);
		break;
	case EVHTTP_REQ_CONNECT:
		request.data.compound["type"] = Data("connect", Data::VERBATIM);
		break;
	case EVHTTP_REQ_PATCH:
		request.data.compound["type"] = Data("patch", Data::VERBATIM);
		break;
	default:
		request.data.compound["type"] = Data("unknown", Data::VERBATIM);
		break;
	}
	raw << boost::to_upper_copy(request.data.compound["type"].atom);

	request.data.compound["remoteHost"] = Data(req->remote_host, Data::VERBATIM);
	request.data.compound["remotePort"] = Data(toStr(req->remote_port), Data::VERBATIM);
	request.data.compound["httpMajor"] = Data(toStr((unsigned short)req->major), Data::VERBATIM);
	request.data.compound["httpMinor"] = Data(toStr((unsigned short)req->minor), Data::VERBATIM);
	request.data.compound["uri"] = Data(HTTPServer::getBaseURL() + req->uri, Data::VERBATIM);

	char* pathCStr = evhttp_decode_uri(evhttp_uri_get_path(evhttp_request_get_evhttp_uri(req)));
	request.data.compound["path"] = Data(pathCStr, Data::VERBATIM);
	free(pathCStr);

	raw << " " << request.data.compound["path"].atom;
	const char* query = evhttp_uri_get_query(evhttp_request_get_evhttp_uri(req));
	if (query)
		raw << "?" << std::string(query);

	raw << " HTTP/" << request.data.compound["httpMajor"].atom << "." << request.data.compound["httpMinor"].atom;
	raw << std::endl;

	struct evkeyval *header;
	struct evbuffer *buf;

	// insert headers to event data
	for (header = headers->tqh_first; header; header = header->next.tqe_next) {
		request.data.compound["header"].compound[header->key] = Data(header->value, Data::VERBATIM);
		raw << header->key << ": " << header->value << std::endl;
	}
	raw << std::endl;

	// seperate path into components
	{
		std::stringstream ss(request.data.compound["path"].atom);
		std::string item;
		while(std::getline(ss, item, '/')) {
			if (item.length() == 0)
				continue;
			request.data.compound["pathComponent"].array.push_back(Data(item, Data::VERBATIM));
		}
	}
	// parse query string
	struct evkeyvalq params;
	struct evkeyval *param;

	evhttp_parse_query_str(query, &params);
	for (param = params.tqh_first; param; param = param->next.tqe_next) {
		request.data.compound["query"].compound[param->key] = Data(param->value, Data::VERBATIM);
	}
	evhttp_clear_headers(&params);

	// get content
	buf = evhttp_request_get_input_buffer(req);
	if (evbuffer_get_length(buf))
		request.data.compound["content"] = Data("", Data::VERBATIM);
	while (evbuffer_get_length(buf)) {
		int n;
		char cbuf[1024];
		n = evbuffer_remove(buf, cbuf, sizeof(buf)-1);
		if (n > 0) {
			request.data.compound["content"].atom.append(cbuf, n);
		}
	}

	raw << request.data.compound["content"].atom;

	// decode content
	if (request.data.compound.find("content") != request.data.compound.end() &&
	        request.data.compound["header"].compound.find("Content-Type") != request.data.compound["header"].compound.end()) {
		std::string contentType = request.data.compound["header"].compound["Content-Type"].atom;
		if (false) {
		} else if (iequals(contentType.substr(0, 33), "application/x-www-form-urlencoded")) {
			// this is a form submit
			std::stringstream ss(request.data.compound["content"].atom);
			std::string item;
			std::string key;
			std::string value;
			while(std::getline(ss, item, '&')) {
				if (item.length() == 0)
					continue;
				size_t equalPos = item.find('=');
				if (equalPos == std::string::npos)
					continue;

				key = item.substr(0, equalPos);
				value = item.substr(equalPos + 1, item.length() - (equalPos + 1));
				size_t keyCStrLen = 0;
				size_t valueCStrLen = 0;
				char* keyCStr = evhttp_uridecode(key.c_str(), 1, &keyCStrLen);
				char* valueCStr = evhttp_uridecode(value.c_str(), 1, &valueCStrLen);
				std::string decKey = std::string(keyCStr, keyCStrLen);
				std::string decValue = std::string(valueCStr, valueCStrLen);

				request.data.compound["content"].compound[decKey] = Data(decValue, Data::VERBATIM);
				free(keyCStr);
				free(valueCStr);
				key.clear();
			}
			request.data.compound["content"].atom.clear();
		} else if (iequals(contentType.substr(0, 16), "application/json")) {
			Data json = Data::fromJSON(request.data.compound["content"].atom);
			if (!json.empty()) {
				request.data.compound["content"] = json;
			}
		} else if (iequals(contentType.substr(0, 15), "application/xml")) {
			assert(0);
//            NameSpacingParser parser = NameSpacingParser::fromXML(request.data.compound["content"].atom);
//			if (parser.errorsReported()) {
//				LOG(USCXML_ERROR) << "Cannot parse contents of HTTP request as XML";
//			} else {
//				request.data.compound["content"].node = parser.getDocument().getDocumentElement();
//			}
		}
	}

	request.raw = raw.str();

#if 0
	std::cout << "====" << std::endl;
	std::cout << request.raw << std::endl;
	std::cout << "====" << std::endl;
#endif

	// try with the handler registered for path first
	bool answered = false;
	if (callbackData != NULL)
		answered = ((HTTPServlet*)callbackData)->requestFromHTTP(request);

	if (!answered)
		HTTPServer::getInstance()->processByMatchingServlet(request);
}

void HTTPServer::processByMatchingServlet(const Request& request) {
	std::lock_guard<std::recursive_mutex> lock(_mutex);

	http_servlet_iter_t servletIter = _httpServlets.begin();

	std::string actualPath = request.data.compound.at("path").atom;
	std::map<std::string, HTTPServlet*, comp_strsize_less> matches;

	while(servletIter != _httpServlets.end()) {
		// is the servlet path a prefix of the actual path?
		std::string servletPath = "/" + servletIter->first;
		if (servletIter->first.length() == 0) {
			matches.insert(std::make_pair(servletPath, servletIter->second)); // single servlet at root
		} else if (iequals(actualPath.substr(0, servletPath.length()), servletPath) && // servlet path is a prefix
		           iequals(actualPath.substr(servletPath.length(), 1), "/")) {     // and next character is a '/'
			matches.insert(std::make_pair(servletPath, servletIter->second));
		}
		servletIter++;
	}

	// process by best matching servlet until someone feels responsible
	std::map<std::string, HTTPServlet*, comp_strsize_less>::iterator matchesIter = matches.begin();
	while(matchesIter != matches.end()) {
		if (matchesIter->second->requestFromHTTP(request)) {
			return;
		}
		matchesIter++;
	}

	LOGD(USCXML_INFO) << "Got an HTTP request at " << actualPath << " but no servlet is registered there or at a prefix"  << std::endl;
	evhttp_send_error(request.evhttpReq, 404, NULL);
}

void HTTPServer::processByMatchingServlet(evws_connection* conn, const WSFrame& frame) {
	std::lock_guard<std::recursive_mutex> lock(_mutex);

	ws_servlet_iter_t servletIter = _wsServlets.begin();

	std::string actualPath = frame.data.compound.at("path").atom;
	std::map<std::string, WebSocketServlet*, comp_strsize_less> matches;

	while(servletIter != _wsServlets.end()) {
		// is the servlet path a prefix of the actual path?
		std::string servletPath = "/" + servletIter->first;
		if (iequals(actualPath.substr(0, servletPath.length()), servletPath) && // servlet path is a prefix
		        iequals(actualPath.substr(servletPath.length(), 1), "/")) {     // and next character is a '/'
			matches.insert(std::make_pair(servletPath, servletIter->second));
		}
		servletIter++;
	}

	// process by best matching servlet until someone feels responsible
	std::map<std::string, WebSocketServlet*, comp_strsize_less>::iterator matchesIter = matches.begin();
	while(matchesIter != matches.end()) {
		if (matchesIter->second->requestFromWS(conn, frame)) {
			return;
		}
		matchesIter++;
	}
}

void HTTPServer::reply(const Reply& reply) {
	// we need to reply from the thread calling event_base_dispatch, just add to its base queue!
	Reply* replyCB = new Reply(reply);
	HTTPServer* INSTANCE = getInstance();
	event_base_once(INSTANCE->_base, -1, EV_TIMEOUT, HTTPServer::replyCallback, replyCB, NULL);
}

void HTTPServer::replyCallback(evutil_socket_t fd, short what, void *arg) {
	Reply* reply = (Reply*)arg;

	if (reply->content.size() > 0 && reply->headers.find("Content-Type") == reply->headers.end()) {
		LOGD(USCXML_INFO) << "Sending content without Content-Type header" << std::endl;
	}

	std::map<std::string, std::string>::const_iterator headerIter = reply->headers.begin();
	while(headerIter != reply->headers.end()) {
		evhttp_add_header(evhttp_request_get_output_headers(reply->evhttpReq), headerIter->first.c_str(), headerIter->second.c_str());
		headerIter++;
	}

	if (reply->status >= 400) {
		evhttp_send_error(reply->evhttpReq, reply->status, NULL);
		return;
	}

	struct evbuffer *evb = NULL;

	if (!iequals(reply->type, "HEAD") && reply->content.size() > 0) {
		evb = evbuffer_new();
		evbuffer_add(evb, reply->content.data(), reply->content.size());
	}

	evhttp_send_reply(reply->evhttpReq, reply->status, NULL, evb);

	if (evb != NULL)
		evbuffer_free(evb);
//  evhttp_request_free(reply->curlReq);
	delete(reply);
}


void HTTPServer::wsSend(struct evws_connection *conn, enum evws_opcode opcode, const char *data, uint64_t length) {
	HTTPServer* INSTANCE = getInstance();
	WSData* sendCB = new WSData(conn, NULL, opcode, data, length);
	event_base_once(INSTANCE->_base, -1, EV_TIMEOUT, HTTPServer::wsSendCallback, sendCB, NULL);
}

void HTTPServer::wsBroadcast(const char *uri, enum evws_opcode opcode, const char *data, uint64_t length) {
	HTTPServer* INSTANCE = getInstance();
	WSData* sendCB = new WSData(NULL, uri, opcode, data, length);
	event_base_once(INSTANCE->_base, -1, EV_TIMEOUT, HTTPServer::wsSendCallback, sendCB, NULL);

}

void HTTPServer::wsSendCallback(evutil_socket_t fd, short what, void *arg) {
	WSData* wsSend = (WSData*)arg;
	if (wsSend->uri.size() > 0) {
		evws_broadcast(getInstance()->_evws, wsSend->uri.c_str(), wsSend->opcode, wsSend->data.data(), wsSend->data.length());
	} else {
		if (evws_is_valid_connection(getInstance()->_evws, wsSend->conn) > 0) {
			evws_send_data(wsSend->conn, wsSend->opcode, wsSend->data.data(), wsSend->data.length());
		}
	}

	delete wsSend;
}

bool HTTPServer::registerServlet(const std::string& path, HTTPServlet* servlet) {
	HTTPServer* INSTANCE = getInstance();

	if (!INSTANCE->_httpHandle) {
		LOGD(USCXML_INFO) << "Registering at unstarted HTTP Server" << std::endl;
		return true; // this is the culprit!
	}

	std::lock_guard<std::recursive_mutex> lock(INSTANCE->_mutex);

	// remove trailing and leading slash
	std::string actualPath = path;
	if (boost::ends_with(actualPath, "/"))
		actualPath = actualPath.substr(0, actualPath.size() - 1);
	if (boost::starts_with(actualPath, "/"))
		actualPath = actualPath.substr(1);
	std::string suffixedPath = actualPath;

	// if this servlet allows to adapt the path, do so
	int i = 2;
	while(INSTANCE->_httpServlets.find(suffixedPath) != INSTANCE->_httpServlets.end()) {
		if (!servlet->canAdaptPath())
			return false;
		std::stringstream ss;
		ss << actualPath << i++;
		suffixedPath = ss.str();
	}

	std::stringstream servletURL;
	servletURL << "http://" << INSTANCE->_address << ":" << INSTANCE->_port << "/" << suffixedPath;
	servlet->setURL(servletURL.str());

	INSTANCE->_httpServlets[suffixedPath] = servlet;
//	LOG(USCXML_INFO) << "HTTP Servlet listening at: " << servletURL.str();

	// register callback
	evhttp_set_cb(INSTANCE->_http, ("/" + suffixedPath).c_str(), HTTPServer::httpRecvReqCallback, servlet);

	return true;
}

void HTTPServer::unregisterServlet(HTTPServlet* servlet) {
	HTTPServer* INSTANCE = getInstance();
	std::lock_guard<std::recursive_mutex> lock(INSTANCE->_mutex);
	http_servlet_iter_t servletIter = INSTANCE->_httpServlets.begin();
	while(servletIter != INSTANCE->_httpServlets.end()) {
		if (servletIter->second == servlet) {
			evhttp_del_cb(INSTANCE->_http, std::string("/" + servletIter->first).c_str());
			INSTANCE->_httpServlets.erase(servletIter);
			break;
		}
		servletIter++;
	}
}

bool HTTPServer::registerServlet(const std::string& path, WebSocketServlet* servlet) {
	HTTPServer* INSTANCE = getInstance();

	if (!INSTANCE->_wsHandle)
		return true;

	std::lock_guard<std::recursive_mutex> lock(INSTANCE->_mutex);

	// remove trailing and leading slash
	std::string actualPath = path;
	if (boost::ends_with(actualPath, "/"))
		actualPath = actualPath.substr(0, actualPath.size() - 1);
	if (boost::starts_with(actualPath, "/"))
		actualPath = actualPath.substr(1);
	std::string suffixedPath = actualPath;

	// if this servlet allows to adapt the path, do so
	int i = 2;
	while(INSTANCE->_wsServlets.find(suffixedPath) != INSTANCE->_wsServlets.end()) {
		if (!servlet->canAdaptPath())
			return false;
		std::stringstream ss;
		ss << actualPath << i++;
		suffixedPath = ss.str();
	}

	std::stringstream servletURL;
	servletURL << "ws://" << INSTANCE->_address << ":" << INSTANCE->_wsPort << "/" << suffixedPath;
	servlet->setURL(servletURL.str());

	INSTANCE->_wsServlets[suffixedPath] = servlet;

	//	LOG(USCXML_INFO) << "HTTP Servlet listening at: " << servletURL.str() << std::endl;

	// register callback
	evws_set_cb(INSTANCE->_evws, ("/" + suffixedPath).c_str(), HTTPServer::wsRecvReqCallback, NULL, servlet);

	return true;
}

void HTTPServer::unregisterServlet(WebSocketServlet* servlet) {
	HTTPServer* INSTANCE = getInstance();
	std::lock_guard<std::recursive_mutex> lock(INSTANCE->_mutex);
	ws_servlet_iter_t servletIter = INSTANCE->_wsServlets.begin();
	while(servletIter != INSTANCE->_wsServlets.end()) {
		if (servletIter->second == servlet) {
			evhttp_del_cb(INSTANCE->_http, std::string("/" + servletIter->first).c_str());
			INSTANCE->_wsServlets.erase(servletIter);
			break;
		}
		servletIter++;
	}
}

std::string HTTPServer::getBaseURL(ServerType type) {
	HTTPServer* INSTANCE = getInstance();
	std::stringstream servletURL;

	switch (type) {
	case HTTP:
		servletURL << "http://" << INSTANCE->_address << ":" << INSTANCE->_port;
		break;
#ifdef HTTPS_ENABLED
	case HTTPS:
		servletURL << "https://" << INSTANCE->_address << ":" << INSTANCE->_sslPort;
		break;
#endif
	case WebSockets:
		servletURL << "ws://" << INSTANCE->_address << ":" << INSTANCE->_wsPort;
		break;
	default:
		break;
	}
	return servletURL.str();
}

void HTTPServer::start() {
	_isRunning = true;
	_thread = new std::thread(HTTPServer::run, this);
}

void HTTPServer::run(void* instance) {
	HTTPServer* INSTANCE = (HTTPServer*)instance;
	while(INSTANCE->_isRunning) {
		// getting this to be non-polling is somewhat tricky and changes among versions
//		event_base_loop(INSTANCE->_base, EVLOOP_ONCE | EVLOOP_NO_EXIT_ON_EMPTY);
//		event_base_dispatch(INSTANCE->_base);

		// BasicDelayedEventQueue::run
		event_base_loop(INSTANCE->_base, EVLOOP_ONCE);

	}
	LOGD(USCXML_INFO) << "HTTP Server stopped" << std::endl;
}

void HTTPServer::determineAddress() {
	char hostname[1024];
	gethostname(hostname, 1024);
	_address = std::string(hostname);
}


#ifdef HTTPS_ENABLED
// see https://github.com/ppelleti/https-example/blob/master/https-server.c
struct bufferevent* HTTPServer::sslBufferEventCallback(struct event_base *base, void *arg) {
	struct bufferevent* r;
	SSL_CTX *ctx = (SSL_CTX *) arg;
	r = bufferevent_openssl_socket_new (base,
	                                    -1,
	                                    SSL_new (ctx),
	                                    BUFFEREVENT_SSL_ACCEPTING,
	                                    BEV_OPT_CLOSE_ON_FREE);
	return r;
}
#endif

}