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
|
/**
* @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 <boost/algorithm/string.hpp>
#include <uscxml/config.h>
#include "XHTMLInvoker.h"
#include <glog/logging.h>
#include "uscxml/DOMUtils.h"
#include <uscxml/plugins/ioprocessor/comet/CometIOProcessor.h>
#include <DOM/io/Stream.hpp>
#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif
#if __APPLE__
# if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
# else
# include <CoreFoundation/CFBundle.h>
# include <ApplicationServices/ApplicationServices.h>
# endif
#endif
namespace uscxml {
#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool pluginConnect(pluma::Host& host) {
host.add( new XHTMLInvokerProvider() );
return true;
}
#endif
XHTMLInvoker::XHTMLInvoker() {
}
XHTMLInvoker::~XHTMLInvoker() {
HTTPServer::unregisterServlet(this);
};
boost::shared_ptr<InvokerImpl> XHTMLInvoker::create(InterpreterImpl* interpreter) {
boost::shared_ptr<XHTMLInvoker> invoker = boost::shared_ptr<XHTMLInvoker>(new XHTMLInvoker());
invoker->_interpreter = interpreter;
// _ioProc = interpreter->getFactory()->createIOProcessor("comet", interpreter);
return invoker;
}
bool XHTMLInvoker::httpRecvRequest(const HTTPServer::Request& req) {
tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
// these are the XHR requests
if (iequals(req.data.at("header").at("X-Requested-With").atom, "XMLHttpRequest")) {
if (iequals(req.data.at("type").atom, "get")) {
// the long-polling GET
if (_longPoll) {
evhttp_send_error(_longPoll.evhttpReq, 204, NULL);
_longPoll.evhttpReq = NULL;
}
_longPoll = req;
if (!_outQueue.empty()) {
send(_outQueue.front());
_outQueue.pop_front();
}
return true;
} else {
// a POST request
Event ev(req);
if (ev.data["header"].hasKey("X-SCXML-Name")) {
ev.name = ev.data["header"]["X-SCXML-Name"].atom;
} else {
ev.name = req.data.at("type").atom;
}
ev.origin = _invokeId;
ev.initContent(req.data.at("content").atom);
ev.data.compound["Connection"] = req.data;
// content is already on ev.raw
ev.data.compound["Connection"].compound.erase("content");
HTTPServer::Reply reply(req);
HTTPServer::reply(reply);
returnEvent(ev);
return true;
}
}
// initial request for a document
if (!req.data.hasKey("query") && // no query parameters
iequals(req.data.at("type").atom, "get") && // request type is GET
req.content.length() == 0) { // no content
HTTPServer::Reply reply(req);
std::string content;
std::stringstream ss;
if (_invokeReq.dom) {
// ss << _invokeReq.getFirstDOMElement();
ss << _invokeReq.dom;
content = ss.str();
} else if(!_invokeReq.data.empty()) {
ss << _invokeReq.data;
content = ss.str();
} else if (_invokeReq.content.length() > 0) {
content = _invokeReq.content;
} else {
URL templateURL("templates/xhtml-invoker.html");
templateURL.toAbsolute(_interpreter->getBaseURI());
templateURL.download(true);
content = templateURL.getInContent();
}
std::cout << content;
_interpreter->getDataModel().replaceExpressions(content);
reply.content = content;
// application/xhtml+xml
reply.headers["Content-Type"] = "text/html; charset=utf-8";
// reply.headers["Content-Type"] = "application/xhtml+xml; charset=utf-8";
// aggressive caching in IE will return all XHR get requests instantenously with the template otherwise
reply.headers["Cache-Control"] = "no-cache";
// reply.headers["Cache-Control"] = "max-age=3600";
HTTPServer::reply(reply);
// queue invoke request for initial html
_longPoll.evhttpReq = NULL;
_outQueue = std::deque<SendRequest>();
SendRequest sendReq(_invokeReq);
send(sendReq);
return true;
}
// don't know what to do with other requests
return false;
}
Data XHTMLInvoker::getDataModelVariables() {
Data data;
return data;
}
void XHTMLInvoker::send(const SendRequest& req) {
tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
SendRequest reqCopy(req);
_interpreter->getDataModel().replaceExpressions(reqCopy.content);
Data json = Data::fromJSON(reqCopy.content);
if (!json.empty()) {
reqCopy.data = json;
}
if (!_longPoll) {
_outQueue.push_back(reqCopy);
return;
}
reply(reqCopy, _longPoll);
_longPoll.evhttpReq = NULL;
}
void XHTMLInvoker::reply(const SendRequest& req, const HTTPServer::Request& longPoll) {
HTTPServer::Reply reply(longPoll);
// is there JSON in the content?
std::string content = req.content;
if (req.dom) {
std::stringstream ss;
// Arabica::DOM::Node<std::string> content = req.dom.getDocumentElement();
Arabica::DOM::Node<std::string> content = req.dom;
if (content && iequals(content.getLocalName(), "content")) {
reply.headers["X-SCXML-Type"] = (HAS_ATTR(content, "type") ? ATTR(content, "type") : "replacechildren");
reply.headers["X-SCXML-XPath"] = (HAS_ATTR(content, "xpath") ? ATTR(content, "xpath") : "/html/body");
if (HAS_ATTR(content, "attr"))
reply.headers["X-SCXML-Attr"] = ATTR(content, "attr");
}
// ss << req.getFirstDOMElement();
ss << req.dom;
reply.content = ss.str();
reply.headers["Content-Type"] = "application/xml";
} else if (!req.data.empty()) {
reply.content = Data::toJSON(req.data);
reply.headers["Content-Type"] = "application/json";
} else if (req.content.length() > 0) {
reply.content = req.content;
reply.headers["Content-Type"] = "text/plain";
}
if (req.params.find("Content-Type") != req.params.end())
reply.headers["Content-Type"] = req.params.find("Content-Type")->first;
HTTPServer::reply(reply);
}
void XHTMLInvoker::cancel(const std::string sendId) {
HTTPServer::unregisterServlet(this);
}
void XHTMLInvoker::invoke(const InvokeRequest& req) {
_invokeReq = req;
HTTPServer::registerServlet(_interpreter->getName() + "/" + req.invokeid + ".html", this);
if (_url.size() == 0) {
returnErrorExecution("No HTTP server running");
}
#if __APPLE__
# if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
# else
// see http://stackoverflow.com/questions/4177744/c-osx-open-default-browser
CFURLRef url = CFURLCreateWithBytes (
NULL, // allocator
(UInt8*)_url.c_str(), // URLBytes
_url.length(), // length
kCFStringEncodingASCII, // encoding
NULL // baseURL
);
LSOpenCFURLRef(url,0);
CFRelease(url);
return;
# endif
#endif
#ifdef _WIN32
// see http://support.microsoft.com/kb/224816
ShellExecute(NULL, "open", _url.c_str(), NULL, NULL, SW_SHOWNORMAL);
return;
#endif
#ifdef HAS_XDG_OPEN
std::string systemCmd;
systemCmd = "xdg-open ";
systemCmd += _url;
system(systemCmd.c_str());
return;
#endif
}
}
|