Skip to content

Commit 702bcde

Browse files
author
Eduardo Gulias Davis
committed
Port of fixes: backslash in domain part
1 parent b0227e0 commit 702bcde

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/main/java/emailvalidator4j/parser/DomainPart.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
import emailvalidator4j.lexer.EmailLexer;
44
import emailvalidator4j.lexer.Token;
5+
import emailvalidator4j.lexer.TokenInterface;
56
import emailvalidator4j.lexer.Tokens;
67
import emailvalidator4j.parser.exception.*;
78

9+
import java.util.HashSet;
810
import java.util.regex.Matcher;
911
import java.util.regex.Pattern;
1012

1113
final class DomainPart extends Parser {
1214

1315
private static final int DOMAINPART_MAX_LENGTH = 255;
1416
private static final int LABEL_MAX_LENGTH = 63;
17+
private final HashSet<TokenInterface> notAllowedTokens = new HashSet<TokenInterface>(2) {{
18+
add(Tokens.BACKSLASH);
19+
add(Tokens.SLASH);
20+
}};
1521

1622
DomainPart (EmailLexer lexer) {
1723
super(lexer);
@@ -51,11 +57,7 @@ private void doParseDomainPart() throws InvalidEmail {
5157
throw new ExpectedATEXT("Expected ATEXT");
5258
}
5359

54-
if (this.lexer.getCurrent().equals(Tokens.SLASH)) {
55-
throw new DomainNotAllowedCharacter(
56-
String.format("%s is not allowed in domain part", this.lexer.getCurrent().getName())
57-
);
58-
}
60+
checkNotAllowedChars(this.lexer.getCurrent());
5961

6062
if (this.lexer.getCurrent().equals(Tokens.OPENPARETHESIS)) {
6163
if (this.lexer.getPrevious().equals(Tokens.AT)) {
@@ -83,6 +85,15 @@ private void doParseDomainPart() throws InvalidEmail {
8385
} while (!this.lexer.isAtEnd());
8486
}
8587

88+
private void checkNotAllowedChars(TokenInterface token) throws DomainNotAllowedCharacter {
89+
if (notAllowedTokens.contains(token)) {
90+
throw new DomainNotAllowedCharacter(
91+
String.format("%s is not allowed in domain part", token.getName())
92+
);
93+
}
94+
95+
}
96+
8697
private void checkLabelLength() {
8798
if (this.lexer.getCurrent().equals(Tokens.DOT) &&
8899
this.lexer.getPrevious().equals(Tokens.get(Tokens.GENERIC)) &&

src/test/java/emailvalidator4j/parser/DomainPartTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public static Object[][] invalidDomainParts() {
4747
{ConsecutiveAT.class, "@@start"},
4848
{ExpectedATEXT.class, "@at[start"},
4949
{DomainHyphen.class, "@atstart-.com"},
50-
{ExpectedATEXT.class, "@atst\\art.com"},
50+
{DomainNotAllowedCharacter.class, "@atst\\art.com"},
51+
{DomainNotAllowedCharacter.class, "@example\\"},
52+
{DomainNotAllowedCharacter.class, "@exa\\mple"}
5153
};
5254
}
5355

0 commit comments

Comments
 (0)