Revert "tighten the isUrlEncoded check"

This reverts commit 82ab88d589.
This commit is contained in:
Andrew Gaul 2015-05-27 17:46:24 -07:00
parent 82ab88d589
commit c22afc5a55
2 changed files with 3 additions and 29 deletions

View File

@ -90,33 +90,11 @@ public class Strings2 {
public static boolean isCidrFormat(String in) {
return CIDR_PATTERN.matcher(in).matches();
}
// taken from https://docs.oracle.com/javase/7/docs/api/java/net/URI.html#legal-chars
private static final Pattern URL_VALID_PATTERN = Pattern.compile("[a-zA-Z0-9_\\-!.~'()*,;:$&+=?/\\[\\]@%]+");
private static boolean isHexadecimal(char ch) {
return (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f') || (ch >= '0' && ch <= '9');
}
private static final Pattern URL_ENCODED_PATTERN = Pattern.compile(".*%[a-fA-F0-9][a-fA-F0-9].*");
public static boolean isUrlEncoded(String in) {
if (!URL_VALID_PATTERN.matcher(in).matches()) {
return false;
}
// ensure that all % are followed by 2 hexadecimal characters
int percentIdx = 0;
while ((percentIdx = in.indexOf('%', percentIdx)) != -1) {
if (percentIdx + 2 >= in.length()) {
return false;
}
if (!isHexadecimal(in.charAt(percentIdx + 1)) ||
!isHexadecimal(in.charAt(percentIdx + 2))) {
return false;
}
percentIdx += 2;
}
return true;
return URL_ENCODED_PATTERN.matcher(in).matches();
}
/**

View File

@ -30,10 +30,6 @@ public class Strings2Test {
public void testIsEncoded() {
assert Strings2.isUrlEncoded("/read-tests/%73%6f%6d%65%20%66%69%6c%65");
assert !Strings2.isUrlEncoded("/read-tests/ tep");
assert !Strings2.isUrlEncoded("/read-tests/dealde%2Fl04 011e%204c8df");
assert !Strings2.isUrlEncoded("/read-tests/%/");
assert !Strings2.isUrlEncoded("/read-tests/%ZZ");
assert Strings2.isUrlEncoded("/read-tests/%20");
}
public void testNoDoubleEncode() {