You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2008/07/06 23:50:53 UTC
svn commit: r674353 - in
/activemq/activemq-cpp/trunk/src/main/decaf/internal/net: URIHelper.cpp
URIHelper.h
Author: tabish
Date: Sun Jul 6 14:50:52 2008
New Revision: 674353
URL: http://svn.apache.org/viewvc?rev=674353&view=rev
Log:
Implementing the URIHelper class to aid the URI class in parsing URIs.
Modified:
activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.cpp
activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.h
Modified: activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.cpp?rev=674353&r1=674352&r2=674353&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.cpp Sun Jul 6 14:50:52 2008
@@ -19,9 +19,15 @@
#include <decaf/lang/Integer.h>
#include <decaf/lang/Character.h>
+#include <decaf/lang/Exception.h>
+#include <decaf/internal/net/URIEncoderDecoder.h>
+#include <decaf/net/URISyntaxException.h>
+#include <decaf/util/StringTokenizer.h>
using namespace decaf;
using namespace decaf::lang;
+using namespace decaf::util;
+using namespace decaf::net;
using namespace decaf::internal;
using namespace decaf::internal::net;
@@ -30,6 +36,200 @@
}
////////////////////////////////////////////////////////////////////////////////
+bool URIHelper::isValidDomainName( const std::string& host ) {
+
+ try {
+ URIEncoderDecoder::validateSimple( host, "-." );
+ } catch( URISyntaxException& e ) {
+ return false;
+ }
+
+ std::string label = "";
+ StringTokenizer st( host, "." );
+
+ while( st.hasMoreTokens() ) {
+ label = st.nextToken();
+ if( *(label.begin()) == '-' || *(label.rbegin()) == '-' ) {
+ return false;
+ }
+ }
+
+ if( label != host ) {
+ if( Character::isDigit( label.at( 0 ) ) ) {
+ return false;
+ }
+ }
+ return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool isValidIPv4Address( const std::string& host ) {
+
+ int index;
+ int index2;
+
+ try {
+
+ int num;
+ index = host.find( '.' );
+ num = Integer::parseInt( host.substr( 0, index ) );
+ if( num < 0 || num > 255 ) {
+ return false;
+ }
+
+ index2 = host.find( '.', index + 1 );
+ num = Integer::parseInt( host.substr( index + 1, index2 ) );
+ if( num < 0 || num > 255 ) {
+ return false;
+ }
+
+ index = host.find( '.', index2 + 1 );
+ num = Integer::parseInt( host.substr( index2 + 1, index ) );
+ if( num < 0 || num > 255 ) {
+ return false;
+ }
+
+ num = Integer::parseInt( host.substr( index + 1, std::string::npos ) );
+ if( num < 0 || num > 255 ) {
+ return false;
+ }
+
+ } catch( Exception& e ) {
+ return false;
+ }
+
+ return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool URIHelper::isValidIP6Address( const std::string& ipAddress ) {
+
+ int length = ipAddress.length();
+ bool doubleColon = false;
+ int numberOfColons = 0;
+ int numberOfPeriods = 0;
+ std::string word = "";
+ char c = 0;
+ char prevChar = 0;
+ int offset = 0; // offset for [] ip addresses
+
+ if( length < 2 ) {
+ return false;
+ }
+
+ for( int i = 0; i < length; i++ ) {
+
+ prevChar = c;
+ c = ipAddress.at( i );
+
+ switch( c ) {
+
+ // case for an open bracket [x:x:x:...x]
+ case '[':
+ if( i != 0 ) {
+ return false; // must be first character
+ }
+ if( ipAddress.at( length - 1 ) != ']' ) {
+ return false; // must have a close ]
+ }
+ if( ( ipAddress.at( 1 ) == ':' ) && ( ipAddress.at( 2 )
+ != ':' ) ) {
+ return false;
+ }
+ offset = 1;
+ if( length < 4 ) {
+ return false;
+ }
+ break;
+
+ // case for a closed bracket at end of IP [x:x:x:...x]
+ case ']':
+ if( i != length - 1 ) {
+ return false; // must be last character
+ }
+ if( ipAddress.at( 0 ) != '[' ) {
+ return false; // must have a open [
+ }
+ break;
+
+ // case for the last 32-bits represented as IPv4
+ // x:x:x:x:x:x:d.d.d.d
+ case '.':
+ numberOfPeriods++;
+ if( numberOfPeriods > 3 ) {
+ return false;
+ }
+ if( !isValidIP4Word( word ) ) {
+ return false;
+ }
+ if( numberOfColons != 6 && !doubleColon ) {
+ return false;
+ }
+ // a special case ::1:2:3:4:5:d.d.d.d allows 7 colons
+ // with
+ // an IPv4 ending, otherwise 7 :'s is bad
+ if( numberOfColons == 7 &&
+ ipAddress.at( 0 + offset ) != ':' &&
+ ipAddress.at( 1 + offset ) != ':' ) {
+
+ return false;
+ }
+ word = "";
+ break;
+
+ case ':':
+ numberOfColons++;
+ if( numberOfColons > 7 ) {
+ return false;
+ }
+ if( numberOfPeriods > 0 ) {
+ return false;
+ }
+ if( prevChar == ':' ) {
+ if( doubleColon ) {
+ return false;
+ }
+ doubleColon = true;
+ }
+ word = "";
+ break;
+
+ default:
+ if( word.length() > 3 ) {
+ return false;
+ }
+ if( !isValidHexChar( c ) ) {
+ return false;
+ }
+ word += c;
+ }
+ }
+
+ // Check if we have an IPv4 ending
+ if( numberOfPeriods > 0 ) {
+ if( numberOfPeriods != 3 || !isValidIP4Word( word ) ) {
+ return false;
+ }
+ } else {
+ // If we're at then end and we haven't had 7 colons then there
+ // is a problem unless we encountered a doubleColon
+ if( numberOfColons != 7 && !doubleColon ) {
+ return false;
+ }
+
+ // If we have an empty word at the end, it means we ended in
+ // either a : or a .
+ // If we did not end in :: then this is invalid
+ if( word == "" && ipAddress.at( length - 1 - offset ) != ':' &&
+ ipAddress.at( length - 2 - offset ) != ':' ) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+////////////////////////////////////////////////////////////////////////////////
bool URIHelper::isValidIP4Word( const std::string& word ) {
if( word.length() < 1 || word.length() > 3 ) {
Modified: activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.h?rev=674353&r1=674352&r2=674353&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/internal/net/URIHelper.h Sun Jul 6 14:50:52 2008
@@ -34,6 +34,30 @@
virtual ~URIHelper() {}
/**
+ * Validates the string past to determine if it is a well formed
+ * domain name.
+ * @param host - domain name to validate.
+ * @return true if host is well formed.
+ */
+ bool isValidDomainName( const std::string& host );
+
+ /**
+ * Validate if the host value is a well formed IPv4 address, this is
+ * the form XXX.XXX.XXX.XXX were X is any number 0-9. and XXX is not
+ * greater than 255.
+ * @param host - IPv4 address string to parse.
+ * @return true if host is a well formed IPv4 address.
+ */
+ bool isValidIPv4Address( const std::string& host );
+
+ /**
+ * Determines if the given address is valid according to the IPv6 spec.
+ * @param ipAddress - string ip address value to validate.
+ * @return true if the address string is valid.
+ */
+ bool isValidIP6Address( const std::string& ipAddress );
+
+ /**
* Check is the string passed contains a Valid IPv4 word, which is
* an integer in the range of 0 to 255.
* @param word - string value to check.