Search in sources :

Example 6 with EndPoint

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

the class HttpClientFailureTest method testFailureBeforeRequestCommit.

@Test
public void testFailureBeforeRequestCommit() throws Exception {
    startServer(new EmptyServerHandler());
    final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
            connectionRef.set(connection);
            return connection;
        }
    }, null);
    client.start();
    try {
        client.newRequest("localhost", connector.getLocalPort()).onRequestHeaders(request -> connectionRef.get().getEndPoint().close()).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected.
    }
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Promise(org.eclipse.jetty.util.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) ExecutionException(java.util.concurrent.ExecutionException) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Test(org.junit.Test)

Example 7 with EndPoint

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

the class PrefaceTest method testOnPrefaceNotifiedForStandardUpgrade.

@Test
public void testOnPrefaceNotifiedForStandardUpgrade() throws Exception {
    Integer maxConcurrentStreams = 128;
    AtomicReference<CountDownLatch> serverPrefaceLatch = new AtomicReference<>(new CountDownLatch(1));
    AtomicReference<CountDownLatch> serverSettingsLatch = new AtomicReference<>(new CountDownLatch(1));
    HttpConfiguration config = new HttpConfiguration();
    prepareServer(new HttpConnectionFactory(config), new HTTP2CServerConnectionFactory(config) {

        @Override
        protected ServerSessionListener newSessionListener(Connector connector, EndPoint endPoint) {
            return new ServerSessionListener.Adapter() {

                @Override
                public Map<Integer, Integer> onPreface(Session session) {
                    Map<Integer, Integer> serverSettings = new HashMap<>();
                    serverSettings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, maxConcurrentStreams);
                    serverPrefaceLatch.get().countDown();
                    return serverSettings;
                }

                @Override
                public void onSettings(Session session, SettingsFrame frame) {
                    serverSettingsLatch.get().countDown();
                }

                @Override
                public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
                    MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                    return null;
                }
            };
        }
    });
    server.start();
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    try (SocketChannel socket = SocketChannel.open()) {
        socket.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
        String upgradeRequest = "" + "GET /one HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: Upgrade, HTTP2-Settings\r\n" + "Upgrade: h2c\r\n" + "HTTP2-Settings: \r\n" + "\r\n";
        ByteBuffer upgradeBuffer = ByteBuffer.wrap(upgradeRequest.getBytes(StandardCharsets.ISO_8859_1));
        socket.write(upgradeBuffer);
        // Make sure onPreface() is called on server.
        Assert.assertTrue(serverPrefaceLatch.get().await(5, TimeUnit.SECONDS));
        Assert.assertTrue(serverSettingsLatch.get().await(5, TimeUnit.SECONDS));
        // The 101 response is the reply to the client preface SETTINGS frame.
        ByteBuffer buffer = byteBufferPool.acquire(1024, true);
        http1: while (true) {
            BufferUtil.clearToFill(buffer);
            int read = socket.read(buffer);
            BufferUtil.flipToFlush(buffer, 0);
            if (read < 0)
                Assert.fail();
            int crlfs = 0;
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                if (b == '\r' || b == '\n')
                    ++crlfs;
                else
                    crlfs = 0;
                if (crlfs == 4)
                    break http1;
            }
        }
        // Reset the latches on server.
        serverPrefaceLatch.set(new CountDownLatch(1));
        serverSettingsLatch.set(new CountDownLatch(1));
        // After the 101, the client must send the connection preface.
        Generator generator = new Generator(byteBufferPool);
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        Map<Integer, Integer> clientSettings = new HashMap<>();
        clientSettings.put(SettingsFrame.ENABLE_PUSH, 1);
        generator.control(lease, new SettingsFrame(clientSettings, false));
        List<ByteBuffer> buffers = lease.getByteBuffers();
        socket.write(buffers.toArray(new ByteBuffer[buffers.size()]));
        // However, we should not call onPreface() again.
        Assert.assertFalse(serverPrefaceLatch.get().await(1, TimeUnit.SECONDS));
        // Although we should notify of the SETTINGS frame.
        Assert.assertTrue(serverSettingsLatch.get().await(5, TimeUnit.SECONDS));
        CountDownLatch clientSettingsLatch = new CountDownLatch(1);
        AtomicBoolean responded = new AtomicBoolean();
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onSettings(SettingsFrame frame) {
                if (frame.isReply())
                    return;
                Assert.assertEquals(maxConcurrentStreams, frame.getSettings().get(SettingsFrame.MAX_CONCURRENT_STREAMS));
                clientSettingsLatch.countDown();
            }

            @Override
            public void onHeaders(HeadersFrame frame) {
                if (frame.isEndStream())
                    responded.set(true);
            }
        }, 4096, 8192);
        // HTTP/2 parsing.
        while (true) {
            parser.parse(buffer);
            if (responded.get())
                break;
            BufferUtil.clearToFill(buffer);
            int read = socket.read(buffer);
            BufferUtil.flipToFlush(buffer, 0);
            if (read < 0)
                Assert.fail();
        }
        Assert.assertTrue(clientSettingsLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) Connector(org.eclipse.jetty.server.Connector) SocketChannel(java.nio.channels.SocketChannel) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) EndPoint(org.eclipse.jetty.io.EndPoint) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HTTP2CServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) Parser(org.eclipse.jetty.http2.parser.Parser) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Map(java.util.Map) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 8 with EndPoint

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

