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 ab18acad1e..b75718e05b 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 @@ -16,6 +16,7 @@ package io.netty.handler.codec.http; import io.netty.buffer.ByteBuf; +import io.netty.util.internal.StringUtil; import java.text.ParseException; import java.util.Calendar; @@ -1671,6 +1672,48 @@ public abstract class HttpHeaders implements Iterable> return false; } + /** + * Returns {@code true} if a header with the {@code name} and {@code value} exists, {@code false} otherwise. + * This also handles multiple values that are seperated with a {@code ,}. + *

+ * If {@code ignoreCase} is {@code true} then a case insensitive compare is done on the value. + * @param name the name of the header to find + * @param value the value of the header to find + * @param ignoreCase {@code true} then a case insensitive compare is run to compare values. + * otherwise a case sensitive compare is run to compare values. + */ + public boolean containsValue(CharSequence name, CharSequence value, boolean ignoreCase) { + List values = getAll(name); + if (values.isEmpty()) { + return false; + } + + for (String v: values) { + if (contains(v, value, ignoreCase)) { + return true; + } + } + return false; + } + + private static boolean contains(String value, CharSequence expected, boolean ignoreCase) { + String[] parts = StringUtil.split(value, ','); + if (ignoreCase) { + for (String s: parts) { + if (equalsIgnoreCase(expected, s.trim())) { + return true; + } + } + } else { + for (String s: parts) { + if (s.trim().contentEquals(expected)) { + return true; + } + } + } + return false; + } + /** * Returns {@code true} if a header with the name and value exists. * diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java index 7ebc4bb708..fe73c5408c 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java @@ -196,10 +196,9 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { + upgrade); } - String connection = headers.get(Names.CONNECTION); - if (!Values.UPGRADE.equalsIgnoreCase(connection)) { + if (!headers.containsValue(Names.CONNECTION, Values.UPGRADE, true)) { throw new WebSocketHandshakeException("Invalid handshake response connection: " - + connection); + + headers.get(Names.CONNECTION)); } ByteBuf challenge = response.content(); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java index 96d503d1a0..95688d509e 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker07.java @@ -170,9 +170,9 @@ public class WebSocketClientHandshaker07 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } - String connection = headers.get(Names.CONNECTION); - if (!Values.UPGRADE.equalsIgnoreCase(connection)) { - throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); + if (!headers.containsValue(Names.CONNECTION, Values.UPGRADE, true)) { + throw new WebSocketHandshakeException("Invalid handshake response connection: " + + headers.get(Names.CONNECTION)); } String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java index d30d04bb8d..416fcc9f62 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java @@ -170,9 +170,9 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } - String connection = headers.get(Names.CONNECTION); - if (!Values.UPGRADE.equalsIgnoreCase(connection)) { - throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); + if (!headers.containsValue(Names.CONNECTION, Values.UPGRADE, true)) { + throw new WebSocketHandshakeException("Invalid handshake response connection: " + + headers.get(Names.CONNECTION)); } String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java index ef88adc6c1..b46d541a95 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java @@ -180,9 +180,9 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException("Invalid handshake response upgrade: " + upgrade); } - String connection = headers.get(Names.CONNECTION); - if (!Values.UPGRADE.equalsIgnoreCase(connection)) { - throw new WebSocketHandshakeException("Invalid handshake response connection: " + connection); + if (!headers.containsValue(Names.CONNECTION, Values.UPGRADE, true)) { + throw new WebSocketHandshakeException("Invalid handshake response connection: " + + headers.get(Names.CONNECTION)); } String accept = headers.get(Names.SEC_WEBSOCKET_ACCEPT); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java index ad2f6e7df4..d352bdd671 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java @@ -109,7 +109,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { protected FullHttpResponse newHandshakeResponse(FullHttpRequest req, HttpHeaders headers) { // Serve the WebSocket handshake request. - if (!Values.UPGRADE.equalsIgnoreCase(req.headers().get(CONNECTION)) + if (!req.headers().containsValue(CONNECTION, Values.UPGRADE, true) || !WEBSOCKET.equalsIgnoreCase(req.headers().get(Names.UPGRADE))) { throw new WebSocketHandshakeException("not a WebSocket handshake request: missing upgrade"); }