Search in sources :

Example 6 with HeadersFrame

use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.

the class HTTP2ServerTest method testRequestWithContinuationFramesWithEmptyHeadersFrame.

@Test
public void testRequestWithContinuationFramesWithEmptyHeadersFrame() throws Exception {
    testRequestWithContinuationFrames(null, () -> {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.control(lease, new PrefaceFrame());
        generator.control(lease, new SettingsFrame(new HashMap<>(), false));
        MetaData.Request metaData = newRequest("GET", new HttpFields());
        generator.control(lease, new HeadersFrame(1, metaData, null, true));
        // Take the HeadersFrame header and set the length to zero.
        List<ByteBuffer> buffers = lease.getByteBuffers();
        ByteBuffer headersFrameHeader = buffers.get(2);
        headersFrameHeader.put(0, (byte) 0);
        headersFrameHeader.putShort(1, (short) 0);
        // Insert a CONTINUATION frame header for the body of the HEADERS frame.
        lease.insert(3, buffers.get(4).slice(), false);
        return lease;
    });
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 7 with HeadersFrame

use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.

the class HTTP2ServerTest method testCommitFailure.

@Test
public void testCommitFailure() throws Exception {
    final long delay = 1000;
    final AtomicBoolean broken = new AtomicBoolean();
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try {
                // Wait for the SETTINGS frames to be exchanged.
                Thread.sleep(delay);
                broken.set(true);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    server.stop();
    ServerConnector connector2 = new ServerConnector(server, new HTTP2ServerConnectionFactory(new HttpConfiguration())) {

        @Override
        protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
            return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {

                @Override
                public void write(Callback callback, ByteBuffer... buffers) throws IllegalStateException {
                    if (broken.get())
                        callback.failed(new IOException("explicitly_thrown_by_test"));
                    else
                        super.write(callback, buffers);
                }
            };
        }
    };
    server.addConnector(connector2);
    server.start();
    ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
    generator.control(lease, new PrefaceFrame());
    generator.control(lease, new SettingsFrame(new HashMap<>(), false));
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    generator.control(lease, new HeadersFrame(1, metaData, null, true));
    try (Socket client = new Socket("localhost", connector2.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        // The server will close the connection abruptly since it
        // cannot write and therefore cannot even send the GO_AWAY.
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter(), 4096, 8192);
        boolean closed = parseResponse(client, parser, 2 * delay);
        Assert.assertTrue(closed);
    }
}
Also used : ManagedSelector(org.eclipse.jetty.io.ManagedSelector) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) InterruptedIOException(java.io.InterruptedIOException) SocketChannel(java.nio.channels.SocketChannel) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServerConnector(org.eclipse.jetty.server.ServerConnector) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) SelectionKey(java.nio.channels.SelectionKey) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) PrefaceFrame(org.eclipse.jetty.http2.frames.PrefaceFrame) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(org.eclipse.jetty.util.Callback) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Socket(java.net.Socket) Test(org.junit.Test)

Example 8 with HeadersFrame

use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testRequestIdleTimeoutSendsResetFrame.

@Test
public void testRequestIdleTimeoutSendsResetFrame() throws Exception {
    CountDownLatch resetLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            return new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    resetLatch.countDown();
                }
            };
        }
    });
    try {
        long idleTimeout = 1000;
        client.newRequest("localhost", connector.getLocalPort()).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).send();
        Assert.fail();
    } catch (ExecutionException e) {
    // Expected.
    }
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 9 with HeadersFrame

use of org.eclipse.jetty.http2.frames.HeadersFrame 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 10 with HeadersFrame

use of org.eclipse.jetty.http2.frames.HeadersFrame in project jetty.project by eclipse.

the class HttpClientTransportOverHTTP2Test method testResponseAbortSendsResetFrame.

@Test
public void testResponseAbortSendsResetFrame() throws Exception {
    CountDownLatch resetLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), metaData, null, false), new Callback() {

                @Override
                public void succeeded() {
                    ByteBuffer data = ByteBuffer.allocate(1024);
                    stream.data(new DataFrame(stream.getId(), data, false), NOOP);
                }
            });
            return new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    resetLatch.countDown();
                }
            };
        }
    });
    try {
        client.newRequest("localhost", connector.getLocalPort()).onResponseContent((response, buffer) -> response.abort(new Exception("explicitly_aborted_by_test"))).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) ServerSocket(java.net.ServerSocket) HttpClient(org.eclipse.jetty.client.HttpClient) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetaData(org.eclipse.jetty.http.MetaData) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) HttpStatus(org.eclipse.jetty.http.HttpStatus) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Callback(org.eclipse.jetty.util.Callback) HttpDestination(org.eclipse.jetty.client.HttpDestination) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Session(org.eclipse.jetty.http2.api.Session) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) BufferUtil(org.eclipse.jetty.util.BufferUtil) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) ErrorCode(org.eclipse.jetty.http2.ErrorCode) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SocketTimeoutException(java.net.SocketTimeoutException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpFields(org.eclipse.jetty.http.HttpFields) OutputStream(java.io.OutputStream) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Executor(java.util.concurrent.Executor) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Generator(org.eclipse.jetty.http2.generator.Generator) HttpMethod(org.eclipse.jetty.http.HttpMethod) Ignore(org.junit.Ignore) Assert(org.junit.Assert) InputStream(java.io.InputStream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ExecutionException(java.util.concurrent.ExecutionException) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Aggregations

HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)114 HttpFields (org.eclipse.jetty.http.HttpFields)111 MetaData (org.eclipse.jetty.http.MetaData)111 Test (org.junit.Test)106 CountDownLatch (java.util.concurrent.CountDownLatch)98 Stream (org.eclipse.jetty.http2.api.Stream)98 Session (org.eclipse.jetty.http2.api.Session)90 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)82 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)57 Callback (org.eclipse.jetty.util.Callback)54 HttpServletResponse (javax.servlet.http.HttpServletResponse)53 Promise (org.eclipse.jetty.util.Promise)50 ByteBuffer (java.nio.ByteBuffer)40 HttpServletRequest (javax.servlet.http.HttpServletRequest)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)38 IOException (java.io.IOException)37 ServletException (javax.servlet.ServletException)36 HashMap (java.util.HashMap)33 HttpServlet (javax.servlet.http.HttpServlet)33