Search in sources :

Example 1 with PushPromiseFrame

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

the class PushedResourcesTest method testPushedResourceCancelled.

@Test
public void testPushedResourceCancelled() throws Exception {
    String pushPath = "/secondary";
    CountDownLatch latch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpURI pushURI = new HttpURI("http://localhost:" + connector.getLocalPort() + pushPath);
            MetaData.Request pushRequest = new MetaData.Request(HttpMethod.GET.asString(), pushURI, HttpVersion.HTTP_2, new HttpFields());
            stream.push(new PushPromiseFrame(stream.getId(), 0, pushRequest), new Promise.Adapter<Stream>() {

                @Override
                public void succeeded(Stream pushStream) {
                    // Just send the normal response and wait for the reset.
                    MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                }
            }, new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    latch.countDown();
                }
            });
            return null;
        }
    });
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpURI(org.eclipse.jetty.http.HttpURI) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) Test(org.junit.Test)

Example 2 with PushPromiseFrame

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

the class PushCacheFilterTest method testPushReferrerNoPath.

@Test
public void testPushReferrerNoPath() 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());
    // Request for the primary and secondary resource to build the cache.
    // The referrerURI does not point to the primary resource, so there will be no
    // resource association with the primary resource and therefore won't be pushed.
    final String referrerURI = "http://localhost:" + connector.getLocalPort();
    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) {
            return new Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    if (frame.isEndStream())
                        pushLatch.countDown();
                }
            };
        }

        @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) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 3 with PushPromiseFrame

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

the class PushCacheFilterTest method testPushWithQueryParameters.

@Test
public void testPushWithQueryParameters() throws Exception {
    String name = "foo";
    String value = "bar";
    final String primaryResource = "/primary.html?" + name + "=" + value;
    final String secondaryResource = "/secondary.html?" + name + "=" + value;
    start(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String requestURI = request.getRequestURI();
            if (requestURI.endsWith(primaryResource)) {
                response.setStatus(HttpStatus.OK_200);
            } else if (requestURI.endsWith(secondaryResource)) {
                String param = request.getParameter(name);
                if (param == null)
                    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500);
                else
                    response.setStatus(HttpStatus.OK_200);
            }
        }
    });
    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 onHeaders(Stream stream, HeadersFrame frame) {
                        if (frame.isEndStream())
                            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 Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            MetaData metaData = frame.getMetaData();
            Assert.assertTrue(metaData instanceof MetaData.Request);
            MetaData.Request pushedRequest = (MetaData.Request) metaData;
            Assert.assertEquals(contextPath + servletPath + secondaryResource, pushedRequest.getURI().getPathQuery());
            return new Adapter() {

                @Override
                public void onHeaders(Stream stream, HeadersFrame frame) {
                    if (frame.isEndStream()) {
                        MetaData.Response response = (MetaData.Response) frame.getMetaData();
                        if (response.getStatus() == HttpStatus.OK_200)
                            pushLatch.countDown();
                    }
                }
            };
        }

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

Example 4 with PushPromiseFrame

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

the class PushCacheFilterTest method testPushIsReset.

@Test
public void testPushIsReset() 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());
    // 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 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, 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));
    // 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 Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            // Reset the stream as soon as we see the push.
            ResetFrame resetFrame = new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code);
            stream.reset(resetFrame, Callback.NOOP);
            return new Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    pushLatch.countDown();
                }
            };
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                primaryResponseLatch.countDown();
        }
    });
    // We should not receive pushed data that we reset.
    Assert.assertFalse(pushLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
    // Make sure the session is sane by requesting the secondary resource.
    HttpFields secondaryFields = new HttpFields();
    secondaryFields.put(HttpHeader.REFERER, primaryURI);
    MetaData.Request secondaryRequest = newRequest("GET", secondaryResource, secondaryFields);
    final CountDownLatch secondaryResponseLatch = new CountDownLatch(1);
    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();
            if (frame.isEndStream())
                secondaryResponseLatch.countDown();
        }
    });
    Assert.assertTrue(secondaryResponseLatch.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) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 5 with PushPromiseFrame

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

the class PushCacheFilterTest method testPOSTRequestIsNotPushed.

@Test
public void testPOSTRequestIsNotPushed() 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 service(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());
    // 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 with POST, we should not get the secondary resource pushed.
    primaryRequest = newRequest("POST", 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) {
            return new Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    if (frame.isEndStream())
                        pushLatch.countDown();
                }
            };
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                primaryResponseLatch.countDown();
        }
    });
    Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
    Assert.assertFalse(pushLatch.await(1, 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)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)15 MetaData (org.eclipse.jetty.http.MetaData)15 PushPromiseFrame (org.eclipse.jetty.http2.frames.PushPromiseFrame)15 Stream (org.eclipse.jetty.http2.api.Stream)14 Test (org.junit.Test)14 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 Session (org.eclipse.jetty.http2.api.Session)12 Promise (org.eclipse.jetty.util.Promise)12 Callback (org.eclipse.jetty.util.Callback)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)10 IOException (java.io.IOException)9 ServletException (javax.servlet.ServletException)9 ServletOutputStream (javax.servlet.ServletOutputStream)9 HttpServlet (javax.servlet.http.HttpServlet)9 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)4 ResetFrame (org.eclipse.jetty.http2.frames.ResetFrame)4 FuturePromise (org.eclipse.jetty.util.FuturePromise)3