Skip to content

Commit

Permalink
- VALIDATOR-203: refactoring UrlValidator to use DomainValidator, rep…
Browse files Browse the repository at this point in the history
…lacing

  the rather ugly domain checking code that caused VALIDATOR-202

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/validator/trunk@589974 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Benjamin Speakmon committed Oct 30, 2007
1 parent feda05b commit 18afdcb
Showing 1 changed file with 2 additions and 49 deletions.
Expand Up @@ -339,55 +339,8 @@ protected boolean isValidAuthority(String authority) {

if (!ipV4Address) {
// Domain is hostname name
Perl5Util domainMatcher = new Perl5Util();
hostname = domainMatcher.match(DOMAIN_PATTERN, hostIP);
}

//rightmost hostname will never start with a digit.
if (hostname) {
// LOW-TECH FIX FOR VALIDATOR-202
// TODO: Rewrite to use ArrayList and .add semantics: see VALIDATOR-203
char[] chars = hostIP.toCharArray();
int size = 1;
for(int i=0; i<chars.length; i++) {
if(chars[i] == '.') {
size++;
}
}
String[] domainSegment = new String[size];
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];
if (topLevel.length() < 2 || topLevel.length() > 4) {
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;
}

// Make sure there's a host name preceding the authority.
if (segmentCount < 2) {
return false;
}
DomainValidator validator = DomainValidator.getInstance();
hostname = validator.isValid(hostIP);
}

if (!hostname && !ipV4Address) {
Expand Down

0 comments on commit 18afdcb

Please sign in to comment.