Search in sources :

Example 26 with Connection

use of org.eclipse.jetty.client.api.Connection 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 27 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_IdleConnection_IsClosed_OnRemoteClose.

@Slow
@Test
public void test_IdleConnection_IsClosed_OnRemoteClose() throws Exception {
    start(new EmptyServerHandler());
    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());
    ContentResponse response = client.newRequest(host, port).scheme(scheme).timeout(30, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    connector.stop();
    // Give the connection some time to process the remote close
    TimeUnit.SECONDS.sleep(1);
    Assert.assertEquals(0, idleConnections.size());
    Assert.assertEquals(0, activeConnections.size());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Connection(org.eclipse.jetty.client.api.Connection) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 28 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpConnectionLifecycleTest method test_ResponseWithConnectionCloseHeader_RemovesConnection.

@Test
public void test_ResponseWithConnectionCloseHeader_RemovesConnection() throws Exception {
    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);
        }
    });
    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());
    final CountDownLatch latch = new CountDownLatch(1);
    client.newRequest(host, port).scheme(scheme).send(new Response.Listener.Adapter() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            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());
}
Also used : Connection(org.eclipse.jetty.client.api.Connection) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) 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) Test(org.junit.Test)

Example 29 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpClientTransportOverFCGI method newConnection.

@Override
public org.eclipse.jetty.io.Connection newConnection(EndPoint endPoint, Map<String, Object> context) throws IOException {
    HttpDestination destination = (HttpDestination) context.get(HTTP_DESTINATION_CONTEXT_KEY);
    @SuppressWarnings("unchecked") Promise<Connection> promise = (Promise<Connection>) context.get(HTTP_CONNECTION_PROMISE_CONTEXT_KEY);
    HttpConnectionOverFCGI connection = newHttpConnection(endPoint, destination, promise);
    if (LOG.isDebugEnabled())
        LOG.debug("Created {}", connection);
    return customize(connection, context);
}
Also used : Promise(org.eclipse.jetty.util.Promise) Connection(org.eclipse.jetty.client.api.Connection) HttpDestination(org.eclipse.jetty.client.HttpDestination)

Example 30 with Connection

use of org.eclipse.jetty.client.api.Connection in project jetty.project by eclipse.

the class HttpSenderOverHTTPTest method test_Send_NoRequestContent.

@Test
public void test_Send_NoRequestContent() 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/"));
    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"));
    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) Test(org.junit.Test)

Aggregations

Connection (org.eclipse.jetty.client.api.Connection)40 Test (org.junit.Test)33 CountDownLatch (java.util.concurrent.CountDownLatch)23 Request (org.eclipse.jetty.client.api.Request)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)14 Origin (org.eclipse.jetty.client.Origin)13 Result (org.eclipse.jetty.client.api.Result)13 HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)13 Promise (org.eclipse.jetty.util.Promise)13 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 IOException (java.io.IOException)9 Destination (org.eclipse.jetty.client.api.Destination)9 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)9 ServletException (javax.servlet.ServletException)8 Response (org.eclipse.jetty.client.api.Response)8 TimeUnit (java.util.concurrent.TimeUnit)7 ByteBufferContentProvider (org.eclipse.jetty.client.util.ByteBufferContentProvider)7 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)7 ByteArrayEndPoint (org.eclipse.jetty.io.ByteArrayEndPoint)7