summaryrefslogtreecommitdiffstats
path: root/src/uscxml/util
diff options
context:
space:
mode:
authorStefan Radomski <github@mintwerk.de>2016-12-11 13:29:10 (GMT)
committerStefan Radomski <github@mintwerk.de>2016-12-11 13:29:10 (GMT)
commit277ca19814890939d5d0e4551e3acb651b1c42e6 (patch)
tree8233c9a4ff2b4104bf5f5f744a723cb19fab4f29 /src/uscxml/util
parent1c1e72a2af9c23dfd800d3a162142d6fac8dbd44 (diff)
downloaduscxml-277ca19814890939d5d0e4551e3acb651b1c42e6.zip
uscxml-277ca19814890939d5d0e4551e3acb651b1c42e6.tar.gz
uscxml-277ca19814890939d5d0e4551e3acb651b1c42e6.tar.bz2
Reduced foreign header dependencies
Diffstat (limited to 'src/uscxml/util')
-rw-r--r--src/uscxml/util/DOM.h15
-rw-r--r--src/uscxml/util/URL.cpp130
-rw-r--r--src/uscxml/util/URL.h106
3 files changed, 129 insertions, 122 deletions
diff --git a/src/uscxml/util/DOM.h b/src/uscxml/util/DOM.h
index e8094cf..2b133d8 100644
--- a/src/uscxml/util/DOM.h
+++ b/src/uscxml/util/DOM.h
@@ -24,25 +24,10 @@
#include <list>
#include <iostream>
-#include "uscxml/config.h"
#include "uscxml/Common.h"
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOM.hpp>
-
-/*
-#define TAGNAME_CAST(elem) ((Arabica::DOM::Element<std::string>)elem).getTagName()
-#define LOCALNAME_CAST(elem) ((Arabica::DOM::Element<std::string>)elem).getLocalName()
-#define ATTR_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).getAttribute(attr)
-#define ATTR_NODE_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).getAttributeNode(attr)
-#define HAS_ATTR_CAST(elem, attr) ((Arabica::DOM::Element<std::string>)elem).hasAttribute(attr)
-
-#define TAGNAME(elem) elem.getTagName()
-#define LOCALNAME(elem) elem.getLocalName()
-#define ATTR(elem, attr) elem.getAttribute(attr)
-#define ATTR_NODE(elem, attr) elem.getAttributeNode(attr)
-*/
-
#define HAS_ATTR(elem, attr) (elem)->hasAttribute(X(attr))
#define HAS_ATTR_CAST(elem, attr) HAS_ATTR(static_cast<const DOMElement*>(elem), attr)
#define ATTR(elem, attr) std::string(X((elem)->getAttribute(X(attr))))
diff --git a/src/uscxml/util/URL.cpp b/src/uscxml/util/URL.cpp
index 7978793..9329e7e 100644
--- a/src/uscxml/util/URL.cpp
+++ b/src/uscxml/util/URL.cpp
@@ -26,6 +26,9 @@
#include "uscxml/interpreter/Logging.h"
#include "uscxml/config.h"
+#include <curl/curl.h>
+#include <uriparser/Uri.h>
+
#ifdef _WIN32
#include <direct.h>
@@ -36,14 +39,17 @@
//#include <pwd.h>
#endif
+#define DOWNLOAD_IF_NECESSARY if (!_isDownloaded) { download(true); }
+#define USCXML_URI_STRING(obj, field) std::string(obj.field.first, (obj.field.afterLast - obj.field.first))
+
namespace uscxml {
-void URLImpl::prepareException(ErrorEvent& exception, int errorCode, const std::string& origUri, UriParserStateA* parser) {
+ void URLImpl::prepareException(ErrorEvent& exception, int errorCode, const std::string& origUri, void* parser) {
exception.data.compound["uri"].atom = origUri;
- if (parser != NULL && parser->errorPos != NULL) {
+ if (parser != NULL && ((UriParserStateA*)parser)->errorPos != NULL) {
const char* startPtr = origUri.c_str();
- while(startPtr != parser->errorPos && *startPtr != '\0') {
+ while(startPtr != ((UriParserStateA*)parser)->errorPos && *startPtr != '\0') {
exception.data.compound["urk"].atom += " ";
startPtr++;
}
@@ -82,16 +88,18 @@ void URLImpl::prepareException(ErrorEvent& exception, int errorCode, const std::
}
URLImpl::URLImpl() : _handle(NULL), _requestType(GET), _isDownloaded(false), _hasFailed(false) {
+ _uri = malloc(sizeof(UriUriA));
}
URLImpl::URLImpl(const std::string& url) : _orig(url), _handle(NULL), _requestType(GET), _isDownloaded(false), _hasFailed(false) {
UriParserStateA state;
- state.uri = &_uri;
+ _uri = malloc(sizeof(UriUriA));
+ state.uri = (UriUriA*)_uri;
int err = uriParseUriA(&state, _orig.c_str());
if (err != URI_SUCCESS) {
UriParserStateA state2;
- state2.uri = &_uri;
+ state2.uri = (UriUriA*)_uri;
char* tmp = (char*)malloc(8 + 3 * _orig.size() + 1);
uriWindowsFilenameToUriStringA(_orig.c_str(), tmp);
@@ -102,7 +110,7 @@ URLImpl::URLImpl(const std::string& url) : _orig(url), _handle(NULL), _requestTy
if (err != URI_SUCCESS) {
UriParserStateA state2;
- state2.uri = &_uri;
+ state2.uri = (UriUriA*)_uri;
char* tmp = (char*)malloc(7 + 3 * _orig.size() + 1 );
uriUnixFilenameToUriStringA(_orig.c_str(), tmp);
@@ -119,14 +127,17 @@ URLImpl::URLImpl(const std::string& url) : _orig(url), _handle(NULL), _requestTy
}
URLImpl::~URLImpl() {
- uriFreeUriMembersA(&_uri);
+ uriFreeUriMembersA((UriUriA*)_uri);
if (_handle != NULL)
curl_easy_cleanup(_handle);
+ free(_uri);
}
URL URLImpl::resolve(URLImpl* relative, URLImpl* absolute) {
std::shared_ptr<URLImpl> dest(new URLImpl());
- int err = uriAddBaseUriExA(&(dest->_uri), &(relative->_uri), &(absolute->_uri), URI_RESOLVE_IDENTICAL_SCHEME_COMPAT);
+ int err = uriAddBaseUriExA(((UriUriA*)dest->_uri),
+ ((UriUriA*)relative->_uri),
+ ((UriUriA*)absolute->_uri), URI_RESOLVE_IDENTICAL_SCHEME_COMPAT);
if (err != URI_SUCCESS) {
ErrorEvent exc("Cannot resolve " + (std::string)(*relative) + " with " + (std::string)(*absolute));
prepareException(exc, err, "", NULL);
@@ -156,7 +167,9 @@ URL URLImpl::resolveWithCWD(URLImpl* relative) {
URL URLImpl::refer(URLImpl* absoluteSource, URLImpl* absoluteBase) {
std::shared_ptr<URLImpl> dest(new URLImpl());
- int err = uriRemoveBaseUriA(&(dest->_uri), &(absoluteSource->_uri), &(absoluteBase->_uri), URI_FALSE);
+ int err = uriRemoveBaseUriA(((UriUriA*)dest->_uri),
+ ((UriUriA*)absoluteSource->_uri),
+ ((UriUriA*)absoluteBase->_uri), URI_FALSE);
if (err != URI_SUCCESS) {
ErrorEvent exc("Cannot make a relative reference for " + (std::string)(*absoluteSource) + " with " + (std::string)(*absoluteBase));
prepareException(exc, err, "", NULL);
@@ -168,7 +181,7 @@ URL URLImpl::refer(URLImpl* absoluteSource, URLImpl* absoluteBase) {
}
void URLImpl::normalize() {
- int err = uriNormalizeSyntaxA(&_uri);
+ int err = uriNormalizeSyntaxA((UriUriA*)_uri);
if (err != URI_SUCCESS) {
ErrorEvent exc("Cannot normalize URL " + (std::string)*this);
prepareException(exc, err, _orig, NULL);
@@ -176,8 +189,33 @@ void URLImpl::normalize() {
}
}
+bool URLImpl::isAbsolute() const {
+ // see https://sourceforge.net/p/uriparser/bugs/3/
+ return ((UriUriA*)_uri)->absolutePath || ((((UriUriA*)_uri)->hostText.first != nullptr) && (((UriUriA*)_uri)->pathHead != nullptr));
+}
+
+std::string URLImpl::scheme() const {
+ return USCXML_URI_STRING((*(UriUriA*)_uri), scheme);
+}
+
+std::string URLImpl::userInfo() const {
+ return USCXML_URI_STRING((*(UriUriA*)_uri), userInfo);
+}
+
+std::string URLImpl::host() const {
+ return USCXML_URI_STRING((*(UriUriA*)_uri), hostText);
+}
+
+std::string URLImpl::port() const {
+ return USCXML_URI_STRING((*(UriUriA*)_uri), portText);
+}
+
+std::string URLImpl::fragment() const {
+ return USCXML_URI_STRING((*(UriUriA*)_uri), fragment);
+}
+
std::string URLImpl::path() const {
- UriPathSegmentA* firstSeg = _uri.pathHead;
+ UriPathSegmentA* firstSeg = ((UriUriA*)_uri)->pathHead;
UriPathSegmentA* lastSeg = firstSeg;
while(lastSeg->next) {
lastSeg = lastSeg->next;
@@ -186,12 +224,12 @@ std::string URLImpl::path() const {
std::string path;
// what a mess!
- if (_uri.absolutePath ||
- (_uri.pathHead != NULL &&
- (_uri.hostText.first != NULL ||
- _uri.hostData.ip4 != NULL ||
- _uri.hostData.ip6 != NULL ||
- _uri.hostData.ipFuture.first != NULL))) {
+ if (((UriUriA*)_uri)->absolutePath ||
+ (((UriUriA*)_uri)->pathHead != NULL &&
+ (((UriUriA*)_uri)->hostText.first != NULL ||
+ ((UriUriA*)_uri)->hostData.ip4 != NULL ||
+ ((UriUriA*)_uri)->hostData.ip6 != NULL ||
+ ((UriUriA*)_uri)->hostData.ipFuture.first != NULL))) {
path += "/";
}
path += std::string(firstSeg->text.first, lastSeg->text.afterLast - firstSeg->text.first);
@@ -201,7 +239,7 @@ std::string URLImpl::path() const {
std::list<std::string> URLImpl::pathComponents() const {
std::list<std::string> pathList;
- UriPathSegmentA* currSeg = _uri.pathHead;
+ UriPathSegmentA* currSeg = ((UriUriA*)_uri)->pathHead;
while(currSeg != NULL) {
pathList.push_back(USCXML_URI_STRING((*currSeg), text));
currSeg = currSeg->next;
@@ -216,7 +254,7 @@ std::map<std::string, std::string> URLImpl::query() const {
std::map<std::string, std::string> queryMap;
int itemCount;
- int err = uriDissectQueryMallocA(&queryList, &itemCount, _uri.query.first, _uri.query.afterLast);
+ int err = uriDissectQueryMallocA(&queryList, &itemCount, (*(UriUriA*)_uri).query.first, (*(UriUriA*)_uri).query.afterLast);
if (err != URI_SUCCESS) {
ErrorEvent exc("Cannot get query from URL " + (std::string)*this);
prepareException(exc, err, _orig, NULL);
@@ -327,10 +365,10 @@ void URLImpl::downloadCompleted() {
}
}
-void URLImpl::downloadFailed(CURLcode errorCode) {
+void URLImpl::downloadFailed(int errorCode) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
- _error = curl_easy_strerror(errorCode);
+ _error = curl_easy_strerror((CURLcode)errorCode);
_hasFailed = true;
_isDownloaded = false;
_condVar.notify_all();
@@ -342,6 +380,48 @@ void URLImpl::downloadFailed(CURLcode errorCode) {
}
}
+
+void URLImpl::addOutHeader(const std::string& key, const std::string& value) {
+ _outHeader[key] = value;
+}
+void URLImpl::setOutContent(const std::string& content) {
+ _outContent = content;
+ _requestType = URLRequestType::POST;
+}
+void URLImpl::setRequestType(URLRequestType requestType) {
+ _requestType = requestType;
+
+}
+
+const std::map<std::string, std::string> URLImpl::getInHeaderFields() {
+ DOWNLOAD_IF_NECESSARY
+ return _inHeaders;
+}
+
+const std::string URLImpl::getInHeaderField(const std::string& key) {
+ DOWNLOAD_IF_NECESSARY
+ if (_inHeaders.find(key) != _inHeaders.end()) {
+ return _inHeaders[key];
+ }
+ return "";
+}
+
+const std::string URLImpl::getStatusCode() const {
+ // DOWNLOAD_IF_NECESSARY
+ return _statusCode;
+}
+
+const std::string URLImpl::getStatusMessage() const {
+ // DOWNLOAD_IF_NECESSARY
+ return _statusMsg;
+}
+
+const std::string URLImpl::getInContent(bool forceReload) {
+ if (forceReload)
+ _isDownloaded = false;
+ DOWNLOAD_IF_NECESSARY
+ return _rawInContent.str();
+}
const void URLImpl::download(bool blocking) {
std::lock_guard<std::recursive_mutex> lock(_mutex);
@@ -399,7 +479,7 @@ URLImpl::operator Data() const {
URLImpl::operator std::string() const {
int charsRequired = 0;
- if (uriToStringCharsRequiredA(&_uri, &charsRequired) != URI_SUCCESS) {
+ if (uriToStringCharsRequiredA((UriUriA*)_uri, &charsRequired) != URI_SUCCESS) {
throw ErrorEvent("Cannot recompose URL");
}
charsRequired++;
@@ -410,7 +490,7 @@ URLImpl::operator std::string() const {
throw ErrorEvent("Malloc failed");
}
- if (uriToStringA(uriString, &_uri, charsRequired, NULL) != URI_SUCCESS) {
+ if (uriToStringA(uriString, (UriUriA*)_uri, charsRequired, NULL) != URI_SUCCESS) {
free(uriString);
throw ErrorEvent("Cannot recompose URL");
}
@@ -635,7 +715,7 @@ void URLFetcher::breakURL(URL& url) {
instance->_handlesToURLs.erase(handle);
}
if (instance->_handlesToHeaders.find(handle) != instance->_handlesToHeaders.end()) {
- curl_slist_free_all(instance->_handlesToHeaders[handle]);
+ curl_slist_free_all((struct curl_slist *)instance->_handlesToHeaders[handle]);
instance->_handlesToHeaders.erase(handle);
}
}
@@ -765,7 +845,7 @@ void URLFetcher::perform() {
}
_handlesToURLs.erase(msg->easy_handle);
- curl_slist_free_all(_handlesToHeaders[msg->easy_handle]);
+ curl_slist_free_all((struct curl_slist *)_handlesToHeaders[msg->easy_handle]);
_handlesToHeaders.erase(msg->easy_handle);
} else {
diff --git a/src/uscxml/util/URL.h b/src/uscxml/util/URL.h
index f6da30d..a603c63 100644
--- a/src/uscxml/util/URL.h
+++ b/src/uscxml/util/URL.h
@@ -23,8 +23,6 @@
#include "uscxml/Common.h"
#include "uscxml/messages/Event.h"
-#define DOWNLOAD_IF_NECESSARY if (!_isDownloaded) { download(true); }
-
#include <string>
#include <sstream>
#include <map>
@@ -32,10 +30,6 @@
#include <list>
#include <thread>
#include <condition_variable>
-#include <curl/curl.h>
-#include <uriparser/Uri.h>
-
-#define USCXML_URI_STRING(obj, field) std::string(obj.field.first, obj.field.afterLast - obj.field.first)
namespace uscxml {
@@ -60,31 +54,12 @@ public:
URLImpl(const std::string& url);
~URLImpl();
- bool isAbsolute() const {
- // see https://sourceforge.net/p/uriparser/bugs/3/
- return _uri.absolutePath || ((_uri.hostText.first != nullptr) && (_uri.pathHead != nullptr));
- }
-
- std::string scheme() const {
- return USCXML_URI_STRING(_uri, scheme);
- }
-
- std::string userInfo() const {
- return USCXML_URI_STRING(_uri, userInfo);
- }
-
- std::string host() const {
- return USCXML_URI_STRING(_uri, hostText);
- }
-
- std::string port() const {
- return USCXML_URI_STRING(_uri, portText);
- }
-
- std::string fragment() const {
- return USCXML_URI_STRING(_uri, fragment);
- }
-
+ bool isAbsolute() const;
+ std::string scheme() const;
+ std::string userInfo() const;
+ std::string host() const;
+ std::string port() const;
+ std::string fragment() const;
std::map<std::string, std::string> query() const;
std::string path() const;
std::list<std::string> pathComponents() const;
@@ -103,48 +78,15 @@ public:
}
// downloading / uploading
- void addOutHeader(const std::string& key, const std::string& value) {
- _outHeader[key] = value;
- }
- void setOutContent(const std::string& content) {
- _outContent = content;
- _requestType = URLRequestType::POST;
- }
- void setRequestType(URLRequestType requestType) {
- _requestType = requestType;
-
- }
-
- const std::map<std::string, std::string> getInHeaderFields() {
- DOWNLOAD_IF_NECESSARY
- return _inHeaders;
- }
-
- const std::string getInHeaderField(const std::string& key) {
- DOWNLOAD_IF_NECESSARY
- if (_inHeaders.find(key) != _inHeaders.end()) {
- return _inHeaders[key];
- }
- return "";
- }
-
- const std::string getStatusCode() const {
-// DOWNLOAD_IF_NECESSARY
- return _statusCode;
- }
-
- const std::string getStatusMessage() const {
-// DOWNLOAD_IF_NECESSARY
- return _statusMsg;
- }
-
- const std::string getInContent(bool forceReload = false) {
- if (forceReload)
- _isDownloaded = false;
- DOWNLOAD_IF_NECESSARY
- return _rawInContent.str();
- }
-
+ void addOutHeader(const std::string& key, const std::string& value);
+ void setOutContent(const std::string& content);
+ void setRequestType(URLRequestType requestType);
+ const std::map<std::string, std::string> getInHeaderFields();
+ const std::string getInHeaderField(const std::string& key);
+
+ const std::string getStatusCode() const;
+ const std::string getStatusMessage() const;
+ const std::string getInContent(bool forceReload = false);
const void download(bool blocking = false);
operator Data() const;
@@ -152,20 +94,20 @@ public:
protected:
URLImpl();
- UriUriA _uri;
+ void* _uri = NULL;
std::string _orig;
- CURL* getCurlHandle();
+ void* getCurlHandle();
static size_t writeHandler(void *ptr, size_t size, size_t nmemb, void *userdata);
static size_t headerHandler(void *ptr, size_t size, size_t nmemb, void *userdata);
void downloadStarted();
void downloadCompleted();
- void downloadFailed(CURLcode errorCode);
+ void downloadFailed(int errorCode);
- static void prepareException(ErrorEvent& exception, int errorCode, const std::string& origUri, UriParserStateA* parser);
+ static void prepareException(ErrorEvent& exception, int errorCode, const std::string& origUri, void* parser);
- CURL* _handle = NULL;
+ void* _handle = NULL;
std::stringstream _rawInContent;
std::stringstream _rawInHeader;
std::map<std::string, std::string> _inHeaders;
@@ -324,10 +266,10 @@ protected:
std::recursive_mutex _mutex;
bool _isStarted;
- std::map<CURL*, URL> _handlesToURLs;
- std::map<CURL*, curl_slist*> _handlesToHeaders;
- CURLM* _multiHandle;
- char* _envProxy;
+ std::map<void*, URL> _handlesToURLs;
+ std::map<void*, void*> _handlesToHeaders;
+ void* _multiHandle = NULL;
+ char* _envProxy = NULL;
};
}