Search in sources :

Example 1 with ResetFrame

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

the class HttpClientTransportOverHTTP2Test method testRequestIdleTimeoutSendsResetFrame.

@Test
public void testRequestIdleTimeoutSendsResetFrame() throws Exception {
    CountDownLatch resetLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            return new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    resetLatch.countDown();
                }
            };
        }
    });
    try {
        long idleTimeout = 1000;
        client.newRequest("localhost", connector.getLocalPort()).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).send();
        Assert.fail();
    } catch (ExecutionException e) {
    // Expected.
    }
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Stream(org.eclipse.jetty.http2.api.Stream) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 2 with ResetFrame

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

the class HttpClientTransportOverHTTP2Test method testResponseAbortSendsResetFrame.

@Test
public void testResponseAbortSendsResetFrame() throws Exception {
    CountDownLatch resetLatch = new CountDownLatch(1);
    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());
            stream.headers(new HeadersFrame(stream.getId(), metaData, null, false), new Callback() {

                @Override
                public void succeeded() {
                    ByteBuffer data = ByteBuffer.allocate(1024);
                    stream.data(new DataFrame(stream.getId(), data, false), NOOP);
                }
            });
            return new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    resetLatch.countDown();
                }
            };
        }
    });
    try {
        client.newRequest("localhost", connector.getLocalPort()).onResponseContent((response, buffer) -> response.abort(new Exception("explicitly_aborted_by_test"))).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    }
}
Also used : Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) ServerSocket(java.net.ServerSocket) HttpClient(org.eclipse.jetty.client.HttpClient) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetaData(org.eclipse.jetty.http.MetaData) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) HttpStatus(org.eclipse.jetty.http.HttpStatus) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Callback(org.eclipse.jetty.util.Callback) HttpDestination(org.eclipse.jetty.client.HttpDestination) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Session(org.eclipse.jetty.http2.api.Session) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) BufferUtil(org.eclipse.jetty.util.BufferUtil) Socket(java.net.Socket) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) ErrorCode(org.eclipse.jetty.http2.ErrorCode) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) SocketTimeoutException(java.net.SocketTimeoutException) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpFields(org.eclipse.jetty.http.HttpFields) OutputStream(java.io.OutputStream) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) Executor(java.util.concurrent.Executor) RawHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.RawHTTP2ServerConnectionFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) ServerParser(org.eclipse.jetty.http2.parser.ServerParser) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Generator(org.eclipse.jetty.http2.generator.Generator) HttpMethod(org.eclipse.jetty.http.HttpMethod) Ignore(org.junit.Ignore) Assert(org.junit.Assert) InputStream(java.io.InputStream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) ServletException(javax.servlet.ServletException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) OutputStream(java.io.OutputStream) InputStream(java.io.InputStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ExecutionException(java.util.concurrent.ExecutionException) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 3 with ResetFrame

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

the class PushedResourcesTest method testPushedResourceCancelled.

@Test
public void testPushedResourceCancelled() throws Exception {
    String pushPath = "/secondary";
    CountDownLatch latch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpURI pushURI = new HttpURI("http://localhost:" + connector.getLocalPort() + pushPath);
            MetaData.Request pushRequest = new MetaData.Request(HttpMethod.GET.asString(), pushURI, HttpVersion.HTTP_2, new HttpFields());
            stream.push(new PushPromiseFrame(stream.getId(), 0, pushRequest), new Promise.Adapter<Stream>() {

                @Override
                public void succeeded(Stream pushStream) {
                    // Just send the normal response and wait for the reset.
                    MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, new HttpFields());
                    stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
                }
            }, new Stream.Listener.Adapter() {

                @Override
                public void onReset(Stream stream, ResetFrame frame) {
                    latch.countDown();
                }
            });
            return null;
        }
    });
    HttpRequest request = (HttpRequest) client.newRequest("localhost", connector.getLocalPort());
    ContentResponse response = request.pushListener((mainRequest, pushedRequest) -> null).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpRequest(org.eclipse.jetty.client.HttpRequest) CountDownLatch(java.util.concurrent.CountDownLatch) PushPromiseFrame(org.eclipse.jetty.http2.frames.PushPromiseFrame) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpURI(org.eclipse.jetty.http.HttpURI) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Test(org.junit.Test)

