activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r674371 - in /activemq/activemq-cpp/trunk/src/main/decaf: internal/net/URIHelper.cpp internal/net/URIHelper.h net/URISyntaxException.h net/UnknownHostException.h
Date Mon, 07 Jul 2008 00:22:59 GMT
Author: tabish
Date: Sun Jul  6 17:22:59 2008
New Revision: 674371

URL: http://svn.apache.org/viewvc?rev=674371&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
    activemq/activemq-cpp/trunk/src/main/decaf/net/URISyntaxException.h
    activemq/activemq-cpp/trunk/src/main/decaf/net/UnknownHostException.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=674371&r1=674370&r2=674371&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 17:22:59
2008
@@ -21,18 +21,222 @@
 #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>
+#include <decaf/lang/exceptions/NumberFormatException.h>
 
 using namespace decaf;
 using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
 using namespace decaf::util;
 using namespace decaf::net;
 using namespace decaf::internal;
 using namespace decaf::internal::net;
 
 ////////////////////////////////////////////////////////////////////////////////
-URIHelper::URIHelper() {
+URIHelper::URIHelper( const std::string& allLegal ) {
+    this->allLegal = allLegal;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void URIHelper::validateQuery( const std::string& uri, const std::string& query,
+                               std::size_t index ) throw( URISyntaxException ) {
+
+    try {
+        URIEncoderDecoder::validate( query, allLegal );
+    } catch( URISyntaxException& e ) {
+        throw URISyntaxException(
+            __FILE__, __LINE__,
+            uri, "Invalid URI Query", index + e.getIndex() );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void URIHelper::validateFragment( const std::string& uri, const std::string& fragment,
+                                  std::size_t index ) throw( URISyntaxException ) {
+
+    try {
+        URIEncoderDecoder::validate( fragment, allLegal );
+    } catch( URISyntaxException& e ) {
+        throw URISyntaxException(
+            __FILE__, __LINE__,
+            uri, "Invalid URI Fragment", index + e.getIndex() );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void URIHelper::parseAuthority( bool forceServer, const std::string& authority )
+    throw( URISyntaxException ) {
+
+    try{
+
+        if( authority == "" ) {
+            return;
+        }
+
+        std::string temp, tempUserinfo = "", tempHost = "";
+        std::size_t index, hostindex = 0;
+        int tempPort = -1;
+
+        temp = authority;
+        index = temp.find( '@' );
+        if( index != std::string::npos ) {
+            // remove user info
+            tempUserinfo = temp.substr( 0, index );
+            validateUserinfo( authority, tempUserinfo, 0 );
+            temp = temp.substr( index + 1, std::string::npos ); // host[:port] is left
+            hostindex = index + 1;
+        }
+
+        index = temp.find_last_of( ':' );
+        std::size_t endindex = temp.find( ']' );
+
+        if( index != std::string::npos && endindex < index ) {
+            // determine port and host
+            tempHost = temp.substr( 0, index );
+
+            if( index < ( temp.length() - 1 ) ) { // port part is not empty
+                try {
+
+                    tempPort = Integer::parseInt( temp.substr( index + 1, std::string::npos
) );
+                    if( tempPort < 0 ) {
+
+                        if( forceServer ) {
+                            throw URISyntaxException(
+                                __FILE__, __LINE__,
+                                authority, "Port number is missing",
+                                hostindex + index + 1 );
+                        }
+
+                        return;
+                    }
+                } catch( NumberFormatException& e ) {
+
+                    if( forceServer ) {
+                        throw URISyntaxException(
+                            __FILE__, __LINE__,
+                            authority, "Port number is malformed.",
+                            hostindex + index + 1 );
+                    }
+
+                    return;
+                }
+            }
+
+        } else {
+            tempHost = temp;
+        }
+
+        if( tempHost == "" ) {
+            if( forceServer ) {
+                throw URISyntaxException(
+                    __FILE__, __LINE__,
+                    authority, "Host name is empty", hostindex );
+            }
+            return;
+        }
+
+        if( !isValidHost( forceServer, tempHost ) ) {
+            return;
+        }
+
+        // this is a server based uri,
+        // fill in the userinfo, host and port fields
+
+        // TODO - Get the parsed Data back to the caller.
+//        userinfo = tempUserinfo;
+//        host = tempHost;
+//        port = tempPort;
+//        serverAuthority = true;
+    }
+    DECAF_CATCH_RETHROW( URISyntaxException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, URISyntaxException )
+    DECAF_CATCHALL_THROW( URISyntaxException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void validateUserinfo( const std::string& uri, const std::string& userinfo, std::size_t
index )
+    throw( URISyntaxException ) {
+
+    for( std::size_t i = 0; i < userinfo.length(); i++ ) {
+
+        char ch = userinfo.at( i );
+        if( ch == ']' || ch == '[' ) {
+            throw URISyntaxException(
+                __FILE__, __LINE__,
+                uri, "User Info cannot contain '[' or ']'", index + i );
+        }
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool URIHelper::isValidHost( bool forceServer, const std::string& host )
+    throw( URISyntaxException ) {
+
+    try{
+
+        if( host.at( 0 ) == '[' ) {
+
+            // ipv6 address
+            if( host.at( host.length() - 1 ) != ']' ) {
+                throw URISyntaxException(
+                    __FILE__, __LINE__,
+                    host, "Host address does not end in ']'", 0 );
+            }
+
+            if( !isValidIP6Address( host ) ) {
+                throw URISyntaxException(
+                    __FILE__, __LINE__,
+                    host, "Host IPv6 address is not valid" );
+            }
+
+            return true;
+        }
+
+        // '[' and ']' can only be the first char and last char
+        // of the host name
+        if( host.find( '[' ) != std::string::npos ||
+            host.find( ']' ) != std::string::npos ) {
+
+            throw URISyntaxException(
+                __FILE__, __LINE__,
+                host, "Unexpected '[' or ']' found in address" );
+        }
+
+        std::size_t index = host.find_last_of( '.' );
+
+        if( index == std::string::npos || index == host.length() - 1 ||
+            !Character::isDigit( host.at( index + 1 ) ) ) {
+
+            // domain name
+            if( isValidDomainName( host ) ) {
+                return true;
+            }
+
+            if( forceServer ) {
+                throw URISyntaxException(
+                    __FILE__, __LINE__,
+                    host, "Host address is not valid" );
+            }
+
+            return false;
+        }
+
+        // IPv4 address
+        if( isValidIPv4Address( host ) ) {
+            return true;
+        }
+
+        if( forceServer ) {
+            throw URISyntaxException(
+                __FILE__, __LINE__,
+                host, "Host IPv4 address is not valid" );
+        }
+
+        return false;
+    }
+    DECAF_CATCH_RETHROW( URISyntaxException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, URISyntaxException )
+    DECAF_CATCHALL_THROW( URISyntaxException )
 }
 
 ////////////////////////////////////////////////////////////////////////////////

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=674371&r1=674370&r2=674371&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 17:22:59
2008
@@ -19,6 +19,7 @@
 #define _DECAF_INTERNAL_NET_URIHELPER_H_
 
 #include <string>
+#include <decaf/net/URISyntaxException.h>
 
 namespace decaf {
 namespace internal {
@@ -28,12 +29,86 @@
      * Helper class used by the URI classes in encoding and decoding of URI's.
      */
     class URIHelper {
+    private:
+
+        /**
+         * All characters that are legal in a URI
+         */
+        std::string allLegal;
+
     public:
 
-        URIHelper();
+        /**
+         * Create a URIHelper with the assigned set of Legal URI Characters.
+         * @param allLegal - All Characters that are legal in this URI instance.
+         */
+        URIHelper( const std::string& allLegal );
+
         virtual ~URIHelper() {}
 
         /**
+         * Validate that the URI Query Segment contains no invalid encodings.
+         * @param uri - the full uri.
+         * @param query - the query to check.
+         * @param index - position in the uri where fragment starts.
+         * @throw URISyntaxException if the fragment has errors.
+         */
+        void validateQuery( const std::string& uri, const std::string& query,
+                            std::size_t index )
+            throw( decaf::net::URISyntaxException );
+
+        /**
+         * Validate that the URI fragment contains no invalid encodings.
+         * @param uri - the full uri.
+         * @param fragment - the fragment to check.
+         * @param index - position in the uri where fragment starts.
+         * @throw URISyntaxException if the fragment has errors.
+         */
+        void validateFragment( const std::string& uri, const std::string& fragment,
+                               std::size_t index )
+            throw( decaf::net::URISyntaxException );
+
+        /**
+         * determine the host, port and user-info if the authority parses
+         * successfully to a server based authority
+         * <p>
+         * behavior in error cases: if forceServer is true, throw
+         * URISyntaxException with the proper diagnostic messages. if
+         * forceServer is false assume this is a registry based uri, and just
+         * return leaving the host, port and user-info fields undefined.
+         * <p>
+         * and there are some error cases where URISyntaxException is thrown
+         * regardless of the forceServer parameter e.g. mal-formed ipv6 address
+         * <p>
+         * @param forceServer
+         * @param authority
+         * @throw URISyntaxException
+         */
+        void parseAuthority( bool forceServer, const std::string& authority )
+            throw( decaf::net::URISyntaxException );
+
+        /**
+         * Check the supplied user info for validity.
+         * @param uri - the uri to parse.
+         * @param userinfo - supplied user info
+         * @return true if valid
+         * @throw URISyntaxException if an error occurs
+         */
+        void validateUserinfo( const std::string& uri, const std::string& userinfo,
std::size_t index )
+            throw( decaf::net::URISyntaxException );
+
+        /**
+         * distinguish between IPv4, IPv6, domain name and validate it based on
+         * its type
+         * @param forceServer
+         * @param host - Host string to validate
+         * @return true if the host value if a valid domain name
+         * @throws URISyntaxException if the host is invalid and forceServer is true.
+         */
+        bool isValidHost( bool forceServer, const std::string& host )
+            throw( decaf::net::URISyntaxException );
+
+        /**
          * Validates the string past to determine if it is a well formed
          * domain name.
          * @param host - domain name to validate.

Modified: activemq/activemq-cpp/trunk/src/main/decaf/net/URISyntaxException.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/net/URISyntaxException.h?rev=674371&r1=674370&r2=674371&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/net/URISyntaxException.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/net/URISyntaxException.h Sun Jul  6 17:22:59
2008
@@ -67,7 +67,32 @@
 
         /**
          * Constructor - Initializes the file name and line number where
-         * this message occured.  Sets the input string that caused the error
+         * this message occurred.  Sets the message to report, using an
+         * optional list of arguments to parse into the message
+         * @param file name where exception occurs
+         * @param line number where the exception occurred.
+         * @param message to report
+         * @param list of primitives that are formatted into the message
+         */
+        URISyntaxException( const char* file, const int lineNumber,
+                            const char* msg DECAF_UNUSED ) throw ()
+        : Exception()
+        {
+
+            this->reason = "<Unknown Reason>";
+            this->input = "<No Address Given>";
+            this->index = -1;
+
+            const char * message = "Input: %s, Reason it failed: %s";
+            this->setMessage( message, input.c_str(), reason.c_str() );
+
+            // Set the first mark for this exception.
+            setMark( file, lineNumber );
+        }
+
+        /**
+         * Constructor - Initializes the file name and line number where
+         * this message occurred.  Sets the input string that caused the error
          * and the reason for the error.
          * @param file name where exception occurs
          * @param line number where the exception occurred.
@@ -92,7 +117,7 @@
 
         /**
          * Constructor - Initializes the file name and line number where
-         * this message occured.  Sets the input string that caused the error
+         * this message occurred.  Sets the input string that caused the error
          * and the reason for the error.
          * @param file name where exception occurs
          * @param line number where the exception occurred.

Modified: activemq/activemq-cpp/trunk/src/main/decaf/net/UnknownHostException.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/net/UnknownHostException.h?rev=674371&r1=674370&r2=674371&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/net/UnknownHostException.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/net/UnknownHostException.h Sun Jul  6 17:22:59
2008
@@ -53,7 +53,7 @@
 
         /**
          * Constructor - Initializes the file name and line number where
-         * this message occured.  Sets the message to report, using an
+         * this message occurred.  Sets the message to report, using an
          * optional list of arguments to parse into the message
          * @param file name where exception occurs
          * @param line number where the exception occurred.
@@ -61,7 +61,7 @@
          * @param list of primitives that are formatted into the message
          */
         UnknownHostException( const char* file, const int lineNumber,
-                            const char* msg, ... ) throw ()
+                              const char* msg, ... ) throw ()
         : io::IOException()
         {
             va_list vargs ;



Mime
View raw message