diff options
Diffstat (limited to 'src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp')
-rw-r--r-- | src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp | 142 |
1 files changed, 63 insertions, 79 deletions
diff --git a/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp b/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp index 1bced48..f81cf54 100644 --- a/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp +++ b/src/uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.cpp @@ -20,9 +20,8 @@ #include "uscxml/Common.h" #include "uscxml/plugins/ioprocessor/basichttp/BasicHTTPIOProcessor.h" -#include "uscxml/Message.h" -#include "uscxml/dom/DOMUtils.h" -#include "uscxml/dom/NameSpacingParser.h" +#include "uscxml/messages/Event.h" +#include "uscxml/util/DOM.h" #include <iostream> #include <event2/dns.h> @@ -31,8 +30,7 @@ #include <string.h> -#include <io/uri.hpp> -#include <glog/logging.h> +#include <easylogging++.h> #include <boost/algorithm/string.hpp> #ifdef _WIN32 @@ -65,6 +63,7 @@ bool pluginConnect(pluma::Host& host) { // see http://www.w3.org/TR/scxml/#BasicHTTPEventProcessor BasicHTTPIOProcessor::BasicHTTPIOProcessor() { + HTTPServer::getInstance(); } BasicHTTPIOProcessor::~BasicHTTPIOProcessor() { @@ -73,8 +72,8 @@ BasicHTTPIOProcessor::~BasicHTTPIOProcessor() { } -boost::shared_ptr<IOProcessorImpl> BasicHTTPIOProcessor::create(InterpreterImpl* interpreter) { - boost::shared_ptr<BasicHTTPIOProcessor> io = boost::shared_ptr<BasicHTTPIOProcessor>(new BasicHTTPIOProcessor()); +std::shared_ptr<IOProcessorImpl> BasicHTTPIOProcessor::create(InterpreterImpl* interpreter) { + std::shared_ptr<BasicHTTPIOProcessor> io(new BasicHTTPIOProcessor()); io->_interpreter = interpreter; // register at http server @@ -104,19 +103,19 @@ Data BasicHTTPIOProcessor::getDataModelVariables() { data.compound["path"] = Data(url.path(), Data::VERBATIM); data.compound["scheme"] = Data(url.scheme(), Data::VERBATIM); - std::vector<std::string> pathComps = url.pathComponents(); - std::vector<std::string>::const_iterator pathCompIter = pathComps.begin(); + std::list<std::string> pathComps = url.pathComponents(); + std::list<std::string>::const_iterator pathCompIter = pathComps.begin(); while(pathCompIter != pathComps.end()) { - data.compound["pathComponens"].array.push_back(Data(*pathCompIter, Data::VERBATIM)); + data.compound["pathComponents"].array.push_back(Data(*pathCompIter, Data::VERBATIM)); pathCompIter++; } return data; } -bool BasicHTTPIOProcessor::httpRecvRequest(const HTTPServer::Request& req) { - Event reqEvent = req; - reqEvent.eventType = Event::EXTERNAL; +bool BasicHTTPIOProcessor::requestFromHTTP(const HTTPServer::Request& req) { + Event event = req; + event.eventType = Event::EXTERNAL; // std::cout << req.raw << std::endl; @@ -130,10 +129,10 @@ bool BasicHTTPIOProcessor::httpRecvRequest(const HTTPServer::Request& req) { // if we sent ourself an event it will end up here // this will call the const subscript operator if (req.data.at("content").hasKey("_scxmleventname")) { - reqEvent.name = req.data.at("content").at("_scxmleventname").atom; + event.name = req.data.at("content").at("_scxmleventname").atom; } if (req.data.at("content").hasKey("content")) { - reqEvent.content = req.data.at("content").at("content").atom; + event.data.atom = req.data.at("content").at("content").atom; } } @@ -143,9 +142,9 @@ bool BasicHTTPIOProcessor::httpRecvRequest(const HTTPServer::Request& req) { for(std::map<std::string, Data>::const_iterator compIter = data.compound.begin(); compIter!= data.compound.end(); compIter++) { if (compIter->first == "content") { - reqEvent.content = compIter->second.atom; + event.data.atom = compIter->second.atom; } else { - reqEvent.data[compIter->first] = compIter->second; + event.data[compIter->first] = compIter->second; } } } @@ -155,62 +154,60 @@ bool BasicHTTPIOProcessor::httpRecvRequest(const HTTPServer::Request& req) { for(std::map<std::string, Data>::const_iterator compIter = data.compound.begin(); compIter!= data.compound.end(); compIter++) { if (compIter->first == "_scxmleventname") { - reqEvent.name = compIter->second.atom; + event.name = compIter->second.atom; } } } - // check whether we can parse it as XML - if (reqEvent.content.length() > 0) { - NameSpacingParser parser = NameSpacingParser::fromXML(reqEvent.content); - if (!parser.errorsReported()) { - reqEvent.dom = parser.getDocument(); - } - } - - /// test532 - if (reqEvent.name.length() == 0) - reqEvent.name = "http." + req.data.compound.at("type").atom; + // test 532 + if (event.name.length() == 0) + event.name = "http." + req.data.compound.at("type").atom; - returnEvent(reqEvent); + eventToSCXML(event, USCXML_IOPROC_BASICHTTP_TYPE, _url); evhttp_send_reply(req.evhttpReq, 200, "OK", NULL); return true; } -void BasicHTTPIOProcessor::send(const SendRequest& req) { +bool BasicHTTPIOProcessor::isValidTarget(const std::string& target) { + try { + URL url(target); + if (url.scheme().compare("http") != 0) + return false; - if (req.target.length() == 0) { - _interpreter->receiveInternal(Event("error.communication", Event::PLATFORM)); - return; + return true; + } catch (ErrorEvent e) { } + return false; +} - bool isLocal = false; - std::string target; - if (!boost::equals(req.target, _url)) { - target = req.target; - } else { - isLocal = true; - target = _url; +void BasicHTTPIOProcessor::eventFromSCXML(const std::string& target, const Event& event) { + + // TODO: is this still needed with isValidTarget()? + if (target.length() == 0) { + _interpreter->enqueueInternal(Event("error.communication", Event::PLATFORM)); + return; } + + bool isLocal = target == _url; URL targetURL(target); std::stringstream kvps; std::string kvpSeperator; // event name - if (req.name.size() > 0) { + if (event.name.size() > 0) { char* eventNameCStr = evhttp_encode_uri("_scxmleventname"); - char* eventValueCStr = evhttp_encode_uri(req.name.c_str()); + char* eventValueCStr = evhttp_encode_uri(event.name.c_str()); kvps << kvpSeperator << eventNameCStr << "=" << eventValueCStr; kvpSeperator = "&"; free(eventNameCStr); free(eventValueCStr); -// targetURL.addOutHeader("_scxmleventname", evhttp_encode_uri(req.name.c_str())); + targetURL.addOutHeader("_scxmleventname", evhttp_encode_uri(event.name.c_str())); } // event namelist - if (req.namelist.size() > 0) { - std::map<std::string, Data>::const_iterator namelistIter = req.namelist.begin(); - while (namelistIter != req.namelist.end()) { + if (event.namelist.size() > 0) { + std::map<std::string, Data>::const_iterator namelistIter = event.namelist.begin(); + while (namelistIter != event.namelist.end()) { char* keyCStr = evhttp_encode_uri(namelistIter->first.c_str()); // this is simplified - Data might be more elaborate than a simple string atom char* valueCStr = evhttp_encode_uri(namelistIter->second.atom.c_str()); @@ -218,15 +215,15 @@ void BasicHTTPIOProcessor::send(const SendRequest& req) { free(keyCStr); free(valueCStr); kvpSeperator = "&"; -// targetURL.addOutHeader(namelistIter->first, namelistIter->second); + targetURL.addOutHeader(namelistIter->first, namelistIter->second); namelistIter++; } } // event params - if (req.params.size() > 0) { - std::multimap<std::string, Data>::const_iterator paramIter = req.params.begin(); - while (paramIter != req.params.end()) { + if (event.params.size() > 0) { + std::multimap<std::string, Data>::const_iterator paramIter = event.params.begin(); + while (paramIter != event.params.end()) { char* keyCStr = evhttp_encode_uri(paramIter->first.c_str()); // this is simplified - Data might be more elaborate than a simple string atom char* valueCStr = evhttp_encode_uri(paramIter->second.atom.c_str()); @@ -234,35 +231,23 @@ void BasicHTTPIOProcessor::send(const SendRequest& req) { free(keyCStr); free(valueCStr); kvpSeperator = "&"; -// targetURL.addOutHeader(paramIter->first, paramIter->second); + targetURL.addOutHeader(paramIter->first, paramIter->second); paramIter++; } } // try hard to find actual content char* keyCStr = evhttp_encode_uri("content"); - if (req.content.size() > 0) { - char* valueCStr = evhttp_encode_uri(req.content.c_str()); - kvps << kvpSeperator << keyCStr << "=" << valueCStr; - free(valueCStr); - kvpSeperator = "&"; - } else if (req.dom) { - std::stringstream xmlStream; - xmlStream << req.dom; - char* valueCStr = evhttp_encode_uri(xmlStream.str().c_str()); - kvps << kvpSeperator << keyCStr << "=" << valueCStr; - free(valueCStr); - kvpSeperator = "&"; - } else if (!req.data.empty()) { + if (!event.data.empty()) { char* valueCStr = NULL; - if (req.data.atom.length() || req.data.array.size() || req.data.compound.size()) { - valueCStr = evhttp_encode_uri(Data::toJSON(req.data).c_str()); - } else if(req.data.node) { + if (event.data.atom.length() || event.data.array.size() || event.data.compound.size()) { + valueCStr = evhttp_encode_uri(Data::toJSON(event.data).c_str()); + } else if(event.data.node) { std::stringstream xmlStream; - xmlStream << req.data.node; + xmlStream << event.data.node; valueCStr = evhttp_encode_uri(xmlStream.str().c_str()); - } else if(req.data.binary) { - valueCStr = evhttp_encode_uri(req.data.binary.base64().c_str()); + } else if(event.data.binary) { + valueCStr = evhttp_encode_uri(event.data.binary.base64().c_str()); } if (valueCStr != NULL) { kvps << kvpSeperator << keyCStr << "=" << valueCStr; @@ -273,13 +258,12 @@ void BasicHTTPIOProcessor::send(const SendRequest& req) { free(keyCStr); targetURL.setOutContent(kvps.str()); + targetURL.addOutHeader("Content-Type", "application/x-www-form-urlencoded"); -// targetURL.addOutHeader("Content-Type", "application/x-www-form-urlencoded"); - - targetURL.setRequestType("post"); + targetURL.setRequestType(URLRequestType::POST); targetURL.addMonitor(this); - _sendRequests[req.sendid] = std::make_pair(targetURL, req); + _sendRequests[event.sendid] = std::make_pair(targetURL, event); if (isLocal) { // test201 use a blocking request with local communication targetURL.download(true); @@ -291,7 +275,7 @@ void BasicHTTPIOProcessor::send(const SendRequest& req) { void BasicHTTPIOProcessor::downloadStarted(const URL& url) {} void BasicHTTPIOProcessor::downloadCompleted(const URL& url) { - std::map<std::string, std::pair<URL, SendRequest> >::iterator reqIter = _sendRequests.begin(); + std::map<std::string, std::pair<URL, Event> >::iterator reqIter = _sendRequests.begin(); while(reqIter != _sendRequests.end()) { if (reqIter->second.first == url) { // test513 @@ -302,7 +286,7 @@ void BasicHTTPIOProcessor::downloadCompleted(const URL& url) { Event event; event.data = url; event.name = "HTTP." + statusPrefix + "." + statusRest; -// returnEvent(event); + eventToSCXML(event, USCXML_IOPROC_BASICHTTP_TYPE, std::string(_url)); } _sendRequests.erase(reqIter); return; @@ -314,12 +298,12 @@ void BasicHTTPIOProcessor::downloadCompleted(const URL& url) { void BasicHTTPIOProcessor::downloadFailed(const URL& url, int errorCode) { - std::map<std::string, std::pair<URL, SendRequest> >::iterator reqIter = _sendRequests.begin(); + std::map<std::string, std::pair<URL, Event> >::iterator reqIter = _sendRequests.begin(); while(reqIter != _sendRequests.end()) { if (reqIter->second.first == url) { Event failEvent; failEvent.name = "error.communication"; - returnEvent(failEvent); + eventToSCXML(failEvent, USCXML_IOPROC_BASICHTTP_TYPE, std::string(_url)); _sendRequests.erase(reqIter); return; |