Search in sources :

Example 36 with FuturePromise

use of org.eclipse.jetty.util.FuturePromise in project jetty.project by eclipse.

the class TrailersTest method testServletRequestTrailers.

@Test
public void testServletRequestTrailers() throws Exception {
    CountDownLatch trailerLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Request jettyRequest = (Request) request;
            // No trailers yet.
            Assert.assertNull(jettyRequest.getTrailers());
            trailerLatch.countDown();
            // Read the content.
            ServletInputStream input = jettyRequest.getInputStream();
            while (true) {
                int read = input.read();
                if (read < 0)
                    break;
            }
            // Now we have the trailers.
            HttpFields trailers = jettyRequest.getTrailers();
            Assert.assertNotNull(trailers);
            Assert.assertNotNull(trailers.get("X-Trailer"));
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields requestFields = new HttpFields();
    requestFields.put("X-Request", "true");
    MetaData.Request request = newRequest("GET", requestFields);
    HeadersFrame requestFrame = new HeadersFrame(request, null, false);
    FuturePromise<Stream> streamPromise = new FuturePromise<>();
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(requestFrame, streamPromise, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
    // Send some data.
    Callback.Completable callback = new Callback.Completable();
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(16), false), callback);
    Assert.assertTrue(trailerLatch.await(5, TimeUnit.SECONDS));
    // Send the trailers.
    callback.thenRun(() -> {
        HttpFields trailerFields = new HttpFields();
        trailerFields.put("X-Trailer", "true");
        MetaData trailers = new MetaData(HttpVersion.HTTP_2, trailerFields);
        HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailers, null, true);
        stream.headers(trailerFrame, Callback.NOOP);
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) HttpServlet(javax.servlet.http.HttpServlet) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 37 with FuturePromise

use of org.eclipse.jetty.util.FuturePromise in project jetty.project by eclipse.

the class RawHTTP2ProxyTest method testRawHTTP2Proxy.

