private synchronized int flushBuffer() throws IOException { if (!_endp.isOpen()) throw new EofException(); if (_buffer != null && _buffer.hasContent()) return _endp.flush(_buffer); return 0; }
@Test public void testMaxIdleWithRequest11NoClientClose() throws Exception { final Exchanger<EndPoint> exchanger = new Exchanger<>(); configureServer( new EchoHandler() { @Override public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { exchanger.exchange(baseRequest.getHttpChannel().getEndPoint()); } catch (Exception e) { e.printStackTrace(); } super.handle(target, baseRequest, request, response); } }); Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort()); client.setSoTimeout(10000); assertFalse(client.isClosed()); OutputStream os = client.getOutputStream(); InputStream is = client.getInputStream(); String content = "Wibble"; byte[] contentB = content.getBytes("utf-8"); os.write( ("POST /echo HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: " + contentB.length + "\r\n" + "connection: close\r\n" + "\r\n") .getBytes("utf-8")); os.write(contentB); os.flush(); // Get the server side endpoint EndPoint endPoint = exchanger.exchange(null, 10, TimeUnit.SECONDS); // read the response IO.toString(is); // check client reads EOF assertEquals(-1, is.read()); TimeUnit.MILLISECONDS.sleep(3 * MAX_IDLE_TIME); // further writes will get broken pipe or similar try { for (int i = 0; i < 1000; i++) { os.write( ("GET / HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: keep-alive\r\n" + "\r\n") .getBytes("utf-8")); os.flush(); } Assert.fail("half close should have timed out"); } catch (SocketException e) { // expected } // check the server side is closed Assert.assertFalse(endPoint.isOpen()); }
@Test public void testMaxIdleWithRequest10ClientIgnoresClose() throws Exception { final Exchanger<EndPoint> exchanger = new Exchanger<>(); configureServer( new HelloWorldHandler() { @Override public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { try { exchanger.exchange(baseRequest.getHttpChannel().getEndPoint()); } catch (Exception e) { e.printStackTrace(); } super.handle(target, baseRequest, request, response); } }); Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort()); client.setSoTimeout(10000); assertFalse(client.isClosed()); OutputStream os = client.getOutputStream(); InputStream is = client.getInputStream(); os.write( ("GET / HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: close\r\n" + "\r\n") .getBytes("utf-8")); os.flush(); // Get the server side endpoint EndPoint endPoint = exchanger.exchange(null, 10, TimeUnit.SECONDS); if (endPoint instanceof SslConnection.DecryptedEndPoint) endPoint = endPoint.getConnection().getEndPoint(); // read the response String result = IO.toString(is); Assert.assertThat("OK", result, containsString("200 OK")); // check client reads EOF assertEquals(-1, is.read()); assertTrue(endPoint.isOutputShutdown()); Thread.sleep(2 * MAX_IDLE_TIME); // further writes will get broken pipe or similar try { long end = System.currentTimeMillis() + MAX_IDLE_TIME + 3000; while (System.currentTimeMillis() < end) { os.write("THIS DATA SHOULD NOT BE PARSED!\n\n".getBytes("utf-8")); os.flush(); Thread.sleep(100); } Assert.fail("half close should have timed out"); } catch (SocketException e) { // expected // Give the SSL onClose time to act Thread.sleep(100); } // check the server side is closed Assert.assertFalse(endPoint.isOpen()); }
public boolean isDisconnected() { return !endPoint.isOpen(); }