Search in sources :

Example 26 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HTTP2Test method testCleanGoAwayDoesNotTriggerFailureNotification.

@Test
public void testCleanGoAwayDoesNotTriggerFailureNotification() throws Exception {
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
            HeadersFrame response = new HeadersFrame(stream.getId(), metaData, null, true);
            stream.headers(response, Callback.NOOP);
            // Close cleanly.
            stream.getSession().close(ErrorCode.NO_ERROR.code, null, Callback.NOOP);
            return null;
        }
    });
    CountDownLatch closeLatch = new CountDownLatch(1);
    CountDownLatch failureLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }

        @Override
        public void onFailure(Session session, Throwable failure) {
            failureLatch.countDown();
        }
    });
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame request = new HeadersFrame(metaData, null, true);
    session.newStream(request, new Promise.Adapter<>(), new Stream.Listener.Adapter());
    // Make sure onClose() is called.
    Assert.assertTrue(closeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertFalse(failureLatch.await(1, TimeUnit.SECONDS));
}
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) 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 27 with HttpFields

use of org.eclipse.jetty.http.HttpFields in project jetty.project by eclipse.

the class HTTP2Test method testCustomResponseCode.

@Test
public void testCustomResponseCode() throws Exception {
    final int status = 475;
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setStatus(status);
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    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(status, 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) 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) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 28 with HttpFields

use of org.eclipse.jetty.http.HttpFields 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 29 with HttpFields

use of org.eclipse.jetty.http.HttpFields 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 30 with HttpFields

use of org.eclipse.jetty.http.HttpFields 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)

Aggregations

HttpFields (org.eclipse.jetty.http.HttpFields)185 Test (org.junit.Test)143 MetaData (org.eclipse.jetty.http.MetaData)118 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)106 CountDownLatch (java.util.concurrent.CountDownLatch)96 Stream (org.eclipse.jetty.http2.api.Stream)94 Session (org.eclipse.jetty.http2.api.Session)90 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)80 FuturePromise (org.eclipse.jetty.util.FuturePromise)70 HttpServletResponse (javax.servlet.http.HttpServletResponse)62 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)55 Callback (org.eclipse.jetty.util.Callback)53 ByteBuffer (java.nio.ByteBuffer)52 Promise (org.eclipse.jetty.util.Promise)49 HttpServletRequest (javax.servlet.http.HttpServletRequest)48 IOException (java.io.IOException)43 ServletException (javax.servlet.ServletException)40 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)37 HttpServlet (javax.servlet.http.HttpServlet)33 HashMap (java.util.HashMap)32