Search in sources :

Example 76 with Session

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

the class SessionFailureTest method testWrongPreface.

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

        @Override
        public void onFailure(Session session, Throwable failure) {
            latch.countDown();
        }
    });
    try (Socket socket = new Socket("localhost", connector.getLocalPort())) {
        // Preface starts with byte 0x50, send something different.
        OutputStream output = socket.getOutputStream();
        output.write(0x0);
        output.flush();
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        // The server will reply with a GOAWAY frame, and then shutdown.
        // Read until EOF.
        socket.setSoTimeout(1000);
        InputStream input = socket.getInputStream();
        while (true) {
            if (input.read() < 0)
                break;
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Socket(java.net.Socket) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 77 with Session

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

the class SessionFailureTest method testWriteFailure.

@Test
public void testWriteFailure() throws Exception {
    final CountDownLatch writeLatch = new CountDownLatch(1);
    final CountDownLatch serverFailureLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            // Forcibly close the connection.
            ((HTTP2Session) stream.getSession()).getEndPoint().close();
            // Now try to write something: it should fail.
            stream.headers(frame, new Callback() {

                @Override
                public void failed(Throwable x) {
                    writeLatch.countDown();
                }
            });
            return null;
        }

        @Override
        public void onFailure(Session session, Throwable failure) {
            serverFailureLatch.countDown();
        }
    });
    final CountDownLatch clientFailureLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onFailure(Session session, Throwable failure) {
            clientFailureLatch.countDown();
        }
    });
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, true);
    Promise<Stream> promise = new Promise.Adapter<>();
    session.newStream(frame, promise, null);
    Assert.assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(serverFailureLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(clientFailureLatch.await(5, TimeUnit.SECONDS));
    long start = System.nanoTime();
    long now = System.nanoTime();
    while (((HTTP2Session) session).getEndPoint().isOpen()) {
        if (TimeUnit.NANOSECONDS.toSeconds(now - start) > 5)
            Assert.fail();
        Thread.sleep(10);
        now = System.nanoTime();
    }
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Callback(org.eclipse.jetty.util.Callback) HttpFields(org.eclipse.jetty.http.HttpFields) OutputStream(java.io.OutputStream) Stream(org.eclipse.jetty.http2.api.Stream) InputStream(java.io.InputStream) 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 78 with Session

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

the class StreamCloseTest method testRequestClosedResponseClosedClosesStream.

@Test
public void testRequestClosedResponseClosedClosesStream() throws Exception {
    final CountDownLatch latch = new CountDownLatch(2);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(final Stream stream, HeadersFrame frame) {
            MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            HeadersFrame response = new HeadersFrame(stream.getId(), metaData, null, true);
            stream.headers(response, new Callback() {

                @Override
                public void succeeded() {
                    Assert.assertTrue(stream.isClosed());
                    Assert.assertEquals(0, stream.getSession().getStreams().size());
                    latch.countDown();
                }
            });
            return null;
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    HeadersFrame frame = new HeadersFrame(newRequest("GET", new HttpFields()), null, true);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(frame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            // The stream promise may not be notified yet here.
            latch.countDown();
        }
    });
    Stream stream = promise.get(5, TimeUnit.SECONDS);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(stream.isClosed());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Callback(org.eclipse.jetty.util.Callback) 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) 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 79 with Session

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

the class ProxyProtocolTest method test_PROXY_GET_v2.

@Test
public void test_PROXY_GET_v2() throws Exception {
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                Assert.assertEquals("10.0.0.4", request.getRemoteAddr());
                Assert.assertEquals(33824, request.getRemotePort());
                Assert.assertEquals("10.0.0.4", request.getLocalAddr());
                Assert.assertEquals(8888, request.getLocalPort());
            } catch (Throwable th) {
                th.printStackTrace();
                response.setStatus(500);
            }
            baseRequest.setHandled(true);
        }
    });
    String request1 = "0D0A0D0A000D0A515549540A211100140A0000040A000004842022B82000050000000000";
    SocketChannel channel = SocketChannel.open();
    channel.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
    channel.write(ByteBuffer.wrap(TypeUtil.fromHexString(request1)));
    FuturePromise<Session> promise = new FuturePromise<>();
    client.accept(null, channel, new Session.Listener.Adapter(), promise);
    Session session = promise.get(5, TimeUnit.SECONDS);
    HttpFields fields = new HttpFields();
    String uri = "http://localhost:" + connector.getLocalPort() + "/";
    MetaData.Request metaData = new MetaData.Request("GET", new HttpURI(uri), HttpVersion.HTTP_2, fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, true);
    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(HttpStatus.OK_200, response.getStatus());
            if (frame.isEndStream())
                latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) 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) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpURI(org.eclipse.jetty.http.HttpURI) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 80 with Session

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

the class ProxyTest method testServerBigDownloadSlowClient.

@Test
public void testServerBigDownloadSlowClient() throws Exception {
    final CountDownLatch serverLatch = new CountDownLatch(1);
    final byte[] content = new byte[1024 * 1024];
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.getOutputStream().write(content);
            serverLatch.countDown();
        }
    });
    Map<String, String> params = new HashMap<>();
    params.put("proxyTo", "http://localhost:" + serverConnector.getLocalPort());
    startProxy(new AsyncProxyServlet.Transparent() {

        @Override
        protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
            proxyRequest.version(HttpVersion.HTTP_1_1);
            super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
        }
    }, params);
    startClient();
    final CountDownLatch clientLatch = new CountDownLatch(1);
    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) {
            try {
                TimeUnit.MILLISECONDS.sleep(1);
                callback.succeeded();
                if (frame.isEndStream())
                    clientLatch.countDown();
            } catch (InterruptedException x) {
                callback.failed(x);
            }
        }
    });
    Assert.assertTrue(serverLatch.await(15, TimeUnit.SECONDS));
    Assert.assertTrue(clientLatch.await(15, TimeUnit.SECONDS));
}
Also used : HashMap(java.util.HashMap) 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) HttpServlet(javax.servlet.http.HttpServlet) Request(org.eclipse.jetty.client.api.Request) 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) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) Callback(org.eclipse.jetty.util.Callback) AsyncProxyServlet(org.eclipse.jetty.proxy.AsyncProxyServlet) 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