Search in sources :

Example 66 with Stream

use of org.eclipse.jetty.http2.api.Stream in project jetty.project by eclipse.

the class HTTP2ServerSession method onHeaders.

@Override
public void onHeaders(HeadersFrame frame) {
    if (LOG.isDebugEnabled())
        LOG.debug("Received {}", frame);
    MetaData metaData = frame.getMetaData();
    if (metaData.isRequest()) {
        IStream stream = createRemoteStream(frame.getStreamId());
        if (stream != null) {
            onStreamOpened(stream);
            stream.process(frame, Callback.NOOP);
            Stream.Listener listener = notifyNewStream(stream, frame);
            stream.setListener(listener);
        }
    } else if (metaData.isResponse()) {
        onConnectionFailure(ErrorCode.PROTOCOL_ERROR.code, "invalid_request");
    } else {
        // Trailers.
        int streamId = frame.getStreamId();
        IStream stream = getStream(streamId);
        if (stream != null) {
            stream.process(frame, Callback.NOOP);
            notifyHeaders(stream, frame);
        } else {
            if (LOG.isDebugEnabled())
                LOG.debug("Ignoring {}, stream #{} not found", frame, streamId);
        }
    }
}
Also used : MetaData(org.eclipse.jetty.http.MetaData) IStream(org.eclipse.jetty.http2.IStream) Stream(org.eclipse.jetty.http2.api.Stream) IStream(org.eclipse.jetty.http2.IStream)

Example 67 with Stream

use of org.eclipse.jetty.http2.api.Stream in project jetty.project by eclipse.

the class StreamCloseTest method testPushedStreamResetIsClosed.

@Test
public void testPushedStreamResetIsClosed() throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(final Stream stream, HeadersFrame frame) {
            PushPromiseFrame pushFrame = new PushPromiseFrame(stream.getId(), 0, newRequest("GET", new HttpFields()));
            stream.push(pushFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream pushedStream, ResetFrame frame) {
                    Assert.assertTrue(pushedStream.isReset());
                    Assert.assertTrue(pushedStream.isClosed());
                    HeadersFrame response = new HeadersFrame(stream.getId(), new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields()), null, true);
                    stream.headers(response, Callback.NOOP);
                    serverLatch.countDown();
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, true);
    final CountDownLatch clientLatch = new CountDownLatch(2);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public Stream.Listener onPush(final Stream pushedStream, PushPromiseFrame frame) {
            pushedStream.reset(new ResetFrame(pushedStream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), new Callback() {

                @Override
                public void succeeded() {
                    Assert.assertTrue(pushedStream.isReset());
                    Assert.assertTrue(pushedStream.isClosed());
                    clientLatch.countDown();
                }
            });
            return null;
        }

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

Example 68 with Stream

use of org.eclipse.jetty.http2.api.Stream in project jetty.project by eclipse.

the class StreamCloseTest method testRequestDataClosedResponseDataClosedClosesStream.

