Search in sources :

Example 81 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class PushCacheFilterTest method testPushWithoutPrimaryResponseContent.

@Test
public void testPushWithoutPrimaryResponseContent() throws Exception {
    final String primaryResource = "/primary.html";
    final String secondaryResource = "/secondary.png";
    start(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String requestURI = request.getRequestURI();
            final ServletOutputStream output = response.getOutputStream();
            if (requestURI.endsWith(secondaryResource))
                output.write("SECONDARY".getBytes(StandardCharsets.UTF_8));
        }
    });
    final Session session = newClient(new Session.Listener.Adapter());
    // Request for the primary and secondary resource to build the cache.
    final String primaryURI = newURI(primaryResource);
    HttpFields primaryFields = new HttpFields();
    MetaData.Request primaryRequest = newRequest("GET", primaryResource, primaryFields);
    final CountDownLatch warmupLatch = new CountDownLatch(1);
    session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream()) {
                // Request for the secondary resource.
                HttpFields secondaryFields = new HttpFields();
                secondaryFields.put(HttpHeader.REFERER, primaryURI);
                MetaData.Request secondaryRequest = newRequest("GET", secondaryResource, secondaryFields);
                session.newStream(new HeadersFrame(secondaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

                    @Override
                    public void onData(Stream stream, DataFrame frame, Callback callback) {
                        callback.succeeded();
                        warmupLatch.countDown();
                    }
                });
            }
        }
    });
    Assert.assertTrue(warmupLatch.await(5, TimeUnit.SECONDS));
    Thread.sleep(1000);
    // Request again the primary resource, we should get the secondary resource pushed.
    primaryRequest = newRequest("GET", primaryResource, primaryFields);
    final CountDownLatch primaryResponseLatch = new CountDownLatch(1);
    final CountDownLatch pushLatch = new CountDownLatch(1);
    session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

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

        @Override
        public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            return new Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    if (frame.isEndStream())
                        pushLatch.countDown();
                }
            };
        }
    });
    Assert.assertTrue(pushLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) Promise(org.eclipse.jetty.util.Promise) Callback(org.eclipse.jetty.util.Callback) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 82 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class PushCacheFilterTest method testPushDisabled.

