Search in sources :

Example 6 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 7 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 8 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 9 with PriorityFrame

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

the class PriorityTest method testPriorityAfterHeaders.

@Test
public void testPriorityAfterHeaders() throws Exception {
    CountDownLatch beforeRequests = new CountDownLatch(1);
    CountDownLatch afterRequests = new CountDownLatch(2);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            try {
                beforeRequests.await(5, TimeUnit.SECONDS);
                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);
                afterRequests.countDown();
                return null;
            } catch (InterruptedException x) {
                x.printStackTrace();
                return null;
            }
        }
    });
    CountDownLatch responses = new CountDownLatch(2);
    Stream.Listener.Adapter listener = new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                responses.countDown();
        }
    };
    Session session = newClient(new Session.Listener.Adapter());
    MetaData metaData1 = newRequest("GET", "/one", new HttpFields());
    HeadersFrame headersFrame1 = new HeadersFrame(metaData1, null, true);
    FuturePromise<Stream> promise1 = new FuturePromise<>();
    session.newStream(headersFrame1, promise1, listener);
    Stream stream1 = promise1.get(5, TimeUnit.SECONDS);
    MetaData metaData2 = newRequest("GET", "/two", new HttpFields());
    HeadersFrame headersFrame2 = new HeadersFrame(metaData2, null, true);
    FuturePromise<Stream> promise2 = new FuturePromise<>();
    session.newStream(headersFrame2, promise2, listener);
    Stream stream2 = promise2.get(5, TimeUnit.SECONDS);
    int streamId = session.priority(new PriorityFrame(stream1.getId(), stream2.getId(), 13, false), Callback.NOOP);
    Assert.assertEquals(stream1.getId(), streamId);
    // Give time to the PRIORITY frame to arrive to server.
    Thread.sleep(1000);
    beforeRequests.countDown();
    Assert.assertTrue(afterRequests.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(responses.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) 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 10 with PriorityFrame

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

the class HTTP2Session method newStream.

@Override
public void newStream(HeadersFrame frame, Promise<Stream> promise, Stream.Listener listener) {
    // Synchronization is necessary to atomically create
    // the stream id and enqueue the frame to be sent.
    boolean queued;
    synchronized (this) {
        int streamId = frame.getStreamId();
        if (streamId <= 0) {
            streamId = streamIds.getAndAdd(2);
            PriorityFrame priority = frame.getPriority();
            priority = priority == null ? null : new PriorityFrame(streamId, priority.getParentStreamId(), priority.getWeight(), priority.isExclusive());
            frame = new HeadersFrame(streamId, frame.getMetaData(), priority, frame.isEndStream());
        }
        final IStream stream = createLocalStream(streamId, promise);
        if (stream == null)
            return;
        stream.setListener(listener);
        ControlEntry entry = new ControlEntry(frame, stream, new PromiseCallback<>(promise, stream));
        queued = flusher.append(entry);
    }
    // Iterate outside the synchronized block.
    if (queued)
        flusher.iterate();
}
Also used : PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) EndPoint(org.eclipse.jetty.io.EndPoint)

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