Search in sources :

Example 56 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method testServerEnforcingIdleTimeoutWithUnrespondedStream.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(10 * idleTimeout);
            return null;
        }
    });
    connector.setIdleTimeout(idleTimeout);
    final CountDownLatch latch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            latch.countDown();
        }
    });
    // The request is not replied, and the server should idle timeout.
    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());
    Assert.assertTrue(latch.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) 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)

Example 57 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method testClientEnforcingIdleTimeoutWithUnrespondedStream.

@Test
public void testClientEnforcingIdleTimeoutWithUnrespondedStream() 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);
            return null;
        }

        @Override
        public void onClose(Session session, GoAwayFrame frame) {
            closeLatch.countDown();
        }
    });
    client.setIdleTimeout(idleTimeout);
    Session session = newClient(new Session.Listener.Adapter());
    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());
    Assert.assertTrue(closeLatch.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) 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)

Example 58 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class AsyncIOServletTest method testAsyncReadEarlyEOF.

@Test
public void testAsyncReadEarlyEOF() throws Exception {
    // SSLEngine receives the close alert from the client, and when
    // the server passes the response to encrypt and write, SSLEngine
    // only generates the close alert back, without encrypting the
    // response, so we need to skip the transports over TLS.
    Assume.assumeThat(transport, Matchers.not(Matchers.isOneOf(Transport.HTTPS, Transport.H2)));
    String content = "jetty";
    int responseCode = HttpStatus.NO_CONTENT_204;
    CountDownLatch readLatch = new CountDownLatch(content.length());
    CountDownLatch errorLatch = new CountDownLatch(1);
    start(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext asyncContext = request.startAsync();
            ServletInputStream input = request.getInputStream();
            input.setReadListener(new ReadListener() {

                @Override
                public void onDataAvailable() throws IOException {
                    while (input.isReady() && !input.isFinished()) {
                        int read = input.read();
                        // System.err.printf("%x%n", read);
                        readLatch.countDown();
                    }
                }

                @Override
                public void onAllDataRead() throws IOException {
                }

                @Override
                public void onError(Throwable x) {
                    response.setStatus(responseCode);
                    asyncContext.complete();
                    errorLatch.countDown();
                }
            });
        }
    });
    CountDownLatch responseLatch = new CountDownLatch(1);
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    contentProvider.offer(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
    org.eclipse.jetty.client.api.Request request = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).onResponseSuccess(response -> responseLatch.countDown());
    Destination destination = client.getDestination(getScheme(), "localhost", connector.getLocalPort());
    FuturePromise<org.eclipse.jetty.client.api.Connection> promise = new FuturePromise<>();
    destination.newConnection(promise);
    org.eclipse.jetty.client.api.Connection connection = promise.get(5, TimeUnit.SECONDS);
    CountDownLatch clientLatch = new CountDownLatch(1);
    connection.send(request, result -> {
        assertThat(result.getResponse().getStatus(), Matchers.equalTo(responseCode));
        clientLatch.countDown();
    });
    assertTrue(readLatch.await(5, TimeUnit.SECONDS));
    switch(transport) {
        case HTTP:
        case HTTPS:
            ((HttpConnectionOverHTTP) connection).getEndPoint().shutdownOutput();
            break;
        case H2C:
        case H2:
            // In case of HTTP/2, we not only send the request, but also the preface and
            // SETTINGS frames. SETTINGS frame need to be replied, so we want to wait to
            // write the reply before shutting output down, so that the test does not fail.
            Thread.sleep(1000);
            Session session = ((HttpConnectionOverHTTP2) connection).getSession();
            ((HTTP2Session) session).getEndPoint().shutdownOutput();
            break;
        default:
            Assert.fail();
    }
    // Wait for the response to arrive before finishing the request.
    assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
    contentProvider.close();
    assertTrue(errorLatch.await(5, TimeUnit.SECONDS));
    assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) AsyncContext(javax.servlet.AsyncContext) HttpConnectionOverHTTP2(org.eclipse.jetty.http2.client.http.HttpConnectionOverHTTP2) Matchers.containsString(org.hamcrest.Matchers.containsString) ReadListener(javax.servlet.ReadListener) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpServlet(javax.servlet.http.HttpServlet) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.io.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) UncheckedIOException(java.io.UncheckedIOException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 59 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class JDK9HTTP2Client method main.

public static void main(String[] args) throws Exception {
    HTTP2Client client = new HTTP2Client();
    SslContextFactory sslContextFactory = new SslContextFactory(true);
    client.addBean(sslContextFactory);
    client.start();
    String host = "localhost";
    int port = 8443;
    FuturePromise<Session> sessionPromise = new FuturePromise<>();
    client.connect(sslContextFactory, new InetSocketAddress(host, port), new Session.Listener.Adapter(), sessionPromise);
    Session session = sessionPromise.get(555, TimeUnit.SECONDS);
    HttpFields requestFields = new HttpFields();
    requestFields.put("User-Agent", client.getClass().getName() + "/" + Jetty.VERSION);
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI("https://" + host + ":" + port + "/"), HttpVersion.HTTP_2, requestFields);
    HeadersFrame headersFrame = new HeadersFrame(metaData, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(headersFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            System.err.println(frame);
            if (frame.isEndStream())
                latch.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            System.err.println(frame);
            callback.succeeded();
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    latch.await(5, TimeUnit.SECONDS);
    client.stop();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Client(org.eclipse.jetty.http2.client.HTTP2Client) Stream(org.eclipse.jetty.http2.api.Stream) Session(org.eclipse.jetty.http2.api.Session)

Example 60 with Session

use of com.developmentontheedge.be5.api.Session in project jetty.project by eclipse.

the class TrailersTest method testTrailersSentByServer.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            HttpFields responseFields = new HttpFields();
            responseFields.put("X-Response", "true");
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, HttpStatus.OK_200, responseFields);
            HeadersFrame responseFrame = new HeadersFrame(stream.getId(), response, null, false);
            stream.headers(responseFrame, new Callback() {

                @Override
                public void succeeded() {
                    HttpFields trailerFields = new HttpFields();
                    trailerFields.put("X-Trailer", "true");
                    MetaData trailer = new MetaData(HttpVersion.HTTP_2, trailerFields);
                    HeadersFrame trailerFrame = new HeadersFrame(stream.getId(), trailer, null, true);
                    stream.headers(trailerFrame, NOOP);
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request request = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(request, null, true);
    CountDownLatch latch = new CountDownLatch(1);
    session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        private boolean responded;

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (!responded) {
                MetaData.Response response = (MetaData.Response) frame.getMetaData();
                Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
                Assert.assertTrue(response.getFields().containsKey("X-Response"));
                Assert.assertFalse(frame.isEndStream());
                responded = true;
            } else {
                MetaData trailer = frame.getMetaData();
                Assert.assertTrue(trailer.getFields().containsKey("X-Trailer"));
                Assert.assertTrue(frame.isEndStream());
                latch.countDown();
            }
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) 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) ServletInputStream(javax.servlet.ServletInputStream) 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)

Aggregations

Session (org.eclipse.jetty.http2.api.Session)101 CountDownLatch (java.util.concurrent.CountDownLatch)93 Test (org.junit.Test)93 HttpFields (org.eclipse.jetty.http.HttpFields)89 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)89 Stream (org.eclipse.jetty.http2.api.Stream)88 MetaData (org.eclipse.jetty.http.MetaData)86 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)77 FuturePromise (org.eclipse.jetty.util.FuturePromise)74 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)51 Callback (org.eclipse.jetty.util.Callback)51 Promise (org.eclipse.jetty.util.Promise)51 HttpServletResponse (javax.servlet.http.HttpServletResponse)47 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)39 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)32 HttpServlet (javax.servlet.http.HttpServlet)30 ISession (org.eclipse.jetty.http2.ISession)26 ByteBuffer (java.nio.ByteBuffer)24