Optimize check for equals header name and value

This commit is contained in:
Norman Maurer 2013-11-24 14:32:55 +01:00
parent 75280ac6e3
commit 8eb88b7889
3 changed files with 46 additions and 39 deletions

View File

@ -50,30 +50,6 @@ public class DefaultHttpHeaders extends HttpHeaders {
}
}
private static boolean eq(String name1, String name2) {
int nameLen = name1.length();
if (nameLen != name2.length()) {
return false;
}
for (int i = nameLen - 1; i >= 0; i --) {
char c1 = name1.charAt(i);
char c2 = name2.charAt(i);
if (c1 != c2) {
if (c1 >= 'A' && c1 <= 'Z') {
c1 += 32;
}
if (c2 >= 'A' && c2 <= 'Z') {
c2 += 32;
}
if (c1 != c2) {
return false;
}
}
}
return true;
}
private static int index(int hash) {
return hash % BUCKET_SIZE;
}
@ -157,7 +133,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
}
for (;;) {
if (e.hash == h && eq(name, e.key)) {
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
e.remove();
HeaderEntry next = e.next;
if (next != null) {
@ -177,7 +153,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
if (next == null) {
break;
}
if (next.hash == h && eq(name, next.key)) {
if (next.hash == h && equalsIgnoreCase(name, next.key)) {
e.next = next.next;
next.remove();
} else {
@ -249,7 +225,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
String value = null;
// loop until the first header was found
while (e != null) {
if (e.hash == h && eq(name, e.key)) {
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
value = e.value;
}
@ -270,7 +246,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
int i = index(h);
HeaderEntry e = entries[i];
while (e != null) {
if (e.hash == h && eq(name, e.key)) {
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
values.addFirst(e.value);
}
e = e.next;
@ -316,7 +292,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
int i = index(h);
HeaderEntry e = entries[i];
while (e != null) {
if (e.hash == h && eq(name, e.key)) {
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
if (ignoreCaseValue) {
if (e.value.equalsIgnoreCase(value)) {
return true;

View File

@ -546,14 +546,14 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
*/
public static boolean isKeepAlive(HttpMessage message) {
String connection = message.headers().get(Names.CONNECTION);
if (Values.CLOSE.equalsIgnoreCase(connection)) {
if (connection != null && equalsIgnoreCase(Values.CLOSE, connection)) {
return false;
}
if (message.getProtocolVersion().isKeepAliveDefault()) {
return !Values.CLOSE.equalsIgnoreCase(connection);
return !equalsIgnoreCase(Values.CLOSE, connection);
} else {
return Values.KEEP_ALIVE.equalsIgnoreCase(connection);
return equalsIgnoreCase(Values.KEEP_ALIVE, connection);
}
}
@ -973,7 +973,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
if (value == null) {
return false;
}
if (Values.CONTINUE.equalsIgnoreCase(value)) {
if (equalsIgnoreCase(Values.CONTINUE, value)) {
return true;
}
@ -1132,7 +1132,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
Iterator<String> valuesIt = values.iterator();
while (valuesIt.hasNext()) {
String value = valuesIt.next();
if (value.equalsIgnoreCase(Values.CHUNKED)) {
if (equalsIgnoreCase(value, Values.CHUNKED)) {
valuesIt.remove();
}
}
@ -1152,6 +1152,37 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
return m.headers().contains(Names.CONTENT_LENGTH);
}
/**
* Returns {@code true} if both {@link CharSequence}'s are equals when ignore the case.
* This only supports US_ASCII.
*/
public static boolean equalsIgnoreCase(CharSequence name1, CharSequence name2) {
if (name1 == name2) {
return true;
}
int nameLen = name1.length();
if (nameLen != name2.length()) {
return false;
}
for (int i = nameLen - 1; i >= 0; i --) {
char c1 = name1.charAt(i);
char c2 = name2.charAt(i);
if (c1 != c2) {
if (c1 >= 'A' && c1 <= 'Z') {
c1 += 32;
}
if (c2 >= 'A' && c2 <= 'Z') {
c2 += 32;
}
if (c1 != c2) {
return false;
}
}
}
return true;
}
protected HttpHeaders() { }
/**
@ -1332,7 +1363,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
for (String v: values) {
if (ignoreCaseValue) {
if (v.equalsIgnoreCase(value)) {
if (equalsIgnoreCase(v, value)) {
return true;
}
} else {

View File

@ -737,7 +737,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
}
String value = sb.toString();
if (value.length() == 1 && value.equals("\r")) {
if (value.length() == 1 && value.charAt(0) == HttpConstants.CR) {
headers.add(name, "");
} else {
headers.add(name, value);
@ -793,9 +793,9 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
} else {
String[] header = splitHeader(line);
String name = header[0];
if (!name.equalsIgnoreCase(HttpHeaders.Names.CONTENT_LENGTH) &&
!name.equalsIgnoreCase(HttpHeaders.Names.TRANSFER_ENCODING) &&
!name.equalsIgnoreCase(HttpHeaders.Names.TRAILER)) {
if (!HttpHeaders.equalsIgnoreCase(name, HttpHeaders.Names.CONTENT_LENGTH) &&
!HttpHeaders.equalsIgnoreCase(name, HttpHeaders.Names.TRANSFER_ENCODING) &&
!HttpHeaders.equalsIgnoreCase(name, HttpHeaders.Names.TRAILER)) {
headers.add(name, header[1]);
}
lastHeader = name;