diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java index 72cacfe32a..2cfaf9e082 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpHeaders.java @@ -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; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java index 506a870061..9001a4223b 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpHeaders.java @@ -546,14 +546,14 @@ public abstract class HttpHeaders implements Iterable> */ 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> 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> Iterator 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> 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> for (String v: values) { if (ignoreCaseValue) { - if (v.equalsIgnoreCase(value)) { + if (equalsIgnoreCase(v, value)) { return true; } } else { diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java index ccf398d9ca..8e971111e2 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java @@ -649,9 +649,9 @@ public abstract class HttpObjectDecoder extends ReplayingDecoder