Search in sources :

Example 11 with PushPromiseFrame

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

the class PushCacheFilterTest method testSelfPush.

@Test
public void testSelfPush() throws Exception {
    // The test case is that of a login page, for example.
    // When the user sends the credentials to the login page,
    // the login may fail and redirect to the same login page,
    // perhaps with different query parameters.
    // In this case a request for the login page will push
    // the login page itself, which will generate the pushed
    // request for the login page, which will push the login
    // page itself, etc. which is not the desired behavior.
    final String primaryResource = "/login.html";
    start(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletOutputStream output = response.getOutputStream();
            String credentials = request.getParameter("credentials");
            if (credentials == null) {
                output.print("<html><head></head><body>LOGIN</body></html>");
            } else if ("secret".equals(credentials)) {
                output.print("<html><head></head><body>OK</body></html>");
            } else {
                response.setStatus(HttpStatus.TEMPORARY_REDIRECT_307);
                response.setHeader(HttpHeader.LOCATION.asString(), primaryResource);
            }
        }
    });
    final String primaryURI = newURI(primaryResource);
    final Session session = newClient(new Session.Listener.Adapter());
    // Login with the wrong credentials, causing a redirect to self.
    HttpFields primaryFields = new HttpFields();
    MetaData.Request primaryRequest = newRequest("GET", primaryResource + "?credentials=wrong", 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()) {
                MetaData.Response response = (MetaData.Response) frame.getMetaData();
                if (response.getStatus() == HttpStatus.TEMPORARY_REDIRECT_307) {
                    // Follow the redirect.
                    String location = response.getFields().get(HttpHeader.LOCATION);
                    HttpFields redirectFields = new HttpFields();
                    redirectFields.put(HttpHeader.REFERER, primaryURI);
                    MetaData.Request redirectRequest = newRequest("GET", location, redirectFields);
                    session.newStream(new HeadersFrame(redirectRequest, null, true), new Promise.Adapter<>(), new 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);
    // Login with the right credentials, there must be no push.
    primaryRequest = newRequest("GET", primaryResource + "?credentials=secret", 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 onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                primaryResponseLatch.countDown();
        }

        @Override
        public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            pushLatch.countDown();
            return null;
        }
    });
    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) 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 12 with PushPromiseFrame

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

the class HttpReceiverOverHTTP2 method onPush.

@Override
public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null)
        return null;
    HttpRequest request = exchange.getRequest();
    MetaData.Request metaData = (MetaData.Request) frame.getMetaData();
    HttpRequest pushRequest = (HttpRequest) getHttpDestination().getHttpClient().newRequest(metaData.getURIString());
    BiFunction<Request, Request, Response.CompleteListener> pushListener = request.getPushListener();
    if (pushListener != null) {
        Response.CompleteListener listener = pushListener.apply(request, pushRequest);
        if (listener != null) {
            HttpChannelOverHTTP2 pushChannel = getHttpChannel().getHttpConnection().newHttpChannel(true);
            List<Response.ResponseListener> listeners = Collections.singletonList(listener);
            HttpExchange pushExchange = new HttpExchange(getHttpDestination(), pushRequest, listeners);
            pushChannel.associate(pushExchange);
            pushChannel.setStream(stream);
            // TODO: idle timeout ?
            pushExchange.requestComplete(null);
            pushExchange.terminateRequest();
            return pushChannel.getStreamListener();
        }
    }
    stream.reset(new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
    return null;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) HttpExchange(org.eclipse.jetty.client.HttpExchange) Request(org.eclipse.jetty.client.api.Request) HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpResponse(org.eclipse.jetty.client.HttpResponse) MetaData(org.eclipse.jetty.http.MetaData) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame)

Example 13 with PushPromiseFrame

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

the class PushPromiseGenerateParseTest method testGenerateParseOneByteAtATime.

