Search in sources :

Example 1 with PriorityFrame

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

the class HTTP2ServerTest method testRequestWithContinuationFrames.

private void testRequestWithContinuationFrames(PriorityFrame priorityFrame, Callable<ByteBufferPool.Lease> frames) throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    startServer(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            if (priorityFrame != null) {
                PriorityFrame priority = frame.getPriority();
                Assert.assertNotNull(priority);
                Assert.assertEquals(priorityFrame.getStreamId(), priority.getStreamId());
                Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
                Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
                Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
            }
            serverLatch.countDown();
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return null;
        }
    });
    generator = new Generator(byteBufferPool, 4096, 4);
    ByteBufferPool.Lease lease = frames.call();
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        OutputStream output = client.getOutputStream();
        for (ByteBuffer buffer : lease.getByteBuffers()) output.write(BufferUtil.toArray(buffer));
        output.flush();
        Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
        final CountDownLatch clientLatch = new CountDownLatch(1);
        Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

            @Override
            public void onHeaders(HeadersFrame frame) {
                if (frame.isEndStream())
                    clientLatch.countDown();
            }
        }, 4096, 8192);
        boolean closed = parseResponse(client, parser);
        Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
        Assert.assertFalse(closed);
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HttpServletResponse(javax.servlet.http.HttpServletResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) Generator(org.eclipse.jetty.http2.generator.Generator)

Example 2 with PriorityFrame

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

the class HTTP2ServerTest method testRequestWithPriorityWithContinuationFrames.

@Test
public void testRequestWithPriorityWithContinuationFrames() throws Exception {
    PriorityFrame priority = new PriorityFrame(1, 13, 200, true);
    testRequestWithContinuationFrames(priority, () -> {
        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, priority, true));
        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) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Test(org.junit.Test)

Example 3 with PriorityFrame

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

the class PriorityTest method testHeadersWithPriority.

@Test
public void testHeadersWithPriority() throws Exception {
    PriorityFrame priorityFrame = new PriorityFrame(13, 200, true);
    CountDownLatch latch = new CountDownLatch(2);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            PriorityFrame priority = frame.getPriority();
            Assert.assertNotNull(priority);
            Assert.assertEquals(priorityFrame.getParentStreamId(), priority.getParentStreamId());
            Assert.assertEquals(priorityFrame.getWeight(), priority.getWeight());
            Assert.assertEquals(priorityFrame.isExclusive(), priority.isExclusive());
            latch.countDown();
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData metaData = newRequest("GET", "/one", new HttpFields());
    HeadersFrame headersFrame = new HeadersFrame(metaData, priorityFrame, true);
    session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 4 with PriorityFrame

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

the class PriorityTest method testPriorityBeforeHeaders.

@Test
public void testPriorityBeforeHeaders() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
            stream.headers(responseFrame, Callback.NOOP);
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    int streamId = session.priority(new PriorityFrame(0, 13, false), Callback.NOOP);
    Assert.assertTrue(streamId > 0);
    CountDownLatch latch = new CountDownLatch(2);
    MetaData metaData = newRequest("GET", new HttpFields());
    HeadersFrame headersFrame = new HeadersFrame(streamId, metaData, null, true);
    session.newStream(headersFrame, new Promise.Adapter<Stream>() {

        @Override
        public void succeeded(Stream result) {
            Assert.assertEquals(streamId, result.getId());
            latch.countDown();
        }
    }, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 5 with PriorityFrame

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

the class PriorityGenerateParseTest method testGenerateParseOneByteAtATime.

@Test
public void testGenerateParseOneByteAtATime() throws Exception {
    PriorityGenerator generator = new PriorityGenerator(new HeaderGenerator());
    final List<PriorityFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onPriority(PriorityFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int parentStreamId = 17;
    int weight = 3;
    boolean exclusive = true;
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generatePriority(lease, streamId, parentStreamId, weight, exclusive);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        PriorityFrame frame = frames.get(0);
        Assert.assertEquals(streamId, frame.getStreamId());
        Assert.assertEquals(parentStreamId, frame.getParentStreamId());
        Assert.assertEquals(weight, frame.getWeight());
        Assert.assertEquals(exclusive, frame.isExclusive());
    }
}
Also used : MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) PriorityGenerator(org.eclipse.jetty.http2.generator.PriorityGenerator) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) Test(org.junit.Test)

Aggregations

PriorityFrame (org.eclipse.jetty.http2.frames.PriorityFrame)10 Test (org.junit.Test)10 HttpFields (org.eclipse.jetty.http.HttpFields)9 MetaData (org.eclipse.jetty.http.MetaData)9 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)8 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)8 ByteBuffer (java.nio.ByteBuffer)7 Parser (org.eclipse.jetty.http2.parser.Parser)6 ArrayList (java.util.ArrayList)5 HeaderGenerator (org.eclipse.jetty.http2.generator.HeaderGenerator)5 MappedByteBufferPool (org.eclipse.jetty.io.MappedByteBufferPool)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Stream (org.eclipse.jetty.http2.api.Stream)4 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)4 HostPortHttpField (org.eclipse.jetty.http.HostPortHttpField)3 HttpField (org.eclipse.jetty.http.HttpField)3 Session (org.eclipse.jetty.http2.api.Session)3 HeadersGenerator (org.eclipse.jetty.http2.generator.HeadersGenerator)3 HpackEncoder (org.eclipse.jetty.http2.hpack.HpackEncoder)3 FuturePromise (org.eclipse.jetty.util.FuturePromise)3