diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java index cda4db05d1d..d51e14053dc 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java @@ -638,35 +638,14 @@ public abstract class AbstractProxyServlet extends HttpServlet if (_log.isDebugEnabled()) _log.debug(getRequestId(clientRequest) + " proxying failed", failure); - if (proxyResponse.isCommitted()) - { - try - { - // Use Jetty specific behavior to close connection. - proxyResponse.sendError(-1); - if (clientRequest.isAsyncStarted()) - { - AsyncContext asyncContext = clientRequest.getAsyncContext(); - asyncContext.complete(); - } - } - catch (Throwable x) - { - if (_log.isDebugEnabled()) - _log.debug(getRequestId(clientRequest) + " could not close the connection", failure); - } - } - else - { - proxyResponse.resetBuffer(); - int status = failure instanceof TimeoutException ? - HttpStatus.GATEWAY_TIMEOUT_504 : - HttpStatus.BAD_GATEWAY_502; - int serverStatus = serverResponse == null ? status : serverResponse.getStatus(); - if (expects100Continue(clientRequest) && serverStatus >= HttpStatus.OK_200) - status = serverStatus; - sendProxyResponseError(clientRequest, proxyResponse, status); - } + int status = failure instanceof TimeoutException ? + HttpStatus.GATEWAY_TIMEOUT_504 : + HttpStatus.BAD_GATEWAY_502; + int serverStatus = serverResponse == null ? status : serverResponse.getStatus(); + if (expects100Continue(clientRequest) && serverStatus >= HttpStatus.OK_200) + status = serverStatus; + sendProxyResponseError(clientRequest, proxyResponse, status); + } protected int getRequestId(HttpServletRequest clientRequest) @@ -676,10 +655,24 @@ public abstract class AbstractProxyServlet extends HttpServlet protected void sendProxyResponseError(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, int status) { - proxyResponse.setStatus(status); - proxyResponse.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString()); - if (clientRequest.isAsyncStarted()) - clientRequest.getAsyncContext().complete(); + try + { + if (!proxyResponse.isCommitted()) + { + proxyResponse.resetBuffer(); + proxyResponse.setHeader(HttpHeader.CONNECTION.asString(), HttpHeaderValue.CLOSE.asString()); + } + proxyResponse.sendError(status); + } + catch(Exception e) + { + _log.ignore(e); + } + finally + { + if (clientRequest.isAsyncStarted()) + clientRequest.getAsyncContext().complete(); + } } protected void onContinue(HttpServletRequest clientRequest, Request proxyRequest) diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java index fe6118e0d62..52fd04c6a4e 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java @@ -1087,10 +1087,10 @@ public class ProxyServletTest Response response = listener.get(5, TimeUnit.SECONDS); Assert.assertEquals(504, response.getStatus()); - // Make sure there is no content, as the proxy-to-client response has been reset. + // Make sure there is error page content, as the proxy-to-client response has been reset. InputStream input = listener.getInputStream(); - Assert.assertEquals(-1, input.read()); - + String body = IO.toString(input); + Assert.assertThat(body,Matchers.containsString("HTTP ERROR: 504")); chunk1Latch.countDown(); // Result succeeds because a 504 is a valid HTTP response. diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/InetAddressSet.java b/jetty-util/src/main/java/org/eclipse/jetty/util/InetAddressSet.java index 0c616878fbf..c42f2a3ea33 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/InetAddressSet.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/InetAddressSet.java @@ -246,11 +246,11 @@ public class InetAddressSet extends AbstractSet implements Set, _octets = cidr/8; _mask = 0xff&(0xff<<(8-cidr%8)); _masked = _mask==0?0:_raw[_octets]&_mask; - + if (cidr>(_raw.length*8)) throw new IllegalArgumentException("CIDR too large: "+pattern); - if (_mask!=0 && _raw[_octets]!=_masked) + if (_mask!=0 && (0xff&_raw[_octets])!=_masked) throw new IllegalArgumentException("CIDR bits non zero: "+pattern); for (int o=_octets+(_mask==0?0:1);o<_raw.length;o++) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/InetAddressSetTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/InetAddressSetTest.java index c52585291c0..14c650980ea 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/InetAddressSetTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/InetAddressSetTest.java @@ -134,8 +134,21 @@ public class InetAddressSetTest set.add("0.0.0.0/0"); assertTrue(set.test(InetAddress.getByName("10.11.0.0"))); + + // test #1664 + set.add("2.144.0.0/14"); + set.add("2.176.0.0/12"); + set.add("5.22.0.0/17"); + set.add("5.22.192.0/19"); + assertTrue(set.test(InetAddress.getByName("2.144.0.1"))); + assertTrue(set.test(InetAddress.getByName("2.176.0.1"))); + assertTrue(set.test(InetAddress.getByName("5.22.0.1"))); + assertTrue(set.test(InetAddress.getByName("5.22.192.1"))); } + + + @Test public void testBadCIDR() throws Exception {