Example 4 with ResetFrame

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

the class FlowControlStrategyTest method testFlowControlWhenServerResetsStream.

@Test
public void testFlowControlWhenServerResetsStream() throws Exception {
    // On server, don't consume the data and immediately reset.
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Request request = (MetaData.Request) frame.getMetaData();
            if (HttpMethod.GET.is(request.getMethod()))
                return new Stream.Listener.Adapter();
            return new Stream.Listener.Adapter() {

                @Override
                public void onData(Stream stream, DataFrame frame, Callback callback) {
                    // Fail the callback to enlarge the session window.
                    // More data frames will be discarded because the
                    // stream is reset, and automatically consumed to
                    // keep the session window large for other streams.
                    callback.failed(new Throwable());
                    stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
                }
            };
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("POST", new HttpFields());
    HeadersFrame frame = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> streamPromise = new FuturePromise<>();
    final CountDownLatch resetLatch = new CountDownLatch(1);
    session.newStream(frame, streamPromise, new Stream.Listener.Adapter() {

        @Override
        public void onReset(Stream stream, ResetFrame frame) {
            resetLatch.countDown();
        }
    });
    Stream stream = streamPromise.get(5, TimeUnit.SECONDS);
    // Perform a big upload that will stall the flow control windows.
    ByteBuffer data = ByteBuffer.allocate(5 * FlowControlStrategy.DEFAULT_WINDOW_SIZE);
    final CountDownLatch dataLatch = new CountDownLatch(1);
    stream.data(new DataFrame(stream.getId(), data, true), new Callback() {

        @Override
        public InvocationType getInvocationType() {
            return InvocationType.NON_BLOCKING;
        }

        @Override
        public void failed(Throwable x) {
            dataLatch.countDown();
        }
    });
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) InvocationType(org.eclipse.jetty.util.thread.Invocable.InvocationType) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ByteBuffer(java.nio.ByteBuffer) Callback(org.eclipse.jetty.util.Callback) FutureCallback(org.eclipse.jetty.util.FutureCallback) 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) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 5 with ResetFrame

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

the class PushCacheFilterTest method testPushIsReset.

@Test
public void testPushIsReset() 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 primaryURI = 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, primaryURI);
                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(1);
    final CountDownLatch pushLatch = new CountDownLatch(1);
    session.newStream(new HeadersFrame(primaryRequest, null, true), new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public Stream.Listener onPush(Stream stream, PushPromiseFrame frame) {
            // Reset the stream as soon as we see the push.
            ResetFrame resetFrame = new ResetFrame(stream.getId(), ErrorCode.REFUSED_STREAM_ERROR.code);
            stream.reset(resetFrame, Callback.NOOP);
            return new Adapter() {

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

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                primaryResponseLatch.countDown();
        }
    });
    // We should not receive pushed data that we reset.
    Assert.assertFalse(pushLatch.await(1, TimeUnit.SECONDS));
    Assert.assertTrue(primaryResponseLatch.await(5, TimeUnit.SECONDS));
    // Make sure the session is sane by requesting the secondary resource.
    HttpFields secondaryFields = new HttpFields();
    secondaryFields.put(HttpHeader.REFERER, primaryURI);
    MetaData.Request secondaryRequest = newRequest("GET", secondaryResource, secondaryFields);
    final CountDownLatch secondaryResponseLatch = new CountDownLatch(1);
    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();
        }
    });
    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) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

ResetFrame (org.eclipse.jetty.http2.frames.ResetFrame)24 Test (org.junit.Test)21 Stream (org.eclipse.jetty.http2.api.Stream)20 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 MetaData (org.eclipse.jetty.http.MetaData)19 HttpFields (org.eclipse.jetty.http.HttpFields)18 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)18 Session (org.eclipse.jetty.http2.api.Session)16 Callback (org.eclipse.jetty.util.Callback)13 FuturePromise (org.eclipse.jetty.util.FuturePromise)12 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)11 IOException (java.io.IOException)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 ByteBuffer (java.nio.ByteBuffer)9 ServletOutputStream (javax.servlet.ServletOutputStream)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ISession (org.eclipse.jetty.http2.ISession)9 ServletException (javax.servlet.ServletException)8 WriteListener (javax.servlet.WriteListener)8