@Test
public void testPushDisabled() throws Exception {
    final String primaryResource = "/primary.html";
    final String secondaryResource = "/secondary.png";
    final byte[] secondaryData = "SECONDARY".getBytes("UTF-8");
    start(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String requestURI = req.getRequestURI();
            ServletOutputStream output = resp.getOutputStream();
            if (requestURI.endsWith(primaryResource))
                output.print("<html><head></head><body>PRIMARY</body></html>");
            else if (requestURI.endsWith(secondaryResource))
                output.write(secondaryData);
        }
    });
    final Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public Map<Integer, Integer> onPreface(Session session) {
            Map<Integer, Integer> settings = new HashMap<>();
            settings.put(SettingsFrame.ENABLE_PUSH, 0);
            return settings;
        }
    });
    // Request for the primary and secondary resource to build the cache.
    final String referrerURI = newURI(primaryResource);
    HttpFields primaryFields = new HttpFields();
    MetaData.Request primaryRequest = newRequest("GET", primaryResource, primaryFields);
    final CountDownLatch warmupLatch = new CountDownLatch(1);
    session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream()) {
                // Request for the secondary resource.
                HttpFields secondaryFields = new HttpFields();
                secondaryFields.put(HttpHeader.REFERER, referrerURI);
                MetaData.Request secondaryRequest = newRequest("GET", secondaryResource, secondaryFields);
                session.newStream(new HeadersFrame(secondaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

                    @Override
                    public void onData(Stream stream, DataFrame frame, Callback callback) {
                        callback.succeeded();
                        warmupLatch.countDown();
                    }
                });
            }
        }
    });
    Assert.assertTrue(warmupLatch.await(5, TimeUnit.SECONDS));
    // Request again the primary resource, we should not get the secondary resource pushed.
    primaryRequest = newRequest("GET", primaryResource, primaryFields);
    final CountDownLatch primaryResponseLatch = new CountDownLatch(1);
    final CountDownLatch pushLatch = new CountDownLatch(1);
    session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            pushLatch.countDown();
            return null;
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                primaryResponseLatch.countDown();
        }
    });
    Assert.assertFalse(pushLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) Promise(org.eclipse.jetty.util.Promise) Callback(org.eclipse.jetty.util.Callback) HashMap(java.util.HashMap) Map(java.util.Map) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 83 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method testStreamIdleTimeoutIsNotEnforcedWhenReceiving.

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

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

                @Override
                public boolean onIdleTimeout(Stream stream, Throwable x) {
                    timeoutLatch.countDown();
                    return true;
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter());
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    sleep(idleTimeout / 2);
    final CountDownLatch dataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), new Callback() {

        private int sends;

        @Override
        public void succeeded() {
            sleep(idleTimeout / 2);
            final boolean last = ++sends == 2;
            stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), last), !last ? this : new Callback() {

                @Override
                public InvocationType getInvocationType() {
                    return InvocationType.NON_BLOCKING;
                }

                @Override
                public void succeeded() {
                    // Idle timeout should not fire while receiving.
                    Assert.assertEquals(1, timeoutLatch.getCount());
                    dataLatch.countDown();
                }
            });
        }
    });
    Assert.assertTrue(dataLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
    // The server did not send a response, so it will eventually timeout.
    Assert.assertTrue(timeoutLatch.await(5 * idleTimeout, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) InvocationType(org.eclipse.jetty.util.thread.Invocable.InvocationType) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) 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) ServletInputStream(javax.servlet.ServletInputStream) 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 84 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method testStreamIdleTimeoutIsNotEnforcedWhenSending.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            return null;
        }

        @Override
        public void onReset(Session session, ResetFrame frame) {
            resetLatch.countDown();
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> promise = new FuturePromise<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(idleTimeout);
            super.succeeded(stream);
        }
    };
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter());
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    Callback.Completable completable1 = new Callback.Completable();
    sleep(idleTimeout / 2);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable1);
    completable1.thenCompose(nil -> {
        Callback.Completable completable2 = new Callback.Completable();
        sleep(idleTimeout / 2);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable2);
        return completable2;
    }).thenRun(() -> {
        sleep(idleTimeout / 2);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), true), Callback.NOOP);
    });
    Assert.assertFalse(resetLatch.await(1, TimeUnit.SECONDS));
}
Also used : ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpVersion(org.eclipse.jetty.http.HttpVersion) TimeoutException(java.util.concurrent.TimeoutException) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) MetaData(org.eclipse.jetty.http.MetaData) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) HttpFields(org.eclipse.jetty.http.HttpFields) Callback(org.eclipse.jetty.util.Callback) InvocationType(org.eclipse.jetty.util.thread.Invocable.InvocationType) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Session(org.eclipse.jetty.http2.api.Session) FuturePromise(org.eclipse.jetty.util.FuturePromise) Log(org.eclipse.jetty.util.log.Log) Assert(org.junit.Assert) 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) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) 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 85 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method testServerNotEnforcingIdleTimeoutWithinCallback.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            // Stay in the callback for more than idleTimeout,
            // but not for an integer number of idle timeouts,
            // to avoid a race where the idle timeout fires
            // again before we can send the headers to the client.
            sleep(idleTimeout + idleTimeout / 2);
            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;
        }
    });
    connector.setIdleTimeout(idleTimeout);
    final CountDownLatch closeLatch = new CountDownLatch(1);
    Session session = newClient(new ServerSessionListener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    final CountDownLatch replyLatch = new CountDownLatch(1);
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, true);
    session.newStream(requestFrame, new Promise.Adapter<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(10 * idleTimeout);
        }
    }, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            replyLatch.countDown();
        }
    });
    Assert.assertTrue(replyLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
    // Just make sure onClose() has never been called, but don't wait too much
    Assert.assertFalse(closeLatch.await(idleTimeout / 2, TimeUnit.MILLISECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) 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)

Aggregations

Session (org.eclipse.jetty.http2.api.Session)101 CountDownLatch (java.util.concurrent.CountDownLatch)93 Test (org.junit.Test)93 HttpFields (org.eclipse.jetty.http.HttpFields)89 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)89 Stream (org.eclipse.jetty.http2.api.Stream)88 MetaData (org.eclipse.jetty.http.MetaData)86 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)77 FuturePromise (org.eclipse.jetty.util.FuturePromise)74 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)51 Callback (org.eclipse.jetty.util.Callback)51 Promise (org.eclipse.jetty.util.Promise)51 HttpServletResponse (javax.servlet.http.HttpServletResponse)47 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)39 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)32 HttpServlet (javax.servlet.http.HttpServlet)30 ISession (org.eclipse.jetty.http2.ISession)26 ByteBuffer (java.nio.ByteBuffer)24