the class AsyncServletTest method testStartAsyncThenServerIdleTimeout.

private void testStartAsyncThenServerIdleTimeout(long sessionTimeout, long streamTimeout) throws Exception {
    prepareServer(new HTTP2ServerConnectionFactory(new HttpConfiguration()) {

        @Override
        protected ServerSessionListener newSessionListener(Connector connector, EndPoint endPoint) {
            return new HTTPServerSessionListener(connector, endPoint) {

                @Override
                public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
                    stream.setIdleTimeout(streamTimeout);
                    return super.onNewStream(stream, frame);
                }
            };
        }
    });
    connector.setIdleTimeout(sessionTimeout);
    ServletContextHandler context = new ServletContextHandler(server, "/");
    long timeout = Math.min(sessionTimeout, streamTimeout);
    CountDownLatch errorLatch = new CountDownLatch(1);
    context.addServlet(new ServletHolder(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = (AsyncContext) request.getAttribute(AsyncContext.class.getName());
            if (asyncContext == null) {
                AsyncContext context = request.startAsync();
                context.setTimeout(2 * timeout);
                request.setAttribute(AsyncContext.class.getName(), context);
                context.addListener(new AsyncListener() {

                    @Override
                    public void onComplete(AsyncEvent event) throws IOException {
                    }

                    @Override
                    public void onTimeout(AsyncEvent event) throws IOException {
                        event.getAsyncContext().complete();
                    }

                    @Override
                    public void onError(AsyncEvent event) throws IOException {
                        errorLatch.countDown();
                    }

                    @Override
                    public void onStartAsync(AsyncEvent event) throws IOException {
                    }
                });
            } else {
                throw new ServletException();
            }
        }
    }), servletPath + "/*");
    server.start();
    prepareClient();
    client.start();
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    CountDownLatch clientLatch = new CountDownLatch(1);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            if (response.getStatus() == HttpStatus.OK_200 && frame.isEndStream())
                clientLatch.countDown();
        }
    });
    // When the server idle times out, but the request has been dispatched
    // then the server must ignore the idle timeout as per Servlet semantic.
    Assert.assertFalse(errorLatch.await(2 * timeout, TimeUnit.MILLISECONDS));
    Assert.assertTrue(clientLatch.await(2 * timeout, TimeUnit.MILLISECONDS));
}
Also used : Connector(org.eclipse.jetty.server.Connector) AsyncListener(javax.servlet.AsyncListener) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) AsyncContext(javax.servlet.AsyncContext) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) EndPoint(org.eclipse.jetty.io.EndPoint) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Stream(org.eclipse.jetty.http2.api.Stream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) HTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncEvent(javax.servlet.AsyncEvent) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) AsyncListener(javax.servlet.AsyncListener) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session)

Example 9 with EndPoint

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

the class ConnectorTimeoutTest method testMaxIdleWithRequest10NoClientClose.

@Test(timeout = 60000)
public void testMaxIdleWithRequest10NoClientClose() 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());
    // wait for idle timeout
    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());
}
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) EndPoint(org.eclipse.jetty.io.EndPoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Socket(java.net.Socket) Test(org.junit.Test)

Example 10 with EndPoint

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

the class ClientCloseTest method testReadEOF.

@Test
public void testReadEOF() throws Exception {
    // Set client timeout
    final int timeout = 1000;
    client.setMaxIdleTimeout(timeout);
    // Client connects
    CloseTrackingSocket clientSocket = new CloseTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    serverConn.upgrade();
    // client confirms connection via echo
    confirmConnection(clientSocket, clientConnectFuture, serverConn);
    // client sends close frame
    final String origCloseReason = "Normal Close";
    clientSocket.getSession().close(StatusCode.NORMAL, origCloseReason);
    // server receives close frame
    confirmServerReceivedCloseFrame(serverConn, StatusCode.NORMAL, is(origCloseReason));
    // client should not have received close message (yet)
    clientSocket.assertNoCloseEvent();
    // server shuts down connection (no frame reply)
    serverConn.disconnect();
    // client reads -1 (EOF)
    // client triggers close event on client ws-endpoint
    clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.ABNORMAL), containsString("EOF"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

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