Search in sources :

Example 1 with ServerParser

use of org.eclipse.jetty.http2.parser.ServerParser in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testClientStopsServerDoesNotCloseClientCloses.

@Test
public void testClientStopsServerDoesNotCloseClientCloses() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        List<Session> sessions = new ArrayList<>();
        HTTP2Client h2Client = new HTTP2Client();
        HttpClient client = new HttpClient(new HttpClientTransportOverHTTP2(h2Client) {

            @Override
            protected HttpConnectionOverHTTP2 newHttpConnection(HttpDestination destination, Session session) {
                sessions.add(session);
                return super.newHttpConnection(destination, session);
            }
        }, null);
        QueuedThreadPool clientExecutor = new QueuedThreadPool();
        clientExecutor.setName("client");
        client.setExecutor(clientExecutor);
        client.start();
        CountDownLatch resultLatch = new CountDownLatch(1);
        client.newRequest("localhost", server.getLocalPort()).send(result -> {
            if (result.getResponse().getStatus() == HttpStatus.OK_200)
                resultLatch.countDown();
        });
        ByteBufferPool byteBufferPool = new MappedByteBufferPool();
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        Generator generator = new Generator(byteBufferPool);
        try (Socket socket = server.accept()) {
            socket.setSoTimeout(1000);
            OutputStream output = socket.getOutputStream();
            InputStream input = socket.getInputStream();
            ServerParser parser = new ServerParser(byteBufferPool, new ServerParser.Listener.Adapter() {

                @Override
                public void onHeaders(HeadersFrame request) {
                    // Server's preface.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
                    // Reply to client's SETTINGS.
                    generator.control(lease, new SettingsFrame(new HashMap<>(), true));
                    // Response.
                    MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    HeadersFrame response = new HeadersFrame(request.getStreamId(), metaData, null, true);
                    generator.control(lease, response);
                    try {
                        // Write the frames.
                        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
                    } catch (Throwable x) {
                        x.printStackTrace();
                    }
                }
            }, 4096, 8192);
            byte[] bytes = new byte[1024];
            while (true) {
                try {
                    int read = input.read(bytes);
                    if (read < 0)
                        Assert.fail();
                    parser.parse(ByteBuffer.wrap(bytes, 0, read));
                } catch (SocketTimeoutException x) {
                    break;
                }
            }
            Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
            // The client will send a GO_AWAY, but the server will not close.
            client.stop();
            // Give some time to process the stop/close operations.
            Thread.sleep(1000);
            Assert.assertTrue(h2Client.getBeans(Session.class).isEmpty());
            for (Session session : sessions) {
                Assert.assertTrue(session.isClosed());
                Assert.assertTrue(((HTTP2Session) session).isDisconnected());
            }
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) InputStream(java.io.InputStream) ServerSocket(java.net.ServerSocket) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) SocketTimeoutException(java.net.SocketTimeoutException) HttpClient(org.eclipse.jetty.client.HttpClient) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) HttpDestination(org.eclipse.jetty.client.HttpDestination) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Generator(org.eclipse.jetty.http2.generator.Generator) Test(org.junit.Test)

Example 2 with ServerParser

use of org.eclipse.jetty.http2.parser.ServerParser in project jetty.project by eclipse.

the class AbstractHTTP2ServerConnectionFactory method newConnection.

@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
    ServerSessionListener listener = newSessionListener(connector, endPoint);
    Generator generator = new Generator(connector.getByteBufferPool(), getMaxDynamicTableSize(), getMaxHeaderBlockFragment());
    FlowControlStrategy flowControl = getFlowControlStrategyFactory().newFlowControlStrategy();
    HTTP2ServerSession session = new HTTP2ServerSession(connector.getScheduler(), endPoint, generator, listener, flowControl);
    session.setMaxLocalStreams(getMaxConcurrentStreams());
    session.setMaxRemoteStreams(getMaxConcurrentStreams());
    // For a single stream in a connection, there will be a race between
    // the stream idle timeout and the connection idle timeout. However,
    // the typical case is that the connection will be busier and the
    // stream idle timeout will expire earlier than the connection's.
    long streamIdleTimeout = getStreamIdleTimeout();
    if (streamIdleTimeout <= 0)
        streamIdleTimeout = endPoint.getIdleTimeout();
    session.setStreamIdleTimeout(streamIdleTimeout);
    session.setInitialSessionRecvWindow(getInitialSessionRecvWindow());
    ServerParser parser = newServerParser(connector, session);
    HTTP2Connection connection = new HTTP2ServerConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, httpConfiguration, parser, session, getInputBufferSize(), listener);
    connection.addListener(connectionListener);
    return configure(connection, connector, endPoint);
}
Also used : FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) BufferingFlowControlStrategy(org.eclipse.jetty.http2.BufferingFlowControlStrategy) HTTP2Connection(org.eclipse.jetty.http2.HTTP2Connection) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Generator(org.eclipse.jetty.http2.generator.Generator)

Aggregations

ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)2 Generator (org.eclipse.jetty.http2.generator.Generator)2 ServerParser (org.eclipse.jetty.http2.parser.ServerParser)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 HttpDestination (org.eclipse.jetty.client.HttpDestination)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 HttpFields (org.eclipse.jetty.http.HttpFields)1 MetaData (org.eclipse.jetty.http.MetaData)1 BufferingFlowControlStrategy (org.eclipse.jetty.http2.BufferingFlowControlStrategy)1 FlowControlStrategy (org.eclipse.jetty.http2.FlowControlStrategy)1 HTTP2Connection (org.eclipse.jetty.http2.HTTP2Connection)1