Search in sources :

Example 16 with HeadersFrame

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

the class HTTP2Test method testInvalidAPIUsageOnServer.

@Test
public void testInvalidAPIUsageOnServer() throws Exception {
    long sleep = 1000;
    CountDownLatch completeLatch = new CountDownLatch(2);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
            DataFrame dataFrame = new DataFrame(stream.getId(), BufferUtil.EMPTY_BUFFER, true);
            // The call to headers() is legal, but slow.
            new Thread(() -> {
                stream.headers(new HeadersFrame(stream.getId(), response, null, false) {

                    @Override
                    public MetaData getMetaData() {
                        sleep(2 * sleep);
                        return super.getMetaData();
                    }
                }, new Callback() {

                    @Override
                    public void succeeded() {
                        stream.data(dataFrame, NOOP);
                    }
                });
            }).start();
            // Wait for the headers() call to happen.
            sleep(sleep);
            // This data call is illegal because it does not
            // wait for the previous callback to complete.
            stream.data(dataFrame, new Callback() {

                @Override
                public void failed(Throwable x) {
                    if (x instanceof WritePendingException) {
                        // Expected.
                        completeLatch.countDown();
                    }
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    session.newStream(frame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                completeLatch.countDown();
        }
    });
    Assert.assertTrue(completeLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WritePendingException(java.nio.channels.WritePendingException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 17 with HeadersFrame

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

the class HTTP2Test method testHostHeader.

@Test
public void testHostHeader() throws Exception {
    final String host = "fooBar";
    final int port = 1313;
    final String authority = host + ":" + port;
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Assert.assertEquals(host, request.getServerName());
            Assert.assertEquals(port, request.getServerPort());
            Assert.assertEquals(authority, request.getHeader("Host"));
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HostPortHttpField hostHeader = new HostPortHttpField(authority);
    MetaData.Request metaData = new MetaData.Request("GET", HttpScheme.HTTP, hostHeader, servletPath, HttpVersion.HTTP_2, new HttpFields());
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    final 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(200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) HostPortHttpField(org.eclipse.jetty.http.HostPortHttpField) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 18 with HeadersFrame

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

the class IdleTimeoutTest method testBufferedReadsResetStreamIdleTimeout.

@Test
public void testBufferedReadsResetStreamIdleTimeout() throws Exception {
    int bufferSize = 8192;
    long delay = 1000;
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletInputStream input = request.getInputStream();
            byte[] buffer = new byte[bufferSize];
            while (true) {
                int read = input.read(buffer);
                Log.getLogger(IdleTimeoutTest.class).info("Read {} bytes", read);
                if (read < 0)
                    break;
                sleep(delay);
            }
        }
    });
    connector.setIdleTimeout(2 * delay);
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("POST", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    // Send data larger than the flow control window.
    // The client will send bytes up to the flow control window immediately
    // and they will be buffered by the server; the Servlet will consume them slowly.
    // Servlet reads should reset the idle timeout.
    int contentLength = FlowControlStrategy.DEFAULT_WINDOW_SIZE + 1;
    ByteBuffer data = ByteBuffer.allocate(contentLength);
    stream.data(new DataFrame(stream.getId(), data, true), Callback.NOOP);
    Assert.assertTrue(latch.await(2 * (contentLength / bufferSize + 1) * delay, TimeUnit.MILLISECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) 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) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) 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) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 19 with HeadersFrame

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

the class IdleTimeoutTest method testClientEnforcingStreamIdleTimeout.

@Test
public void testClientEnforcingStreamIdleTimeout() throws Exception {
    final int idleTimeout = 1000;
    start(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            sleep(2 * idleTimeout);
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    final CountDownLatch dataLatch = new CountDownLatch(1);
    final CountDownLatch timeoutLatch = 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(idleTimeout);
        }
    }, new Stream.Listener.Adapter() {

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

        @Override
        public boolean onIdleTimeout(Stream stream, Throwable x) {
            Assert.assertThat(x, Matchers.instanceOf(TimeoutException.class));
            timeoutLatch.countDown();
            return true;
        }
    });
    Assert.assertTrue(timeoutLatch.await(5, TimeUnit.SECONDS));
    // We must not receive any DATA frame.
    Assert.assertFalse(dataLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    // Stream must be gone.
    Assert.assertTrue(session.getStreams().isEmpty());
    // Session must not be closed, nor disconnected.
    Assert.assertFalse(session.isClosed());
    Assert.assertFalse(((HTTP2Session) session).isDisconnected());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) 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) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 20 with HeadersFrame

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

the class IdleTimeoutTest method testClientNotEnforcingIdleTimeoutWithinCallback.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            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;
        }

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    client.setIdleTimeout(idleTimeout);
    Session session = newClient(new Session.Listener.Adapter());
    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) {
            // Stay in the callback for more than idleTimeout,
            // but not for an integer number of idle timeouts,
            // to avoid that the idle timeout fires again.
            sleep(idleTimeout + idleTimeout / 2);
            replyLatch.countDown();
        }
    });
    Assert.assertFalse(closeLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    Assert.assertTrue(replyLatch.await(5 * idleTimeout, 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

HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)114 HttpFields (org.eclipse.jetty.http.HttpFields)111 MetaData (org.eclipse.jetty.http.MetaData)111 Test (org.junit.Test)106 CountDownLatch (java.util.concurrent.CountDownLatch)98 Stream (org.eclipse.jetty.http2.api.Stream)98 Session (org.eclipse.jetty.http2.api.Session)90 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)82 FuturePromise (org.eclipse.jetty.util.FuturePromise)69 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)57 Callback (org.eclipse.jetty.util.Callback)54 HttpServletResponse (javax.servlet.http.HttpServletResponse)53 Promise (org.eclipse.jetty.util.Promise)50 ByteBuffer (java.nio.ByteBuffer)40 HttpServletRequest (javax.servlet.http.HttpServletRequest)39 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)38 IOException (java.io.IOException)37 ServletException (javax.servlet.ServletException)36 HashMap (java.util.HashMap)33 HttpServlet (javax.servlet.http.HttpServlet)33