Search in sources :

Example 56 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class Client method main.

public static void main(String[] args) throws Exception {
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory();
    client.addBean(sslContextFactory);
    client.start();
    String host = "webtide.com";
    int port = 443;
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new ServerSessionListener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(5, TimeUnit.SECONDS);
    HttpFields requestFields = new HttpFields();
    requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
    HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
    final Phaser phaser = new Phaser(2);
    session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            System.err.println(frame);
            if (frame.isEndStream())
                phaser.arrive();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            System.err.println(frame);
            callback.succeeded();
            if (frame.isEndStream())
                phaser.arrive();
        }

        @Override
        public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            System.err.println(frame);
            phaser.register();
            return this;
        }
    });
    phaser.awaitAdvanceInterruptibly(phaser.arrive(), 5, TimeUnit.SECONDS);
    client.stop();
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) Phaser(java.util.concurrent.Phaser) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HttpURI(org.eclipse.jetty.http.HttpURI) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session)

Example 57 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class StreamCloseTest method testPushedStreamIsClosed.

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

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

                @Override
                public void succeeded(final Stream pushedStream) {
                    // When created, pushed stream must be implicitly remotely closed.
                    Assert.assertTrue(((HTTP2Stream) pushedStream).isRemotelyClosed());
                    // Send some data with endStream = true.
                    pushedStream.data(new DataFrame(pushedStream.getId(), ByteBuffer.allocate(16), true), new Callback() {

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

        @Override
        public Stream.Listener onPush(Stream pushedStream, PushPromiseFrame frame) {
            Assert.assertTrue(((HTTP2Stream) pushedStream).isLocallyClosed());
            return new Adapter() {

                @Override
                public void onData(Stream pushedStream, DataFrame frame, Callback callback) {
                    Assert.assertTrue(pushedStream.isClosed());
                    callback.succeeded();
                    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) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) 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) 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 58 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class StreamCountTest method testServerAllowsOneStreamEnforcedByServer.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HTTP2Session session = (HTTP2Session) stream.getSession();
            session.setMaxRemoteStreams(1);
            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();
                    }
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    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() {

        @Override
        public void onReset(Stream stream, ResetFrame frame) {
            resetLatch.countDown();
        }
    });
    streamPromise2.get(5, TimeUnit.SECONDS);
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    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) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) 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) 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 59 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class StreamResetTest method testClientResetConsumesQueuedData.

@Test
public void testClientResetConsumesQueuedData() throws Exception {
    start(new EmptyHttpServlet());
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(request, null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    client.newStream(frame, promise, new Stream.Listener.Adapter());
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    ByteBuffer data = ByteBuffer.allocate(FlowControlStrategy.DEFAULT_WINDOW_SIZE);
    CountDownLatch dataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), data, false), new Callback() {

        @Override
        public void succeeded() {
            dataLatch.countDown();
        }
    });
    // The server does not read the data, so the flow control window should be zero.
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertEquals(0, ((ISession) client).updateSendWindow(0));
    // Now reset the stream.
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
    // Wait for the server to receive the reset and process
    // it, and for the client to process the window updates.
    Thread.sleep(1000);
    Assert.assertThat(((ISession) client).updateSendWindow(0), Matchers.greaterThan(0));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) 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) FutureCallback(org.eclipse.jetty.util.FutureCallback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 60 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class StreamResetTest method testStreamResetDoesNotCloseConnection.

@Test
public void testStreamResetDoesNotCloseConnection() throws Exception {
    final CountDownLatch serverResetLatch = new CountDownLatch(1);
    final CountDownLatch serverDataLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame requestFrame) {
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, false);
            Callback.Completable completable = new Callback.Completable();
            stream.headers(responseFrame, completable);
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    completable.thenRun(() -> stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(16), true), new Callback() {

                        @Override
                        public void succeeded() {
                            serverDataLatch.countDown();
                        }
                    }));
                }

                @Override
                public void onReset(Stream s, ResetFrame frame) {
                    // Simulate that there is pending data to send.
                    IStream stream = (IStream) s;
                    stream.getSession().frames(stream, new Callback() {

                        @Override
                        public void failed(Throwable x) {
                            serverResetLatch.countDown();
                        }
                    }, new DataFrame(s.getId(), ByteBuffer.allocate(16), true));
                }
            };
        }
    });
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request1 = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame1 = new HeadersFrame(request1, null, false);
    FuturePromise<Stream> promise1 = new FuturePromise<>();
    final CountDownLatch stream1HeadersLatch = new CountDownLatch(1);
    final CountDownLatch stream1DataLatch = new CountDownLatch(1);
    client.newStream(requestFrame1, promise1, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            stream1HeadersLatch.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            stream1DataLatch.countDown();
        }
    });
    Stream stream1 = promise1.get(5, TimeUnit.SECONDS);
    Assert.assertTrue(stream1HeadersLatch.await(5, TimeUnit.SECONDS));
    MetaData.Request request2 = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame2 = new HeadersFrame(request2, null, false);
    FuturePromise<Stream> promise2 = new FuturePromise<>();
    final CountDownLatch stream2DataLatch = new CountDownLatch(1);
    client.newStream(requestFrame2, promise2, new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            stream2DataLatch.countDown();
        }
    });
    Stream stream2 = promise2.get(5, TimeUnit.SECONDS);
    ResetFrame resetFrame = new ResetFrame(stream1.getId(), ErrorCode.CANCEL_STREAM_ERROR.code);
    stream1.reset(resetFrame, Callback.NOOP);
    Assert.assertTrue(serverResetLatch.await(5, TimeUnit.SECONDS));
    // Stream MUST NOT receive data sent by server after reset.
    Assert.assertFalse(stream1DataLatch.await(1, TimeUnit.SECONDS));
    // The other stream should still be working.
    stream2.data(new DataFrame(stream2.getId(), ByteBuffer.allocate(16), true), Callback.NOOP);
    Assert.assertTrue(serverDataLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(stream2DataLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) IStream(org.eclipse.jetty.http2.IStream) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)171 Test (org.junit.Test)142 MetaData (org.eclipse.jetty.http.MetaData)117 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)105 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)93 Session (org.eclipse.jetty.http2.api.Session)89 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)79 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 HttpServletResponse (javax.servlet.http.HttpServletResponse)59 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)53 ByteBuffer (java.nio.ByteBuffer)52 Promise (org.eclipse.jetty.util.Promise)49 HttpServletRequest (javax.servlet.http.HttpServletRequest)45 IOException (java.io.IOException)42 ServletException (javax.servlet.ServletException)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32