diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java index 9fef840cf94..0b6e5238548 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java @@ -2,6 +2,7 @@ package org.eclipse.jetty.server.handler; import java.io.IOException; import java.net.InetSocketAddress; +import java.net.SocketTimeoutException; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; @@ -225,19 +226,25 @@ public class ConnectHandler extends HandlerWrapper SocketChannel channel; - try + try { - channel = connectToServer(request, host, port); + channel = connectToServer(request,host,port); } - catch ( IOException ioe ) + catch (SocketTimeoutException ste) { - LOG.info("ConnectHandler: " + ioe.getMessage()); - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - baseRequest.setHandled(true); + LOG.info("ConnectHandler: " + ste.getMessage()); + response.setStatus(HttpServletResponse.SC_GATEWAY_TIMEOUT); + baseRequest.setHandled(true); return; } - - + catch (IOException ioe) + { + LOG.info("ConnectHandler: " + ioe.getMessage()); + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + baseRequest.setHandled(true); + return; + } + // Transfer unread data from old connection to new connection // We need to copy the data to avoid races: // 1. when this unread data is written and the server replies before the clientToProxy @@ -320,12 +327,7 @@ public class ConnectHandler extends HandlerWrapper // may return null private SocketChannel connectToServer(HttpServletRequest request, String host, int port) throws IOException { - SocketChannel channel = connect(request, host, port); - if ( channel == null ) - { - throw new IOException("unable to connector to " + host + ":" + port); - } - + SocketChannel channel = connect(request, host, port); channel.configureBlocking(false); return channel; } @@ -342,6 +344,12 @@ public class ConnectHandler extends HandlerWrapper protected SocketChannel connect(HttpServletRequest request, String host, int port) throws IOException { SocketChannel channel = SocketChannel.open(); + + if (channel == null) + { + throw new IOException("unable to connect to " + host + ":" + port); + } + try { // Connect to remote server