Optimize check for equals header name and value
This commit is contained in:
parent
75280ac6e3
commit
8eb88b7889
@ -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) {
|
private static int index(int hash) {
|
||||||
return hash % BUCKET_SIZE;
|
return hash % BUCKET_SIZE;
|
||||||
}
|
}
|
||||||
@ -157,7 +133,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (e.hash == h && eq(name, e.key)) {
|
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
|
||||||
e.remove();
|
e.remove();
|
||||||
HeaderEntry next = e.next;
|
HeaderEntry next = e.next;
|
||||||
if (next != null) {
|
if (next != null) {
|
||||||
@ -177,7 +153,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
|
|||||||
if (next == null) {
|
if (next == null) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (next.hash == h && eq(name, next.key)) {
|
if (next.hash == h && equalsIgnoreCase(name, next.key)) {
|
||||||
e.next = next.next;
|
e.next = next.next;
|
||||||
next.remove();
|
next.remove();
|
||||||
} else {
|
} else {
|
||||||
@ -249,7 +225,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
|
|||||||
String value = null;
|
String value = null;
|
||||||
// loop until the first header was found
|
// loop until the first header was found
|
||||||
while (e != null) {
|
while (e != null) {
|
||||||
if (e.hash == h && eq(name, e.key)) {
|
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
|
||||||
value = e.value;
|
value = e.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,7 +246,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
|
|||||||
int i = index(h);
|
int i = index(h);
|
||||||
HeaderEntry e = entries[i];
|
HeaderEntry e = entries[i];
|
||||||
while (e != null) {
|
while (e != null) {
|
||||||
if (e.hash == h && eq(name, e.key)) {
|
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
|
||||||
values.addFirst(e.value);
|
values.addFirst(e.value);
|
||||||
}
|
}
|
||||||
e = e.next;
|
e = e.next;
|
||||||
@ -316,7 +292,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
|
|||||||
int i = index(h);
|
int i = index(h);
|
||||||
HeaderEntry e = entries[i];
|
HeaderEntry e = entries[i];
|
||||||
while (e != null) {
|
while (e != null) {
|
||||||
if (e.hash == h && eq(name, e.key)) {
|
if (e.hash == h && equalsIgnoreCase(name, e.key)) {
|
||||||
if (ignoreCaseValue) {
|
if (ignoreCaseValue) {
|
||||||
if (e.value.equalsIgnoreCase(value)) {
|
if (e.value.equalsIgnoreCase(value)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -546,14 +546,14 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
|
|||||||
*/
|
*/
|
||||||
public static boolean isKeepAlive(HttpMessage message) {
|
public static boolean isKeepAlive(HttpMessage message) {
|
||||||
String connection = message.headers().get(Names.CONNECTION);
|
String connection = message.headers().get(Names.CONNECTION);
|
||||||
if (Values.CLOSE.equalsIgnoreCase(connection)) {
|
if (connection != null && equalsIgnoreCase(Values.CLOSE, connection)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (message.getProtocolVersion().isKeepAliveDefault()) {
|
if (message.getProtocolVersion().isKeepAliveDefault()) {
|
||||||
return !Values.CLOSE.equalsIgnoreCase(connection);
|
return !equalsIgnoreCase(Values.CLOSE, connection);
|
||||||
} else {
|
} 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) {
|
if (value == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (Values.CONTINUE.equalsIgnoreCase(value)) {
|
if (equalsIgnoreCase(Values.CONTINUE, value)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1132,7 +1132,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
|
|||||||
Iterator<String> valuesIt = values.iterator();
|
Iterator<String> valuesIt = values.iterator();
|
||||||
while (valuesIt.hasNext()) {
|
while (valuesIt.hasNext()) {
|
||||||
String value = valuesIt.next();
|
String value = valuesIt.next();
|
||||||
if (value.equalsIgnoreCase(Values.CHUNKED)) {
|
if (equalsIgnoreCase(value, Values.CHUNKED)) {
|
||||||
valuesIt.remove();
|
valuesIt.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1152,6 +1152,37 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
|
|||||||
return m.headers().contains(Names.CONTENT_LENGTH);
|
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() { }
|
protected HttpHeaders() { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1332,7 +1363,7 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
|
|||||||
|
|
||||||
for (String v: values) {
|
for (String v: values) {
|
||||||
if (ignoreCaseValue) {
|
if (ignoreCaseValue) {
|
||||||
if (v.equalsIgnoreCase(value)) {
|
if (equalsIgnoreCase(v, value)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -737,7 +737,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String value = sb.toString();
|
String value = sb.toString();
|
||||||
if (value.length() == 1 && value.equals("\r")) {
|
if (value.length() == 1 && value.charAt(0) == HttpConstants.CR) {
|
||||||
headers.add(name, "");
|
headers.add(name, "");
|
||||||
} else {
|
} else {
|
||||||
headers.add(name, value);
|
headers.add(name, value);
|
||||||
@ -793,9 +793,9 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
|
|||||||
} else {
|
} else {
|
||||||
String[] header = splitHeader(line);
|
String[] header = splitHeader(line);
|
||||||
String name = header[0];
|
String name = header[0];
|
||||||
if (!name.equalsIgnoreCase(HttpHeaders.Names.CONTENT_LENGTH) &&
|
if (!HttpHeaders.equalsIgnoreCase(name, HttpHeaders.Names.CONTENT_LENGTH) &&
|
||||||
!name.equalsIgnoreCase(HttpHeaders.Names.TRANSFER_ENCODING) &&
|
!HttpHeaders.equalsIgnoreCase(name, HttpHeaders.Names.TRANSFER_ENCODING) &&
|
||||||
!name.equalsIgnoreCase(HttpHeaders.Names.TRAILER)) {
|
!HttpHeaders.equalsIgnoreCase(name, HttpHeaders.Names.TRAILER)) {
|
||||||
headers.add(name, header[1]);
|
headers.add(name, header[1]);
|
||||||
}
|
}
|
||||||
lastHeader = name;
|
lastHeader = name;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user