@Test
public void testRawHTTP2Proxy() throws Exception {
    byte[] data1 = new byte[1024];
    new Random().nextBytes(data1);
    ByteBuffer buffer1 = ByteBuffer.wrap(data1);
    Server server1 = startServer("server1", new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("SERVER1 received {}", frame);
            return new Stream.Listener.Adapter() {

                @Override
                public void onHeaders(Stream stream, HeadersFrame frame) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("SERVER1 received {}", frame);
                    if (frame.isEndStream()) {
                        MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                        HeadersFrame reply = new HeadersFrame(stream.getId(), response, null, false);
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("SERVER1 sending {}", reply);
                        stream.headers(reply, new Callback() {

                            @Override
                            public void succeeded() {
                                DataFrame data = new DataFrame(stream.getId(), buffer1.slice(), true);
                                if (LOGGER.isDebugEnabled())
                                    LOGGER.debug("SERVER1 sending {}", data);
                                stream.data(data, NOOP);
                            }
                        });
                    }
                }
            };
        }
    });
    ServerConnector connector1 = (ServerConnector) server1.getAttribute("connector");
    Server server2 = startServer("server2", new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("SERVER2 received {}", frame);
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("SERVER2 received {}", frame);
                    callback.succeeded();
                    MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    Callback.Completable completable1 = new Callback.Completable();
                    HeadersFrame reply = new HeadersFrame(stream.getId(), response, null, false);
                    if (LOGGER.isDebugEnabled())
                        LOGGER.debug("SERVER2 sending {}", reply);
                    stream.headers(reply, completable1);
                    completable1.thenCompose(ignored -> {
                        Callback.Completable completable2 = new Callback.Completable();
                        DataFrame data = new DataFrame(stream.getId(), buffer1.slice(), false);
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("SERVER2 sending {}", data);
                        stream.data(data, completable2);
                        return completable2;
                    }).thenRun(() -> {
                        MetaData trailer = new MetaData(HttpVersion.HTTP_2, new HttpFields());
                        HeadersFrame end = new HeadersFrame(stream.getId(), trailer, null, true);
                        if (LOGGER.isDebugEnabled())
                            LOGGER.debug("SERVER2 sending {}", end);
                        stream.headers(end, Callback.NOOP);
                    });
                }
            };
        }
    });
    ServerConnector connector2 = (ServerConnector) server2.getAttribute("connector");
    HTTP2Client proxyClient = startClient("proxyClient");
    Server proxyServer = startServer("proxyServer", new ClientToProxySessionListener(proxyClient));
    ServerConnector proxyConnector = (ServerConnector) proxyServer.getAttribute("connector");
    InetSocketAddress proxyAddress = new InetSocketAddress("localhost", proxyConnector.getLocalPort());
    HTTP2Client client = startClient("client");
    FuturePromise<Session> clientPromise = new FuturePromise<>();
    client.connect(proxyAddress, new Session.Listener.Adapter(), clientPromise);
    Session clientSession = clientPromise.get(5, TimeUnit.SECONDS);
    // Send a request with trailers for server1.
    HttpFields fields1 = new HttpFields();
    fields1.put("X-Target", String.valueOf(connector1.getLocalPort()));
    MetaData.Request request1 = new MetaData.Request("GET", new HttpURI("http://localhost/server1"), HttpVersion.HTTP_2, fields1);
    FuturePromise<Stream> streamPromise1 = new FuturePromise<>();
    CountDownLatch latch1 = new CountDownLatch(1);
    clientSession.newStream(new HeadersFrame(request1, null, false), streamPromise1, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("CLIENT received {}", frame);
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("CLIENT received {}", frame);
            Assert.assertEquals(buffer1.slice(), frame.getData());
            callback.succeeded();
            latch1.countDown();
        }
    });
    Stream stream1 = streamPromise1.get(5, TimeUnit.SECONDS);
    stream1.headers(new HeadersFrame(stream1.getId(), new MetaData(HttpVersion.HTTP_2, new HttpFields()), null, true), Callback.NOOP);
    // Send a request for server2.
    HttpFields fields2 = new HttpFields();
    fields2.put("X-Target", String.valueOf(connector2.getLocalPort()));
    MetaData.Request request2 = new MetaData.Request("GET", new HttpURI("http://localhost/server1"), HttpVersion.HTTP_2, fields2);
    FuturePromise<Stream> streamPromise2 = new FuturePromise<>();
    CountDownLatch latch2 = new CountDownLatch(1);
    clientSession.newStream(new HeadersFrame(request2, null, false), streamPromise2, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("CLIENT received {}", frame);
            if (frame.isEndStream())
                latch2.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("CLIENT received {}", frame);
            callback.succeeded();
        }
    });
    Stream stream2 = streamPromise2.get(5, TimeUnit.SECONDS);
    stream2.data(new DataFrame(stream2.getId(), buffer1.slice(), true), Callback.NOOP);
    Assert.assertTrue(latch1.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Server(org.eclipse.jetty.server.Server) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ServerConnector(org.eclipse.jetty.server.ServerConnector) Random(java.util.Random) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) HttpURI(org.eclipse.jetty.http.HttpURI) Callback(org.eclipse.jetty.util.Callback) IteratingCallback(org.eclipse.jetty.util.IteratingCallback) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 38 with FuturePromise

use of org.eclipse.jetty.util.FuturePromise in project jetty.project by eclipse.

the class SmallThreadPoolLoadTest method test.

