Search in sources :

Example 6 with ByteArrayEndPoint

use of org.eclipse.jetty.io.ByteArrayEndPoint in project jetty.project by eclipse.

the class SslConnectionTest method testSslConnectionClosedBeforeFill.

@Test
public void testSslConnectionClosedBeforeFill() throws Exception {
    File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
    sslContextFactory.setKeyStorePassword("storepwd");
    sslContextFactory.start();
    ByteBufferPool byteBufferPool = new MappedByteBufferPool();
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.start();
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    SSLEngine sslEngine = sslContextFactory.newSSLEngine();
    sslEngine.setUseClientMode(false);
    SslConnection sslConnection = new SslConnection(byteBufferPool, threadPool, endPoint, sslEngine);
    EndPoint sslEndPoint = sslConnection.getDecryptedEndPoint();
    sslEndPoint.setConnection(new AbstractConnection(sslEndPoint, threadPool) {

        @Override
        public void onFillable() {
        }
    });
    // There are no bytes in the endPoint, so we fill zero.
    // However, this will trigger state changes in SSLEngine
    // that will later cause it to throw ISE("Internal error").
    sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
    // Close the connection before filling.
    sslEndPoint.shutdownOutput();
    // Put some bytes in the endPoint to trigger
    // the required state changes in SSLEngine.
    byte[] bytes = new byte[] { 0x16, 0x03, 0x03, 0x00, 0x00 };
    endPoint.addInput(ByteBuffer.wrap(bytes));
    // reads from the EndPoint.
    try {
        sslEndPoint.fill(BufferUtil.EMPTY_BUFFER);
        Assert.fail();
    } catch (SSLHandshakeException x) {
    // Expected.
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SSLEngine(javax.net.ssl.SSLEngine) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) EndPoint(org.eclipse.jetty.io.EndPoint) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) MappedByteBufferPool(org.eclipse.jetty.io.MappedByteBufferPool) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) File(java.io.File) Test(org.junit.Test)

Example 7 with ByteArrayEndPoint

use of org.eclipse.jetty.io.ByteArrayEndPoint in project jetty.project by eclipse.

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent_IncompleteFlush_Exception.

@Test
public void test_Send_NoRequestContent_IncompleteFlush_Exception() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint("", 16);
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
    Request request = client.newRequest(URI.create("http://localhost/"));
    final CountDownLatch failureLatch = new CountDownLatch(2);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onFailure(Request request, Throwable x) {
            failureLatch.countDown();
        }
    });
    connection.send(request, new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            failureLatch.countDown();
        }
    });
    // Shutdown output to trigger the exception on write
    endPoint.shutdownOutput();
    // This take will free space in the buffer and allow for the write to complete
    // although it will fail because we shut down the output
    endPoint.takeOutputString();
    Assert.assertTrue(failureLatch.await(5, TimeUnit.SECONDS));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Promise(org.eclipse.jetty.util.Promise) Test(org.junit.Test)

Example 8 with ByteArrayEndPoint

use of org.eclipse.jetty.io.ByteArrayEndPoint in project jetty.project by eclipse.

the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_Chunked_InTwoChunks.

@Test
public void test_Send_SmallRequestContent_Chunked_InTwoChunks() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
    Request request = client.newRequest(URI.create("http://localhost/"));
    String content1 = "0123456789";
    String content2 = "ABCDEF";
    request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))) {

        @Override
        public long getLength() {
            return -1;
        }
    });
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onHeaders(Request request) {
            headersLatch.countDown();
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    });
    connection.send(request, null);
    String requestString = endPoint.takeOutputString();
    Assert.assertTrue(requestString.startsWith("GET "));
    String content = Integer.toHexString(content1.length()).toUpperCase(Locale.ENGLISH) + "\r\n" + content1 + "\r\n";
    content += Integer.toHexString(content2.length()).toUpperCase(Locale.ENGLISH) + "\r\n" + content2 + "\r\n";
    content += "0\r\n\r\n";
    Assert.assertTrue(requestString.endsWith("\r\n\r\n" + content));
    Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(org.eclipse.jetty.util.Promise) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Example 9 with ByteArrayEndPoint

use of org.eclipse.jetty.io.ByteArrayEndPoint in project jetty.project by eclipse.

the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_InTwoBuffers.

@Test
public void test_Send_SmallRequestContent_InTwoBuffers() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
    Request request = client.newRequest(URI.create("http://localhost/"));
    String content1 = "0123456789";
    String content2 = "abcdef";
    request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content1.getBytes(StandardCharsets.UTF_8)), ByteBuffer.wrap(content2.getBytes(StandardCharsets.UTF_8))));
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onHeaders(Request request) {
            headersLatch.countDown();
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    });
    connection.send(request, null);
    String requestString = endPoint.takeOutputString();
    Assert.assertTrue(requestString.startsWith("GET "));
    Assert.assertThat(requestString, Matchers.endsWith("\r\n\r\n" + content1 + content2));
    Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(org.eclipse.jetty.util.Promise) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Example 10 with ByteArrayEndPoint

use of org.eclipse.jetty.io.ByteArrayEndPoint in project jetty.project by eclipse.

the class HttpSenderOverHTTPTest method test_Send_SmallRequestContent_InOneBuffer.

@Test
public void test_Send_SmallRequestContent_InOneBuffer() throws Exception {
    ByteArrayEndPoint endPoint = new ByteArrayEndPoint();
    HttpDestinationOverHTTP destination = new HttpDestinationOverHTTP(client, new Origin("http", "localhost", 8080));
    destination.start();
    HttpConnectionOverHTTP connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<Connection>());
    Request request = client.newRequest(URI.create("http://localhost/"));
    String content = "abcdef";
    request.content(new ByteBufferContentProvider(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8))));
    final CountDownLatch headersLatch = new CountDownLatch(1);
    final CountDownLatch successLatch = new CountDownLatch(1);
    request.listener(new Request.Listener.Adapter() {

        @Override
        public void onHeaders(Request request) {
            headersLatch.countDown();
        }

        @Override
        public void onSuccess(Request request) {
            successLatch.countDown();
        }
    });
    connection.send(request, null);
    String requestString = endPoint.takeOutputString();
    Assert.assertTrue(requestString.startsWith("GET "));
    Assert.assertTrue(requestString.endsWith("\r\n\r\n" + content));
    Assert.assertTrue(headersLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(successLatch.await(5, TimeUnit.SECONDS));
}
Also used : Origin(org.eclipse.jetty.client.Origin) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) ByteArrayEndPoint(org.eclipse.jetty.io.ByteArrayEndPoint) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(org.eclipse.jetty.util.Promise) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Aggregations

ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)10 Origin (org.eclipse.jetty.client.Origin)8 Test (org.junit.Test)8 Connection (org.eclipse.jetty.client.api.Connection)7 Request (org.eclipse.jetty.client.api.Request)7 Promise (org.eclipse.jetty.util.Promise)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 ByteBufferContentProvider (org.eclipse.jetty.client.util.ByteBufferContentProvider)3 Result (org.eclipse.jetty.client.api.Result)2 Before (org.junit.Before)2 File (java.io.File)1 ByteBuffer (java.nio.ByteBuffer)1 SSLEngine (javax.net.ssl.SSLEngine)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpClient (org.eclipse.jetty.client.HttpClient)1 AbstractConnection (org.eclipse.jetty.io.AbstractConnection)1 AbstractEndPoint (org.eclipse.jetty.io.AbstractEndPoint)1 ByteBufferPool (org.eclipse.jetty.io.ByteBufferPool)1