From 96142b471661105c39042cf4294c2ebb0eec5fed Mon Sep 17 00:00:00 2001 From: simon Date: Fri, 24 Aug 2018 18:02:20 +0900 Subject: [PATCH 1/4] add convert functions --- src/controllerclientimpl.cpp | 100 +++++++++++++++++++++++++++++++++++ src/controllerclientimpl.h | 8 ++- 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/src/controllerclientimpl.cpp b/src/controllerclientimpl.cpp index 3cb87a6b..f116beb5 100644 --- a/src/controllerclientimpl.cpp +++ b/src/controllerclientimpl.cpp @@ -1363,6 +1363,106 @@ void ControllerClientImpl::_DownloadFileFromController(const std::string& destur } } + +std::string ControllerClientImpl::Quote(const std::string& value) const { + if(_curl){ + char* output = curl_easy_escape(_curl, value.c_str(), value.length()); + if(output){ + return std::string(output); + } + } + return ""; +} + +std::string ControllerClientImpl::Unquote(const std::string& value) const{ + if(_curl){ + int* outlength; + char* result = NULL; + result = curl_easy_unescape(_curl, value.c_str(), value.length(), outlength); + if(result){ + return string(result); + } + } + return ""; +} + +bool ControllerClientImpl::ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const{ + static pcrecpp::RE re("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"); + std::string s1, s3, s6, s8; + if (re.FullMatch(uri, &s1, &scheme, &s3, &authority, &path, &s6, &query, &s8, &fragment)) + { + if(scheme == "mujin"){ + // need to do special check since may container fragment in path; + if(!boost::ends_with(path, "mujin.dae")){ + // try only to get last fragment part; + size_t fragmentIndex = fragment.rfind('#'); + path += "#" + fragment.substr(0, fragmentIndex); + fragment = fragment.substr(fragmentIndex+1); + } + } + path = Unquote(path); + return true; + } + return false; +} + +std::string ControllerClientImpl::AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment){ + std::string uri = ""; + + uri = scheme + ":"; + if(!authority.empty()){ + uri = uri + "//" + authority; + } + if(scheme == "mujin"){ + if(!path.empty()){ + if(path[0] != '/'){ + uri = uri + '/' + path; + } + else{ + uri = uri + path; + } + } + } + else{ + if(!path.empty()){ + if(path[0] != '/'){ + uri += '/' + Quote(path); + } + else{ + uri += Quote(path); + } + } + } + + if(!query.empty()){ + uri += "?" + query; + } + if(!fragment.empty()){ + uri += '#' + fragment; + } + return uri; +} + +std::string ControllerClientImpl::GetPrimaryKeyFromURI(const std::string& uri) const { + if(_curl){ + if(boost::starts_with(uri, "mujin:/")){ + // mujin uri is represented in unicode + std::string pk = uri.substr(); + + } + else if(boost::starts_with(uri, "file:/")){ + // file scheme container the full file path and it's + } + } + return ""; +} + +std::string ControllerClientImpl::GetUnicodeFromPrimaryKey(const std::string& pk) const{ + return Unquote(pk); +} + + + void ControllerClientImpl::DeleteFileOnController_UTF8(const std::string& desturi) { boost::mutex::scoped_lock lock(_mutex); diff --git a/src/controllerclientimpl.h b/src/controllerclientimpl.h index 4fef3aee..bb72abc2 100644 --- a/src/controllerclientimpl.h +++ b/src/controllerclientimpl.h @@ -20,6 +20,7 @@ #include #include +#include namespace mujinclient { @@ -151,7 +152,12 @@ class ControllerClientImpl : public ControllerClient, public boost::enable_share { return _baseuri; } - + std::string Quote(const std::string& value) const; + std::string Unquote(const std::string& value) const; + bool ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const; + std::string AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment); + std::string GetPrimaryKeyFromURI(const std::string& uri) const; + std::string GetUnicodeFromPrimaryKey(const std::string& pk) const; protected: int _CallPut(const std::string& relativeuri, const void* pdata, size_t nDataSize, rapidjson::Document& pt, curl_slist* headers, int expectedhttpcode=202, double timeout = 5.0); From 72b384c8d5f25d5675b54c94be8e3a833cf2a024 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 26 Aug 2018 20:15:00 +0900 Subject: [PATCH 2/4] add some url related functions --- CMakeLists.txt | 12 ++++++++++++ .../mujincontrollerclient/mujincontrollerclient.h | 8 ++++++++ src/controllerclientimpl.cpp | 4 ++-- src/controllerclientimpl.h | 1 - 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8e9343f..fbe441df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -271,6 +271,18 @@ endif() set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) + +pkg_check_modules(libpcrecpp libpcrecpp) +if( libpcrecpp_FOUND ) + set(CMAKE_REQUIRED_INCLUDES ${libpcrecpp_INCLUDE_DIRS} ${REQUIRED_INCLUDES}) + check_include_file_cxx(pcrecpp.h HAVE_PCRECPP_H) + set(CMAKE_REQUIRED_INCLUDES) + if( NOT HAVE_PCRECPP_H ) + set(libpcrecpp_FOUND 0) + endif() + link_libraries(pcrecpp) +endif() + find_package(ZeroMQ QUIET) if( ZeroMQ_FOUND ) include_directories(${ZeroMQ_INCLUDE_DIR}) diff --git a/include/mujincontrollerclient/mujincontrollerclient.h b/include/mujincontrollerclient/mujincontrollerclient.h index 2cf4a7e9..80d04a3f 100644 --- a/include/mujincontrollerclient/mujincontrollerclient.h +++ b/include/mujincontrollerclient/mujincontrollerclient.h @@ -519,6 +519,14 @@ class MUJINCLIENT_API ControllerClient /// \param vdata filled with the contents of the file on the controller filesystem virtual void DownloadFileFromControllerIfModifiedSince_UTF16(const std::wstring& desturi, long localtimeval, long &remotetimeval, std::vector& vdata, double timeout = 5.0) = 0; + + virtual std::string Quote(const std::string& value) const = 0; + virtual std::string Unquote(const std::string& value) const = 0; + virtual bool ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const = 0; + virtual std::string AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment) = 0; + virtual std::string GetPrimaryKeyFromURI(const std::string& uri) const = 0; + virtual std::string GetUnicodeFromPrimaryKey(const std::string& pk) const = 0; + /// \brief Deletes a file on the controller network filesystem. /// /// \param uri UTF-8 encoded file in the network filesystem to delete. diff --git a/src/controllerclientimpl.cpp b/src/controllerclientimpl.cpp index f116beb5..0b26f0e6 100644 --- a/src/controllerclientimpl.cpp +++ b/src/controllerclientimpl.cpp @@ -1376,7 +1376,7 @@ std::string ControllerClientImpl::Quote(const std::string& value) const { std::string ControllerClientImpl::Unquote(const std::string& value) const{ if(_curl){ - int* outlength; + int* outlength = NULL; char* result = NULL; result = curl_easy_unescape(_curl, value.c_str(), value.length(), outlength); if(result){ @@ -1388,7 +1388,7 @@ std::string ControllerClientImpl::Unquote(const std::string& value) const{ bool ControllerClientImpl::ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const{ static pcrecpp::RE re("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"); - std::string s1, s3, s6, s8; + std::string s1, s3, s6, s8; if (re.FullMatch(uri, &s1, &scheme, &s3, &authority, &path, &s6, &query, &s8, &fragment)) { if(scheme == "mujin"){ diff --git a/src/controllerclientimpl.h b/src/controllerclientimpl.h index bb72abc2..9e905efc 100644 --- a/src/controllerclientimpl.h +++ b/src/controllerclientimpl.h @@ -21,7 +21,6 @@ #include #include - namespace mujinclient { class ControllerClientImpl : public ControllerClient, public boost::enable_shared_from_this From 4ca03cbccc0f27caa70bea7e00f1a32f21abf6f5 Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 28 Aug 2018 13:56:56 +0900 Subject: [PATCH 3/4] remove useless function --- .../mujincontrollerclient.h | 1 - src/controllerclientimpl.cpp | 16 ---------------- src/controllerclientimpl.h | 1 - 3 files changed, 18 deletions(-) diff --git a/include/mujincontrollerclient/mujincontrollerclient.h b/include/mujincontrollerclient/mujincontrollerclient.h index 80d04a3f..a7f41c8e 100644 --- a/include/mujincontrollerclient/mujincontrollerclient.h +++ b/include/mujincontrollerclient/mujincontrollerclient.h @@ -524,7 +524,6 @@ class MUJINCLIENT_API ControllerClient virtual std::string Unquote(const std::string& value) const = 0; virtual bool ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const = 0; virtual std::string AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment) = 0; - virtual std::string GetPrimaryKeyFromURI(const std::string& uri) const = 0; virtual std::string GetUnicodeFromPrimaryKey(const std::string& pk) const = 0; /// \brief Deletes a file on the controller network filesystem. diff --git a/src/controllerclientimpl.cpp b/src/controllerclientimpl.cpp index 0b26f0e6..fdecda0c 100644 --- a/src/controllerclientimpl.cpp +++ b/src/controllerclientimpl.cpp @@ -1443,26 +1443,10 @@ std::string ControllerClientImpl::AssembleURI(const std::string& scheme, const s return uri; } -std::string ControllerClientImpl::GetPrimaryKeyFromURI(const std::string& uri) const { - if(_curl){ - if(boost::starts_with(uri, "mujin:/")){ - // mujin uri is represented in unicode - std::string pk = uri.substr(); - - } - else if(boost::starts_with(uri, "file:/")){ - // file scheme container the full file path and it's - } - } - return ""; -} - std::string ControllerClientImpl::GetUnicodeFromPrimaryKey(const std::string& pk) const{ return Unquote(pk); } - - void ControllerClientImpl::DeleteFileOnController_UTF8(const std::string& desturi) { boost::mutex::scoped_lock lock(_mutex); diff --git a/src/controllerclientimpl.h b/src/controllerclientimpl.h index 9e905efc..73c16202 100644 --- a/src/controllerclientimpl.h +++ b/src/controllerclientimpl.h @@ -155,7 +155,6 @@ class ControllerClientImpl : public ControllerClient, public boost::enable_share std::string Unquote(const std::string& value) const; bool ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const; std::string AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment); - std::string GetPrimaryKeyFromURI(const std::string& uri) const; std::string GetUnicodeFromPrimaryKey(const std::string& pk) const; protected: From 2379c84233123ae94f7fbbed5b632fdbeae9752a Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 9 Oct 2018 16:06:51 +0900 Subject: [PATCH 4/4] make protected functions --- .../mujincontrollerclient.h | 9 +++++---- src/controllerclientimpl.cpp | 16 ++++++++-------- src/controllerclientimpl.h | 13 ++++++++----- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/include/mujincontrollerclient/mujincontrollerclient.h b/include/mujincontrollerclient/mujincontrollerclient.h index a7f41c8e..45ce1b57 100644 --- a/include/mujincontrollerclient/mujincontrollerclient.h +++ b/include/mujincontrollerclient/mujincontrollerclient.h @@ -520,10 +520,6 @@ class MUJINCLIENT_API ControllerClient virtual void DownloadFileFromControllerIfModifiedSince_UTF16(const std::wstring& desturi, long localtimeval, long &remotetimeval, std::vector& vdata, double timeout = 5.0) = 0; - virtual std::string Quote(const std::string& value) const = 0; - virtual std::string Unquote(const std::string& value) const = 0; - virtual bool ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const = 0; - virtual std::string AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment) = 0; virtual std::string GetUnicodeFromPrimaryKey(const std::string& pk) const = 0; /// \brief Deletes a file on the controller network filesystem. @@ -609,6 +605,11 @@ class MUJINCLIENT_API ControllerClient /// virtual std::string SetObjectGeometryMesh(const std::string& objectPk, const std::string& geometryPk, const std::vector& data, const std::string& unit = "mm", double timeout = 5) = 0; +protected: + virtual std::string _Quote(const std::string& value) const = 0; + virtual std::string _Unquote(const std::string& value) const = 0; + virtual bool _ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const = 0; + virtual std::string _AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment) = 0; }; class MUJINCLIENT_API WebResource diff --git a/src/controllerclientimpl.cpp b/src/controllerclientimpl.cpp index fdecda0c..c5202d38 100644 --- a/src/controllerclientimpl.cpp +++ b/src/controllerclientimpl.cpp @@ -1364,7 +1364,7 @@ void ControllerClientImpl::_DownloadFileFromController(const std::string& destur } -std::string ControllerClientImpl::Quote(const std::string& value) const { +std::string ControllerClientImpl::_Quote(const std::string& value) const { if(_curl){ char* output = curl_easy_escape(_curl, value.c_str(), value.length()); if(output){ @@ -1374,7 +1374,7 @@ std::string ControllerClientImpl::Quote(const std::string& value) const { return ""; } -std::string ControllerClientImpl::Unquote(const std::string& value) const{ +std::string ControllerClientImpl::_Unquote(const std::string& value) const{ if(_curl){ int* outlength = NULL; char* result = NULL; @@ -1386,7 +1386,7 @@ std::string ControllerClientImpl::Unquote(const std::string& value) const{ return ""; } -bool ControllerClientImpl::ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const{ +bool ControllerClientImpl::_ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const{ static pcrecpp::RE re("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?"); std::string s1, s3, s6, s8; if (re.FullMatch(uri, &s1, &scheme, &s3, &authority, &path, &s6, &query, &s8, &fragment)) @@ -1400,13 +1400,13 @@ bool ControllerClientImpl::ParseURI(const std::string& uri, std::string& scheme, fragment = fragment.substr(fragmentIndex+1); } } - path = Unquote(path); + path = _Unquote(path); return true; } return false; } -std::string ControllerClientImpl::AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment){ +std::string ControllerClientImpl::_AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment){ std::string uri = ""; uri = scheme + ":"; @@ -1426,10 +1426,10 @@ std::string ControllerClientImpl::AssembleURI(const std::string& scheme, const s else{ if(!path.empty()){ if(path[0] != '/'){ - uri += '/' + Quote(path); + uri += '/' + _Quote(path); } else{ - uri += Quote(path); + uri += _Quote(path); } } } @@ -1444,7 +1444,7 @@ std::string ControllerClientImpl::AssembleURI(const std::string& scheme, const s } std::string ControllerClientImpl::GetUnicodeFromPrimaryKey(const std::string& pk) const{ - return Unquote(pk); + return _Unquote(pk); } void ControllerClientImpl::DeleteFileOnController_UTF8(const std::string& desturi) diff --git a/src/controllerclientimpl.h b/src/controllerclientimpl.h index 73c16202..3c82378d 100644 --- a/src/controllerclientimpl.h +++ b/src/controllerclientimpl.h @@ -151,10 +151,6 @@ class ControllerClientImpl : public ControllerClient, public boost::enable_share { return _baseuri; } - std::string Quote(const std::string& value) const; - std::string Unquote(const std::string& value) const; - bool ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const; - std::string AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment); std::string GetUnicodeFromPrimaryKey(const std::string& pk) const; protected: @@ -194,6 +190,13 @@ class ControllerClientImpl : public ControllerClient, public boost::enable_share int _CallGet(const std::string& desturi, rapidjson::Document& pt, int expectedhttpcode=200, double timeout = 5.0); int _CallGet(const std::string& desturi, std::string& outputdata, int expectedhttpcode=200, double timeout = 5.0); int _CallGet(const std::string& desturi, std::vector& outputdata, int expectedhttpcode=200, double timeout = 5.0); + + + std::string _Quote(const std::string& value) const; + std::string _Unquote(const std::string& value) const; + bool _ParseURI(const std::string& uri, std::string& scheme, std::string& authority, std::string& path, std::string& query, std::string& fragment) const; + std::string _AssembleURI(const std::string& scheme, const std::string& authority, const std::string& path, const std::string& query, const std::string& fragment); + /// \brief desturi is URL-encoded. Also assume _mutex is locked. virtual void _UploadFileToController_UTF8(const std::string& filename, const std::string& desturi); @@ -230,7 +233,7 @@ class ControllerClientImpl : public ControllerClient, public boost::enable_share /// \param stream is std::pair::const_iterator, size_t>*, which gets incremented everytime this function is called. static size_t _ReadInMemoryUploadCallback(void *ptr, size_t size, size_t nmemb, void *stream); - + int _lastmode; CURL *_curl; boost::mutex _mutex;