summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/http/HTTPServletInvoker.cpp
blob: 1a9539673099e6a966303c44d5d4077073e860b1 (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
#include "HTTPServletInvoker.h"
#include <glog/logging.h>

#include "uscxml/config.h"

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

namespace uscxml {

#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool connect(pluma::Host& host) {
	host.add( new HTTPServletInvokerProvider() );
	return true;
}
#endif

HTTPServletInvoker::HTTPServletInvoker() {
}

HTTPServletInvoker::~HTTPServletInvoker() {
	HTTPServer::unregisterServlet(this);

};

boost::shared_ptr<InvokerImpl> HTTPServletInvoker::create(InterpreterImpl* interpreter) {
	boost::shared_ptr<HTTPServletInvoker> invoker = boost::shared_ptr<HTTPServletInvoker>(new HTTPServletInvoker());
	invoker->_interpreter = interpreter;
	return invoker;
}

Data HTTPServletInvoker::getDataModelVariables() {
	Data data;
	assert(_url.length() > 0);
	data.compound["location"] = Data(_url, Data::VERBATIM);
	return data;
}

void HTTPServletInvoker::send(const SendRequest& req) {

	if (req.name.find("reply.", 0, req.name.length())) {
		// this is a reply
		const std::string requestId = req.name.substr(6);
		if (_requests.find(requestId) == _requests.end()) {
			LOG(ERROR) << "Replying to non existing request " << requestId;
			return;
		}

		HTTPServer::Request httpRequest = _requests[requestId];
		HTTPServer::Reply httpReply(httpRequest);
		httpReply.content = req.content;

		std::map<std::string, Data>::const_iterator nameListIter = req.namelist.begin();
		while(nameListIter != req.namelist.end()) {
			httpReply.headers[nameListIter->first] = nameListIter->second.atom;
			nameListIter++;
		}

		std::multimap<std::string, Data>::const_iterator paramIter = req.params.begin();
		while(paramIter != req.params.end()) {
			httpReply.headers[paramIter->first] = paramIter->second.atom;
			paramIter++;
		}

		HTTPServer::reply(httpReply);
		return;
	}
}

void HTTPServletInvoker::cancel(const std::string sendId) {
}

void HTTPServletInvoker::invoke(const InvokeRequest& req) {

	_invokeId = req.invokeid;
	if (req.params.find("path") == req.params.end()) {
		LOG(ERROR) << "Path parameter required with httpserver";
	}
	_path = (*req.params.find("path")).second.atom;

	if (req.params.find("callback") != req.params.end()) {
		_callback = (*req.params.find("callback")).second.atom;
	} else {
		_callback = _path;
		std::replace(_callback.begin(), _callback.end(), '/', '.');
	}

	if (!HTTPServer::registerServlet(_path, this)) {
		LOG(ERROR) << "Cannot register http servlet at " << _path << ": " << " already taken";
	}
}

/**
 * Receive a request and deliver it to the interpreter
 */
bool HTTPServletInvoker::httpRecvRequest(const HTTPServer::Request& req) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

//  evhttp_request_own(req.curlReq);

	_requests[toStr((uintptr_t)req.curlReq)] = req;

	Event event = req;

	event.name = _callback;
	event.data.compound["reqId"] = Data(toStr((uintptr_t)req.curlReq), Data::VERBATIM);

	returnEvent(event);
	return true;
}

std::string HTTPServletInvoker::getPath() {
	return _path;
}

}