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 5fcd6599c2..170508d394 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 @@ -348,6 +348,32 @@ public class DefaultHttpHeaders extends HttpHeaders { return head == head.after; } + @Override + public boolean contains(String name, String value, boolean ignoreCaseValue) { + if (name == null) { + throw new NullPointerException("name"); + } + + int h = hash(name); + int i = index(h); + HeaderEntry e = entries[i]; + while (e != null) { + if (e.hash == h && eq(name, e.key)) { + if (ignoreCaseValue) { + if (e.value.equalsIgnoreCase(value)) { + return true; + } + } else { + if (e.value.equals(value)) { + return true; + } + } + } + e = e.next; + } + return false; + } + @Override public Set names() { 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 09fe0538f2..6c9ad5b066 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 @@ -980,12 +980,7 @@ public abstract class HttpHeaders implements Iterable> } // Multiple 'Expect' headers. Search through them. - for (String v: message.headers().getAll(Names.EXPECT)) { - if (Values.CONTINUE.equalsIgnoreCase(v)) { - return true; - } - } - return false; + return message.headers().contains(Names.EXPECT, Values.CONTINUE, true); } /** @@ -1131,17 +1126,7 @@ public abstract class HttpHeaders implements Iterable> * @return True if transfer encoding is chunked, otherwise false */ public static boolean isTransferEncodingChunked(HttpMessage message) { - List transferEncodingHeaders = message.headers().getAll(Names.TRANSFER_ENCODING); - if (transferEncodingHeaders.isEmpty()) { - return false; - } - - for (String value: transferEncodingHeaders) { - if (value.equalsIgnoreCase(Values.CHUNKED)) { - return true; - } - } - return false; + return message.headers().contains(Names.TRANSFER_ENCODING, Values.CHUNKED, true); } public static void removeTransferEncodingChunked(HttpMessage m) { @@ -1169,8 +1154,7 @@ public abstract class HttpHeaders implements Iterable> } public static boolean isContentLengthSet(HttpMessage m) { - List contentLength = m.headers().getAll(Names.CONTENT_LENGTH); - return !contentLength.isEmpty(); + return m.headers().contains(Names.CONTENT_LENGTH); } protected HttpHeaders() { } @@ -1336,4 +1320,32 @@ public abstract class HttpHeaders implements Iterable> * @return {@code this} */ public abstract HttpHeaders clear(); + + /** + * Returns {@code true} if a header with the name and value exists. + * + * @param name the headername + * @param value the value + * @param ignoreCaseValue {@code true} if case should be ignored + * @return contains {@code true} if it contains it {@code false} otherwise + */ + public boolean contains(String name, String value, boolean ignoreCaseValue) { + List values = getAll(name); + if (values.isEmpty()) { + return false; + } + + for (String v: values) { + if (ignoreCaseValue) { + if (v.equalsIgnoreCase(value)) { + return true; + } + } else { + if (v.equals(value)) { + return true; + } + } + } + return false; + } }