-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix problem which causes regular expression matching to become
exponentially slower on longer email addresses.
- Loading branch information
1 parent
161caca
commit ecfa4cf
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,24 @@ public final class ERXEmailValidator implements Serializable { | |
private final String mailbox; | ||
private final String patternString; | ||
private final Pattern validPattern; | ||
|
||
/** | ||
* This second validator exists because there is an issue with validating | ||
* addresses that allowQuotedIdentifiers that have no quoting and a long | ||
* mailbox name. Example: [email protected] | ||
* | ||
* It seems that after about 25 chars, the regular expression matching | ||
* takes exponentially longer to match the string. The same address with | ||
* quoting does not exhibit the problem. | ||
* Ex. "Blah blah" <[email protected]> | ||
* | ||
* Nor does using a validator that does not allow quoted identifiers. In | ||
* order to work around this problem, a second internal validator is | ||
* created when allowQuotedIdentifiers is true. This internal validator | ||
* does not allow quoted identifiers. It is tried first and only if it | ||
* returns false is the full regular expression used. | ||
*/ | ||
private final ERXEmailValidator _internal; | ||
|
||
/** | ||
* | ||
|
@@ -128,6 +146,11 @@ public ERXEmailValidator(boolean allowQuotedIdentifiers, boolean allowDomainLite | |
mailbox = nameAddr + "|" + addrSpec; | ||
patternString = allowQuotedIdentifiers ? mailbox : addrSpec; | ||
validPattern = Pattern.compile(patternString); | ||
|
||
/* | ||
* See javadoc for the _internal ivar | ||
*/ | ||
_internal = allowQuotedIdentifiers?new ERXEmailValidator(false, allowDomainLiterals):null; | ||
} | ||
|
||
/** | ||
|
@@ -140,6 +163,12 @@ public ERXEmailValidator(boolean allowQuotedIdentifiers, boolean allowDomainLite | |
* otherwise. | ||
*/ | ||
public boolean isValidEmailString(String email) { | ||
/* | ||
* See javadoc for the _internal ivar | ||
*/ | ||
if(_internal != null && _internal.isValidEmailString(email)) { | ||
return true; | ||
} | ||
return email != null && validPattern.matcher(email).matches(); | ||
} | ||
|
||
|