Search in sources :

Example 6 with PushPromiseFrame

use of org.eclipse.jetty.http2.frames.PushPromiseFrame 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 7 with PushPromiseFrame

use of org.eclipse.jetty.http2.frames.PushPromiseFrame 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 8 with PushPromiseFrame

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

the class HTTP2ClientSession method onPushPromise.

@Override
public void onPushPromise(PushPromiseFrame frame) {
    if (LOG.isDebugEnabled())
        LOG.debug("Received {}", frame);
    int streamId = frame.getStreamId();
    int pushStreamId = frame.getPromisedStreamId();
    IStream stream = getStream(streamId);
    if (stream == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("Ignoring {}, stream #{} not found", frame, streamId);
    } else {
        IStream pushStream = createRemoteStream(pushStreamId);
        pushStream.process(frame, Callback.NOOP);
        Stream.Listener listener = notifyPush(stream, pushStream, frame);
        pushStream.setListener(listener);
    }
}
Also used : IStream(org.eclipse.jetty.http2.IStream) Stream(org.eclipse.jetty.http2.api.Stream) IStream(org.eclipse.jetty.http2.IStream) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 9 with PushPromiseFrame

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

the class PushCacheFilterTest method testPush.

@Test
public void testPush() 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 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 get the secondary resource pushed.
    primaryRequest = newRequest("GET", primaryResource, primaryFields);
    final CountDownLatch primaryResponseLatch = new CountDownLatch(2);
    final CountDownLatch pushLatch = new CountDownLatch(2);
    session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

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

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

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

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

                @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) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Callback(org.eclipse.jetty.util.Callback) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 10 with PushPromiseFrame

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

the class PushCacheFilterTest method testRecursivePush.

@Test
public void testRecursivePush() throws Exception {
    final String primaryResource = "/primary.html";
    final String secondaryResource1 = "/secondary1.css";
    final String secondaryResource2 = "/secondary2.js";
    final String tertiaryResource = "/tertiary.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(primaryResource))
                output.print("<html><head></head><body>PRIMARY</body></html>");
            else if (requestURI.endsWith(secondaryResource1))
                output.print("body { background-image: url(\"" + tertiaryResource + "\"); }");
            else if (requestURI.endsWith(secondaryResource2))
                output.print("(function() { window.alert('HTTP/2'); })()");
            if (requestURI.endsWith(tertiaryResource))
                output.write("TERTIARY".getBytes(StandardCharsets.UTF_8));
        }
    });
    final Session session = newClient(new Session.Listener.Adapter());
    // Request for the primary, secondary and tertiary 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(2);
    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 resources.
                String secondaryURI1 = newURI(secondaryResource1);
                HttpFields secondaryFields1 = new HttpFields();
                secondaryFields1.put(HttpHeader.REFERER, primaryURI);
                MetaData.Request secondaryRequest1 = newRequest("GET", secondaryResource1, secondaryFields1);
                session.newStream(new HeadersFrame(secondaryRequest1, 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 tertiary resource.
                            HttpFields tertiaryFields = new HttpFields();
                            tertiaryFields.put(HttpHeader.REFERER, secondaryURI1);
                            MetaData.Request tertiaryRequest = newRequest("GET", tertiaryResource, tertiaryFields);
                            session.newStream(new HeadersFrame(tertiaryRequest, null, true), new Promise.Adapter<>(), new Adapter() {

                                @Override
                                public void onData(Stream stream, DataFrame frame, Callback callback) {
                                    callback.succeeded();
                                    if (frame.isEndStream())
                                        warmupLatch.countDown();
                                }
                            });
                        }
                    }
                });
                HttpFields secondaryFields2 = new HttpFields();
                secondaryFields2.put(HttpHeader.REFERER, primaryURI);
                MetaData.Request secondaryRequest2 = newRequest("GET", secondaryResource2, secondaryFields2);
                session.newStream(new HeadersFrame(secondaryRequest2, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

                    @Override
                    public void onData(Stream stream, DataFrame frame, Callback callback) {
                        callback.succeeded();
                        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 and tertiary resources pushed.
    primaryRequest = newRequest("GET", primaryResource, primaryFields);
    final CountDownLatch primaryResponseLatch = new CountDownLatch(1);
    final CountDownLatch primaryPushesLatch = new CountDownLatch(3);
    final CountDownLatch recursiveLatch = 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())
                primaryResponseLatch.countDown();
        }

        @Override
        public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            // The stream id of the PUSH_PROMISE must
            // always be a client stream and therefore odd.
            Assert.assertEquals(1, frame.getStreamId() & 1);
            return new Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    callback.succeeded();
                    if (frame.isEndStream())
                        primaryPushesLatch.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())
                                recursiveLatch.countDown();
                        }
                    };
                }
            };
        }
    });
    Assert.assertTrue(primaryPushesLatch.await(5, TimeUnit.SECONDS));
    Assert.assertFalse(recursiveLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
    // Make sure that explicitly requesting a secondary resource, we get the tertiary pushed.
    CountDownLatch secondaryResponseLatch = new CountDownLatch(1);
    CountDownLatch secondaryPushLatch = new CountDownLatch(1);
    MetaData.Request secondaryRequest = newRequest("GET", secondaryResource1, new HttpFields());
    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();
        }

        @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())
                        secondaryPushLatch.countDown();
                }
            };
        }
    });
    Assert.assertTrue(secondaryPushLatch.await(5, TimeUnit.SECONDS));
    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) 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