Search in sources :

Example 41 with EndPoint

use of org.eclipse.jetty.io.EndPoint in project jetty.project by eclipse.

the class ConnectorTimeoutTest method testMaxIdleWithRequest10ClientIgnoresClose.

@Test(timeout = 60000)
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);
    Assert.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, Matchers.containsString("200 OK"));
    // check client reads EOF
    Assert.assertEquals(-1, is.read());
    Assert.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());
}
Also used : SocketException(java.net.SocketException) Exchanger(java.util.concurrent.Exchanger) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) EndPoint(org.eclipse.jetty.io.EndPoint) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) SocketException(java.net.SocketException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Socket(java.net.Socket) Test(org.junit.Test)

Example 42 with EndPoint

use of org.eclipse.jetty.io.EndPoint in project zm-mailbox by Zimbra.

the class JettyUtil method setIdleTimeout.

public static void setIdleTimeout(long timeout, HttpServletRequest request) {
    if (request != null) {
        Object attr = request.getAttribute("org.eclipse.jetty.server.HttpConnection");
        if (attr instanceof HttpConnection) {
            @SuppressWarnings("resource") HttpConnection conn = (HttpConnection) attr;
            EndPoint ep = conn.getEndPoint();
            if (ep != null) {
                ep.setIdleTimeout(timeout);
            } else {
                ZimbraLog.misc.warn("null endpoint setting Jetty timeout?", new Exception());
            }
        } else {
            //this won't work for SPDY connections, so we'll have to consider this further once we enable it.
            ZimbraLog.misc.warn("got [%s] not instanceof org.eclipse.jetty.server.HttpConnection", attr, new Exception());
        }
    } else {
        ZimbraLog.misc.warn("cannot set timeout for null request", new Exception());
    }
}
Also used : HttpConnection(org.eclipse.jetty.server.HttpConnection) EndPoint(org.eclipse.jetty.io.EndPoint)

Aggregations

EndPoint (org.eclipse.jetty.io.EndPoint)42 Test (org.junit.Test)19 IOException (java.io.IOException)11 ByteBuffer (java.nio.ByteBuffer)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 SSLEngine (javax.net.ssl.SSLEngine)8 ServletException (javax.servlet.ServletException)8 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)8 InputStream (java.io.InputStream)7 Socket (java.net.Socket)7 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 OutputStream (java.io.OutputStream)6 SslConnection (org.eclipse.jetty.io.ssl.SslConnection)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 HttpClientTransportOverHTTP (org.eclipse.jetty.client.http.HttpClientTransportOverHTTP)5 Connector (org.eclipse.jetty.server.Connector)5 ServerConnector (org.eclipse.jetty.server.ServerConnector)5