@Test
public void testRequestDataClosedResponseDataClosedClosesStream() throws Exception {
    final CountDownLatch serverDataLatch = 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, 200, new HttpFields());
            HeadersFrame response = new HeadersFrame(stream.getId(), metaData, null, false);
            Callback.Completable completable = new Callback.Completable();
            stream.headers(response, completable);
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(final Stream stream, DataFrame frame, final Callback callback) {
                    Assert.assertTrue(((HTTP2Stream) stream).isRemotelyClosed());
                    // We must copy the data that we send asynchronously.
                    ByteBuffer data = frame.getData();
                    ByteBuffer copy = ByteBuffer.allocate(data.remaining());
                    copy.put(data).flip();
                    completable.thenRun(() -> stream.data(new DataFrame(stream.getId(), copy, frame.isEndStream()), new Callback() {

                        @Override
                        public void succeeded() {
                            Assert.assertTrue(stream.isClosed());
                            Assert.assertEquals(0, stream.getSession().getStreams().size());
                            callback.succeeded();
                            serverDataLatch.countDown();
                        }
                    }));
                }
            };
        }
    });
    final CountDownLatch completeLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            // The sent data callback may not be notified yet here.
            callback.succeeded();
            completeLatch.countDown();
        }
    });
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertFalse(stream.isClosed());
    Assert.assertFalse(((HTTP2Stream) stream).isLocallyClosed());
    final CountDownLatch clientDataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.wrap(new byte[512]), true), new Callback() {

        @Override
        public void succeeded() {
            // Here the stream may be just locally closed or fully closed.
            clientDataLatch.countDown();
        }
    });
    Assert.assertTrue(clientDataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(serverDataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(stream.isClosed());
    Assert.assertEquals(0, stream.getSession().getStreams().size());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 69 with Stream

use of org.eclipse.jetty.http2.api.Stream in project jetty.project by eclipse.

the class StreamCloseTest method testFailedSessionClosesIdleStream.

@Test
public void testFailedSessionClosesIdleStream() throws Exception {
    AtomicReference<Session> sessionRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Stream> streams = new ArrayList<>();
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            streams.add(stream);
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            if ("GET".equals(request.getMethod())) {
                ((HTTP2Session) stream.getSession()).getEndPoint().close();
                // Try to write something to force an error.
                stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1024), true), Callback.NOOP);
            }
            return null;
        }

        @Override
        public void onFailure(Session session, Throwable failure) {
            sessionRef.set(session);
            latch.countDown();
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    // First stream will be idle on server.
    HeadersFrame request1 = new HeadersFrame(newRequest("HEAD", new HttpFields()), null, true);
    session.newStream(request1, new Promise.Adapter<>(), new Stream.Listener.Adapter());
    // Second stream will fail on server.
    HeadersFrame request2 = new HeadersFrame(newRequest("GET", new HttpFields()), null, true);
    session.newStream(request2, new Promise.Adapter<>(), new Stream.Listener.Adapter());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Session serverSession = sessionRef.get();
    // Wait for the server to finish the close activities.
    Thread.sleep(1000);
    Assert.assertEquals(0, serverSession.getStreams().size());
    for (Stream stream : streams) Assert.assertTrue(stream.isClosed());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) 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) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 70 with Stream

use of org.eclipse.jetty.http2.api.Stream in project jetty.project by eclipse.

the class StreamCountTest method testServerAllowsOneStreamEnforcedByClient.

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

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 1);
            return settings;
        }

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

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    if (frame.isEndStream()) {
                        HttpFields fields = new HttpFields();
                        MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, fields);
                        stream.headers(new HeadersFrame(stream.getId(), metaData, null, true), callback);
                    } else {
                        callback.succeeded();
                    }
                }
            };
        }
    });
    final CountDownLatch settingsLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onSettings(Session session, SettingsFrame frame) {
            settingsLatch.countDown();
        }
    });
    Assert.assertTrue(settingsLatch.await(5, TimeUnit.SECONDS));
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame1 = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> streamPromise1 = new FuturePromise<>();
    final CountDownLatch responseLatch = new CountDownLatch(1);
    session.newStream(frame1, streamPromise1, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                responseLatch.countDown();
        }
    });
    Stream stream1 = streamPromise1.get(5, TimeUnit.SECONDS);
    HeadersFrame frame2 = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> streamPromise2 = new FuturePromise<>();
    session.newStream(frame2, streamPromise2, new Stream.Listener.Adapter());
    try {
        streamPromise2.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected
    }
    stream1.data(new DataFrame(stream1.getId(), BufferUtil.EMPTY_BUFFER, true), Callback.NOOP);
    Assert.assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) 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) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

Stream (org.eclipse.jetty.http2.api.Stream)105 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)97 HttpFields (org.eclipse.jetty.http.HttpFields)95 MetaData (org.eclipse.jetty.http.MetaData)95 CountDownLatch (java.util.concurrent.CountDownLatch)93 Test (org.junit.Test)91 Session (org.eclipse.jetty.http2.api.Session)89 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)77 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)54 Promise (org.eclipse.jetty.util.Promise)50 HttpServletResponse (javax.servlet.http.HttpServletResponse)49 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 IOException (java.io.IOException)36 HttpServletRequest (javax.servlet.http.HttpServletRequest)36 ServletException (javax.servlet.ServletException)33 HttpServlet (javax.servlet.http.HttpServlet)29 ByteBuffer (java.nio.ByteBuffer)26 ISession (org.eclipse.jetty.http2.ISession)24