if ( authority == null )
{
return false;
}
Perl5Util authorityMatcher = new Perl5Util();
Perl5Util matchIPV4Pat = new Perl5Util();
if ( !authorityMatcher.match( AUTHORITY_PATTERN, authority ) )
{
return false;
}
boolean ipV4Address = false;
boolean hostname = false;
// check if authority is IP address or hostname
String hostIP = authorityMatcher.group( PARSE_AUTHORITY_HOST_IP );
ipV4Address = matchIPV4Pat.match( IP_V4_DOMAIN_PATTERN, hostIP );
if ( ipV4Address )
{
// this is an IP address so check components
for ( int i = 1; i <= 4; i++ )
{
String ipSegment = matchIPV4Pat.group( i );
if ( ipSegment == null || ipSegment.length() <= 0 )
{
return false;
}
try
{
if ( Integer.parseInt( ipSegment ) > 255 )
{
return false;
}
}
catch ( NumberFormatException e )
{
return false;
}
}
}
else
{
// Domain is hostname name
Perl5Util domainMatcher = new Perl5Util();
hostname = domainMatcher.match( DOMAIN_PATTERN, hostIP );
}
//rightmost hostname will never start with a digit.
if ( hostname )
{
String[] domainSegment = new String[10];
boolean match = true;
int segmentCount = 0;
int segmentLength = 0;
Perl5Util atomMatcher = new Perl5Util();
while ( match )
{
match = atomMatcher.match( ATOM_PATTERN, hostIP );
if ( match )
{
domainSegment[segmentCount] = atomMatcher.group( 1 );
segmentLength = domainSegment[segmentCount].length() + 1;
hostIP = ( segmentLength >= hostIP.length() ) ? "" : hostIP.substring( segmentLength );
segmentCount++;
}
}
String topLevel = domainSegment[segmentCount - 1];
// don't check toplevel when we have only a server name like localhost
if ( segmentCount != 1 && (topLevel.length() < 2 ) )
{
return false;
}
// First letter of top level must be a alpha
Perl5Util alphaMatcher = new Perl5Util();
if ( !alphaMatcher.match( ALPHA_PATTERN, topLevel.substring( 0, 1 ) ) )
{
return false;
}
}
if ( !hostname && !ipV4Address )
{
return false;
}
String port = authorityMatcher.group( PARSE_AUTHORITY_PORT );
if ( port != null )
{
Perl5Util portMatcher = new Perl5Util();
if ( !portMatcher.match( PORT_PATTERN, port ) )
{
return false;
}
}