@Test
public void testGenerateParseOneByteAtATime() throws Exception {
    PushPromiseGenerator generator = new PushPromiseGenerator(new HeaderGenerator(), new HpackEncoder());
    final List<PushPromiseFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onPushPromise(PushPromiseFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int promisedStreamId = 17;
    HttpFields fields = new HttpFields();
    fields.put("Accept", "text/html");
    fields.put("User-Agent", "Jetty");
    MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields);
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generatePushPromise(lease, streamId, promisedStreamId, metaData);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
            }
        }
        Assert.assertEquals(1, frames.size());
        PushPromiseFrame frame = frames.get(0);
        Assert.assertEquals(streamId, frame.getStreamId());
        Assert.assertEquals(promisedStreamId, frame.getPromisedStreamId());
        MetaData.Request request = (MetaData.Request) frame.getMetaData();
        Assert.assertEquals(metaData.getMethod(), request.getMethod());
        Assert.assertEquals(metaData.getURI(), request.getURI());
        for (int j = 0; j < fields.size(); ++j) {
            HttpField field = fields.getField(j);
            Assert.assertTrue(request.getFields().contains(field));
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PushPromiseGenerator(org.eclipse.jetty.http2.generator.PushPromiseGenerator) ArrayList(java.util.ArrayList) HpackEncoder(org.eclipse.jetty.http2.hpack.HpackEncoder) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) MetaData(org.eclipse.jetty.http.MetaData) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) Test(org.junit.Test)

Example 14 with PushPromiseFrame

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

the class PushPromiseBodyParser method onPushPromise.

private void onPushPromise(int streamId, MetaData metaData) {
    PushPromiseFrame frame = new PushPromiseFrame(getStreamId(), streamId, metaData);
    notifyPushPromise(frame);
}
Also used : PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame)

Example 15 with PushPromiseFrame

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

the class PushPromiseGenerateParseTest method testGenerateParse.

@Test
public void testGenerateParse() throws Exception {
    PushPromiseGenerator generator = new PushPromiseGenerator(new HeaderGenerator(), new HpackEncoder());
    final List<PushPromiseFrame> frames = new ArrayList<>();
    Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {

        @Override
        public void onPushPromise(PushPromiseFrame frame) {
            frames.add(frame);
        }
    }, 4096, 8192);
    int streamId = 13;
    int promisedStreamId = 17;
    HttpFields fields = new HttpFields();
    fields.put("Accept", "text/html");
    fields.put("User-Agent", "Jetty");
    MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, new HostPortHttpField("localhost:8080"), "/path", HttpVersion.HTTP_2, fields);
    // Iterate a few times to be sure generator and parser are properly reset.
    for (int i = 0; i < 2; ++i) {
        ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
        generator.generatePushPromise(lease, streamId, promisedStreamId, metaData);
        frames.clear();
        for (ByteBuffer buffer : lease.getByteBuffers()) {
            while (buffer.hasRemaining()) {
                parser.parse(buffer);
            }
        }
        Assert.assertEquals(1, frames.size());
        PushPromiseFrame frame = frames.get(0);
        Assert.assertEquals(streamId, frame.getStreamId());
        Assert.assertEquals(promisedStreamId, frame.getPromisedStreamId());
        MetaData.Request request = (MetaData.Request) frame.getMetaData();
        Assert.assertEquals(metaData.getMethod(), request.getMethod());
        Assert.assertEquals(metaData.getURI(), request.getURI());
        for (int j = 0; j < fields.size(); ++j) {
            HttpField field = fields.getField(j);
            Assert.assertTrue(request.getFields().contains(field));
        }
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) PushPromiseGenerator(org.eclipse.jetty.http2.generator.PushPromiseGenerator) ArrayList(java.util.ArrayList) HpackEncoder(org.eclipse.jetty.http2.hpack.HpackEncoder) ByteBuffer(java.nio.ByteBuffer) Parser(org.eclipse.jetty.http2.parser.Parser) MetaData(org.eclipse.jetty.http.MetaData) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) HttpField(org.eclipse.jetty.http.HttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HeaderGenerator(org.eclipse.jetty.http2.generator.HeaderGenerator) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) 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