private boolean test(Session session, CountDownLatch latch) throws Exception {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    // Choose a random method
    boolean download = random.nextBoolean();
    HttpMethod method = download ? HttpMethod.GET : HttpMethod.POST;
    int maxContentLength = 128 * 1024;
    int contentLength = random.nextInt(maxContentLength) + 1;
    long requestId = requestIds.incrementAndGet();
    MetaData.Request request = newRequest(method.asString(), "/" + requestId, new HttpFields());
    if (download)
        request.getFields().put("X-Download", String.valueOf(contentLength));
    HeadersFrame requestFrame = new HeadersFrame(request, null, download);
    FuturePromise<Stream> promise = new FuturePromise<>();
    CountDownLatch requestLatch = new CountDownLatch(1);
    AtomicBoolean reset = new AtomicBoolean();
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter() {

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

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

        @Override
        public void onReset(Stream stream, ResetFrame frame) {
            reset.set(true);
            requestLatch.countDown();
        }
    });
    if (!download) {
        Stream stream = promise.get(5, TimeUnit.SECONDS);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(contentLength), true), Callback.NOOP);
    }
    boolean success = requestLatch.await(5, TimeUnit.SECONDS);
    if (success)
        latch.countDown();
    else
        logger.warn("Request {} took too long{}Server:{}{}{}Client:{}{}", requestId, System.lineSeparator(), System.lineSeparator(), server.dump(), System.lineSeparator(), System.lineSeparator(), client.dump());
    return !reset.get();
}
Also used : 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IntStream(java.util.stream.IntStream) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) HttpMethod(org.eclipse.jetty.http.HttpMethod)

Example 39 with FuturePromise

use of org.eclipse.jetty.util.FuturePromise in project jetty.project by eclipse.

the class StreamCloseTest method testRequestClosedResponseClosedClosesStream.

@Test
public void testRequestClosedResponseClosedClosesStream() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    start(new ServerSessionListener.Adapter() {

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

                @Override
                public void succeeded() {
                    Assert.assertTrue(stream.isClosed());
                    Assert.assertEquals(0, stream.getSession().getStreams().size());
                    latch.countDown();
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, true);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            // The stream promise may not be notified yet here.
            latch.countDown();
        }
    });
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(stream.isClosed());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) 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) 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 40 with FuturePromise

use of org.eclipse.jetty.util.FuturePromise in project jetty.project by eclipse.

the class ProxyProtocolTest method test_PROXY_GET_v2.

@Test
public void test_PROXY_GET_v2() throws Exception {
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                Assert.assertEquals("10.0.0.4", request.getRemoteAddr());
                Assert.assertEquals(33824, request.getRemotePort());
                Assert.assertEquals("10.0.0.4", request.getLocalAddr());
                Assert.assertEquals(8888, request.getLocalPort());
            } catch (Throwable th) {
                th.printStackTrace();
                response.setStatus(500);
            }
            baseRequest.setHandled(true);
        }
    });
    String request1 = "0D0A0D0A000D0A515549540A211100140A0000040A000004842022B82000050000000000";
    SocketChannel channel = SocketChannel.open();
    channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
    channel.write(ByteBuffer.wrap(TypeUtil.fromHexString(request1)));
    FuturePromise<Session> promise = new FuturePromise<>();
    client.accept(null, channel, new Session.Listener.Adapter(), promise);
    Session session = promise.get(5, TimeUnit.SECONDS);
    HttpFields fields = new HttpFields();
    String uri = "http://localhost:" + connector.getLocalPort() + "/";
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            MetaData.Response response = (MetaData.Response) frame.getMetaData();
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) 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) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

FuturePromise (org.eclipse.jetty.util.FuturePromise)49 Test (org.junit.Test)42 Session (org.eclipse.jetty.http2.api.Session)40 CountDownLatch (java.util.concurrent.CountDownLatch)38 HttpFields (org.eclipse.jetty.http.HttpFields)36 Stream (org.eclipse.jetty.http2.api.Stream)36 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)36 MetaData (org.eclipse.jetty.http.MetaData)35 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)29 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)25 Callback (org.eclipse.jetty.util.Callback)22 HttpServletResponse (javax.servlet.http.HttpServletResponse)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)17 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)15 ISession (org.eclipse.jetty.http2.ISession)15 IOException (java.io.IOException)13 ServletException (javax.servlet.ServletException)13 FutureCallback (org.eclipse.jetty.util.FutureCallback)11 ByteBuffer (java.nio.ByteBuffer)10 HttpServlet (javax.servlet.http.HttpServlet)10