URIType URIHelper::parseAuthority()

in activemq-cpp/src/main/decaf/internal/net/URIHelper.cpp [271:363]


URIType URIHelper::parseAuthority( bool forceServer, const std::string& authority ) {

    try{

        URIType result( authority );

        if( authority == "" ) {
            return result;
        }

        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.rfind( ':' );
        std::size_t endindex = temp.find( ']' );

        if( index != std::string::npos && ( endindex < index || endindex == std::string::npos ) ){
            // 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",
                                (int)hostindex + (int)index + 1 );
                        }

                        return result;
                    }

                } catch( NumberFormatException& e ) {

                    if( forceServer ) {
                        throw URISyntaxException(
                            __FILE__, __LINE__,
                            authority, "Port number is malformed.",
                            (int)hostindex + (int)index + 1 );
                    }

                    return result;
                }
            }

        } else {
            tempHost = temp;
        }

        if( tempHost == "" ) {
            if( forceServer ) {
                throw URISyntaxException(
                    __FILE__, __LINE__,
                    authority, "Host name is empty", (int)hostindex );
            }
            return result;
        }

        if( !isValidHost( forceServer, tempHost ) ) {
            return result;
        }

        // this is a server based uri,
        // fill in the userinfo, host and port fields
        result.setUserInfo( tempUserinfo );
        result.setHost( tempHost );
        result.setPort( tempPort );
        result.setServerAuthority( true );

        // We know its valid now so tag it.
        result.setValid( true );

        return result;
    }
    DECAF_CATCH_RETHROW( URISyntaxException )
    DECAF_CATCH_EXCEPTION_CONVERT( Exception, URISyntaxException )
    DECAF_CATCHALL_THROW( URISyntaxException )
}