@@ -18,15 +18,16 @@ under the License.
1818-----------------------------------------------------------------------*/
1919#include " EtpHelpers.h"
2020
21- #if (defined(_WIN32) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))))
2221#include < regex>
23- #endif
2422
25- #include " AbstractSession.h"
2623#include " EtpException.h"
2724
2825std::string ETP_NS::EtpHelpers::getDataObjectType (const std::string& uri)
2926{
27+ if (!ETP_NS::EtpHelpers::validateDataObjectUri (uri)) {
28+ throw ETP_NS::EtpException (9 , " The dataobject ETP URI \" " + uri + " \" is invalid." );
29+ }
30+
3031 const std::size_t lastSlash = uri.find_last_of (" /" );
3132 const std::size_t lastOpenParenthesis = uri.find_last_of (" (" );
3233 return lastSlash != std::string::npos && lastOpenParenthesis != std::string::npos
@@ -36,9 +37,13 @@ std::string ETP_NS::EtpHelpers::getDataObjectType(const std::string& uri)
3637
3738std::string ETP_NS::EtpHelpers::getDataspaceUri (const std::string& uri)
3839{
40+ if (!ETP_NS::EtpHelpers::validateUri (uri)) {
41+ throw ETP_NS::EtpException (9 , " The ETP URI \" " + uri + " \" is invalid." );
42+ }
43+
3944 const size_t dataspacePos = uri.find (" dataspace('" );
4045 if (dataspacePos == std::string::npos) {
41- return " " ;
46+ return " eml:/// " ;
4247 }
4348
4449 const size_t closingParenthesisPos = uri.find (' )' , dataspacePos);
@@ -48,72 +53,23 @@ std::string ETP_NS::EtpHelpers::getDataspaceUri(const std::string& uri)
4853 return uri.substr (0 , closingParenthesisPos+1 );
4954}
5055
51- Energistics::Etp::v12::Datatypes::ErrorInfo ETP_NS::EtpHelpers::validateUri (const std::string & uri, ETP_NS::AbstractSession* session )
56+ bool ETP_NS::EtpHelpers::validateUri (const std::string & uri)
5257{
53- Energistics::Etp::v12::Datatypes::ErrorInfo errorInfo;
54- errorInfo.code = -1 ;
5558 // Regular expressions are not handled before GCC 4.9
5659 // https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions
57- #if (defined(_WIN32) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))))
58- const bool result =
60+ return
5961 std::regex_match (uri, std::regex (" ^eml:///(dataspace[(]'.*'[)])?" , std::regex::ECMAScript)) ||
6062 std::regex_match (uri, std::regex (" ^eml:///(dataspace[(]'.*'[)]/)?(resqml20|eml20)\.obj_[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]" , std::regex::ECMAScript)) ||
6163 std::regex_match (uri, std::regex (" ^eml:///(dataspace[(]'.*'[)]/)?(witsml|resqml|prodml|eml)([0-9]{2})\[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]" , std::regex::ECMAScript));
62- if (!result) {
63- std::cerr << " The URI \" " + uri + " \" is invalid." << std::endl;
64- }
65- #else
66- const bool result = uri.find (" eml:///" ) == 0 ;
67- #endif
68-
69- if (!result) {
70- errorInfo.code = 9 ;
71- errorInfo.message = " The URI " + uri + " is invalid." ;
72-
73- if (session != nullptr ) {
74- Energistics::Etp::v12::Protocol::Core::ProtocolException error;
75- error.error .emplace (errorInfo);
76- session->send (error);
77- }
78- }
79-
80- return errorInfo;
81-
8264}
8365
84- Energistics::Etp::v12::Datatypes::ErrorInfo ETP_NS::EtpHelpers::validateDataObjectUri (const std::string & uri, AbstractSession* session )
66+ bool ETP_NS::EtpHelpers::validateDataObjectUri (const std::string & uri)
8567{
86- Energistics::Etp::v12::Datatypes::ErrorInfo errorInfo;
87- errorInfo.code = -1 ;
8868 // Regular expressions are not handled before GCC 4.9
8969 // https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions
90- #if (defined(_WIN32) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))))
91- const bool result = (uri.find (" resqml20" ) != std::string::npos || uri.find (" eml20" ) != std::string::npos)
70+ return (uri.find (" resqml20" ) != std::string::npos || uri.find (" eml20" ) != std::string::npos)
9271 ? std::regex_match (uri, std::regex (" ^eml:///(dataspace[(]'.*'[)]/)?(resqml20|eml20)\.obj_[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]" , std::regex::ECMAScript))
9372 : std::regex_match (uri, std::regex (" ^eml:///(dataspace[(]'.*'[)]/)?(witsml|resqml|prodml|eml)([0-9]{2})\.[a-zA-Z0-9]+[(][a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}(,.*)?[)]" , std::regex::ECMAScript));
94- if (!result) {
95- std::cerr << " The data object URI \" " + uri + " \" is invalid." << std::endl;
96- }
97- #else
98- const bool result = uri.find (" eml:///" ) == 0 &&
99- (uri.find (" resqml20.obj_" ) != std::string::npos || uri.find (" eml20.obj_" ) != std::string::npos ||
100- uri.find (" witsml20." ) != std::string::npos || uri.find (" eml21." ) != std::string::npos ||
101- uri.find (" prodml21." ) != std::string::npos || uri.find (" eml22." ) != std::string::npos ||
102- uri.find (" witsml21." ) != std::string::npos);
103- #endif
104-
105- if (!result) {
106- errorInfo.code = 9 ;
107- errorInfo.message = " The data object URI " + uri + " is invalid." ;
108-
109- if (session != nullptr ) {
110- Energistics::Etp::v12::Protocol::Core::ProtocolException error;
111- error.error .emplace (errorInfo);
112- session->send (error);
113- }
114- }
115-
116- return errorInfo;
11773}
11874
11975Energistics::Etp::v12::Protocol::Core::ProtocolException ETP_NS::EtpHelpers::buildSingleMessageProtocolException (int32_t m_code, const std::string & m_message)
@@ -131,11 +87,11 @@ std::pair<std::string, std::string> ETP_NS::EtpHelpers::getUuidAndVersionFromUri
13187{
13288 std::pair<std::string, std::string> result;
13389
134- Energistics::Etp::v12::Datatypes::ErrorInfo error = ETP_NS::EtpHelpers::validateDataObjectUri (uri);
135- if (error.code > -1 ) {
136- throw ETP_NS::EtpException (error.code , error.message );
90+ if (!ETP_NS::EtpHelpers::validateDataObjectUri (uri)) {
91+ throw ETP_NS::EtpException (9 , " Invalid URI" );
13792 }
13893
94+ // old code to be updated
13995 if (uri[6 ] != ' /' ) {
14096 throw ETP_NS::EtpException (2 , " The URI " + uri + " uses some dataspaces. This agent does not support dataspace." );
14197 }
0 commit comments