From 5a59cd19f82bd7dd93919ad09470d2feda21de49 Mon Sep 17 00:00:00 2001 From: Jeff Mesnil Date: Fri, 20 Jan 2017 15:22:25 +0100 Subject: [PATCH] [ARTEMIS-931] Improve HTTP Upgrade connection If the object received is not an expected HttpResponse, close the ChannelHandlerContext and countdown the latch JIRA: https://issues.apache.org/jira/browse/ARTEMIS-931 --- .../artemis/core/client/ActiveMQClientLogger.java | 4 ++++ .../core/remoting/impl/netty/NettyConnector.java | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/ActiveMQClientLogger.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/ActiveMQClientLogger.java index 6ed0e12a50..eeff924c1b 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/ActiveMQClientLogger.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/client/ActiveMQClientLogger.java @@ -433,4 +433,8 @@ public interface ActiveMQClientLogger extends BasicLogger { format = Message.Format.MESSAGE_FORMAT) void reconnectCreatingNewSession(long id); + @LogMessage(level = Logger.Level.ERROR) + @Message(id = 214029, value = "Unexpected response from HTTP server: %s") + void unexpectedResponseFromHttpServer(Object response); + } diff --git a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java index c809b96625..c317ec919a 100644 --- a/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java +++ b/artemis-core-client/src/main/java/org/apache/activemq/artemis/core/remoting/impl/netty/NettyConnector.java @@ -70,6 +70,7 @@ import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.HttpClientCodec; +import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObject; @@ -737,9 +738,14 @@ public class NettyConnector extends AbstractConnector { @Override public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception { - if (msg instanceof HttpResponse) { + try { + if (!(msg instanceof HttpResponse)) { + ActiveMQClientLogger.LOGGER.unexpectedResponseFromHttpServer(msg); + ctx.close(); + return; + } HttpResponse response = (HttpResponse) msg; - if (response.getStatus().code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code() && response.headers().get(HttpHeaders.Names.UPGRADE).equals(ACTIVEMQ_REMOTING)) { + if (response.status().code() == HttpResponseStatus.SWITCHING_PROTOCOLS.code() && response.headers().get(HttpHeaderNames.UPGRADE).equals(ACTIVEMQ_REMOTING)) { String accept = response.headers().get(SEC_ACTIVEMQ_REMOTING_ACCEPT); String expectedResponse = createExpectedResponse(MAGIC_NUMBER, ctx.channel().attr(REMOTING_KEY).get()); @@ -754,10 +760,11 @@ public class NettyConnector extends AbstractConnector { ActiveMQClientLogger.LOGGER.httpHandshakeFailed(accept, expectedResponse); ctx.close(); } - } else if (response.getStatus().code() == HttpResponseStatus.FORBIDDEN.code()) { + } else if (response.status().code() == HttpResponseStatus.FORBIDDEN.code()) { ActiveMQClientLogger.LOGGER.httpUpgradeNotSupportedByRemoteAcceptor(); ctx.close(); } + } finally { latch.countDown(); } }