Search in sources :

Example 1 with ByteBufferContentProvider

use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.

the class HttpClientRedirectTest method test_307_WithRequestContent.

@Test
public void test_307_WithRequestContent() throws Exception {
    start(new RedirectHandler());
    byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
    ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).path("/307/localhost/done").content(new ByteBufferContentProvider(ByteBuffer.wrap(data))).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertFalse(response.getHeaders().containsKey(HttpHeader.LOCATION.asString()));
    Assert.assertArrayEquals(data, response.getContent());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) Test(org.junit.Test)

Example 2 with ByteBufferContentProvider

use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnContent.

@Test
public void testAbortOnContent() throws Exception {
    try (StacklessLogging suppressor = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
        CountDownLatch serverLatch = new CountDownLatch(1);
        start(new EmptyServerHandler() {

            @Override
            public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                try {
                    super.handle(target, baseRequest, request, response);
                    if (request.getDispatcherType() != DispatcherType.ERROR)
                        IO.copy(request.getInputStream(), response.getOutputStream());
                } finally {
                    serverLatch.countDown();
                }
            }
        });
        final Throwable cause = new Exception();
        final AtomicBoolean aborted = new AtomicBoolean();
        final CountDownLatch latch = new CountDownLatch(1);
        try {
            client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestContent((request, content) -> {
                aborted.set(request.abort(cause));
                latch.countDown();
            }).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {

                @Override
                public long getLength() {
                    return -1;
                }
            }).timeout(5, TimeUnit.SECONDS).send();
            Assert.fail();
        } catch (ExecutionException x) {
            Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
            if (aborted.get())
                Assert.assertSame(cause, x.getCause());
        }
        Assert.assertTrue(serverLatch.await(5, TimeUnit.SECONDS));
        HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
        DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
        Assert.assertEquals(0, connectionPool.getConnectionCount());
        Assert.assertEquals(0, connectionPool.getActiveConnections().size());
        Assert.assertEquals(0, connectionPool.getIdleConnections().size());
    }
}
Also used : Result(org.eclipse.jetty.client.api.Result) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Test(org.junit.Test) Request(org.eclipse.jetty.client.api.Request) IO(org.eclipse.jetty.util.IO) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) DispatcherType(javax.servlet.DispatcherType) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with ByteBufferContentProvider

use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection.

@Test
public void test_BigRequestContent_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(HttpConnection.class)) {
        start(new AbstractHandler() {

            @Override
            public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                response.setHeader("Connection", "close");
                baseRequest.setHandled(true);
            // Don't read request content; this causes the server parser to be closed
            }
        });
        String host = "localhost";
        int port = connector.getLocalPort();
        HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
        DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
        final Collection<Connection> idleConnections = connectionPool.getIdleConnections();
        Assert.assertEquals(0, idleConnections.size());
        final Collection<Connection> activeConnections = connectionPool.getActiveConnections();
        Assert.assertEquals(0, activeConnections.size());
        Log.getLogger(HttpConnection.class).info("Expecting java.lang.IllegalStateException: HttpParser{s=CLOSED,...");
        final CountDownLatch latch = new CountDownLatch(1);
        ByteBuffer buffer = ByteBuffer.allocate(16 * 1024 * 1024);
        Arrays.fill(buffer.array(), (byte) 'x');
        client.newRequest(host, port).scheme(scheme).content(new ByteBufferContentProvider(buffer)).send(new Response.Listener.Adapter() {

            @Override
            public void onComplete(Result result) {
                Assert.assertEquals(1, latch.getCount());
                Assert.assertEquals(0, idleConnections.size());
                Assert.assertEquals(0, activeConnections.size());
                latch.countDown();
            }
        });
        Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
        Assert.assertEquals(0, idleConnections.size());
        Assert.assertEquals(0, activeConnections.size());
        server.stop();
    }
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 4 with ByteBufferContentProvider

use of org.eclipse.jetty.client.util.ByteBufferContentProvider in project jetty.project by eclipse.

the class HttpRequestAbortTest method testAbortOnCommitWithContent.

@Test
public void testAbortOnCommitWithContent() throws Exception {
    final AtomicReference<IOException> failure = new AtomicReference<>();
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            try {
                baseRequest.setHandled(true);
                if (request.getDispatcherType() != DispatcherType.ERROR)
                    IO.copy(request.getInputStream(), response.getOutputStream());
            } catch (IOException x) {
                failure.set(x);
                throw x;
            }
        }
    });
    final Throwable cause = new Exception();
    final AtomicBoolean aborted = new AtomicBoolean();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).onRequestCommit(request -> {
            aborted.set(request.abort(cause));
            latch.countDown();
        }).content(new ByteBufferContentProvider(ByteBuffer.wrap(new byte[] { 0 }), ByteBuffer.wrap(new byte[] { 1 })) {

            @Override
            public long getLength() {
                return -1;
            }
        }).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        if (aborted.get())
            Assert.assertSame(cause, x.getCause());
    }
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, "localhost", connector.getLocalPort());
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Result(org.eclipse.jetty.client.api.Result) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Test(org.junit.Test) Request(org.eclipse.jetty.client.api.Request) IO(org.eclipse.jetty.util.IO) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServletRequest(javax.servlet.http.HttpServletRequest) DispatcherType(javax.servlet.DispatcherType) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) HttpServletResponse(javax.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ByteBufferContentProvider(org.eclipse.jetty.client.util.ByteBufferContentProvider) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 5 with ByteBufferContentProvider

use of org.eclipse.jetty.client.util.ByteBufferContentProvider 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)

Aggregations

ByteBufferContentProvider (org.eclipse.jetty.client.util.ByteBufferContentProvider)8 Test (org.junit.Test)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Request (org.eclipse.jetty.client.api.Request)5 ByteBuffer (java.nio.ByteBuffer)4 Connection (org.eclipse.jetty.client.api.Connection)4 Result (org.eclipse.jetty.client.api.Result)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Origin (org.eclipse.jetty.client.Origin)3 HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)3 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)3 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)3 Promise (org.eclipse.jetty.util.Promise)3 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)3 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2