Search in sources :

Example 11 with ResetFrame

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

the class StreamResetTest method testBlockingWriteAfterStreamReceivingReset.

@Test
public void testBlockingWriteAfterStreamReceivingReset() throws Exception {
    final CountDownLatch resetLatch = new CountDownLatch(1);
    final CountDownLatch dataLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Charset charset = StandardCharsets.UTF_8;
            byte[] data = "AFTER RESET".getBytes(charset);
            response.setStatus(200);
            response.setContentType("text/plain;charset=" + charset.name());
            response.setContentLength(data.length * 10);
            response.flushBuffer();
            try {
                // Wait for the reset to happen.
                Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
            try {
                // been reset, it should throw an exception.
                for (int i = 0; i < 10; i++) {
                    Thread.sleep(500);
                    response.getOutputStream().write(data);
                    response.flushBuffer();
                }
            } catch (InterruptedException x) {
            } catch (IOException x) {
                dataLatch.countDown();
            }
        }
    });
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame frame = new HeadersFrame(request, null, true);
    client.newStream(frame, new FuturePromise<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
            resetLatch.countDown();
        }
    });
    Assert.assertTrue(dataLatch.await(5, TimeUnit.SECONDS));
}
Also used : InterruptedIOException(java.io.InterruptedIOException) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Charset(java.nio.charset.Charset) InterruptedIOException(java.io.InterruptedIOException) 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) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 12 with ResetFrame

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

the class StreamResetTest method testStreamReceivingResetIsRemoved.

@Test
public void testStreamReceivingResetIsRemoved() throws Exception {
    final AtomicReference<Stream> streamRef = new AtomicReference<>();
    final 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) {
                    Assert.assertNotNull(stream);
                    Assert.assertTrue(stream.isReset());
                    streamRef.set(stream);
                    resetLatch.countDown();
                }
            };
        }
    });
    Session client = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(request, null, false);
    FuturePromise<Stream> promise = new FuturePromise<>();
    client.newStream(requestFrame, promise, new Stream.Listener.Adapter());
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    ResetFrame resetFrame = new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code);
    stream.reset(resetFrame, Callback.NOOP);
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    // Wait a while to let the server remove the
    // stream after returning from onReset().
    Thread.sleep(1000);
    Stream serverStream = streamRef.get();
    Assert.assertEquals(0, serverStream.getSession().getStreams().size());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) WriteListener(javax.servlet.WriteListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServletOutputStream(javax.servlet.ServletOutputStream) IStream(org.eclipse.jetty.http2.IStream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 13 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 14 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 15 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)

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