Search in sources :

Example 1 with HttpConnectionOverHTTP

use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.

the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange.

@Test
public void test_ClientConnectionClose_ServerConnectionClose_ClientClosesAfterExchange() throws Exception {
    byte[] data = new byte[128 * 1024];
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletInputStream input = request.getInputStream();
            while (true) {
                int read = input.read();
                if (read < 0)
                    break;
            }
            response.setContentLength(data.length);
            response.getOutputStream().write(data);
            try {
                // Delay the server from sending the TCP FIN.
                Thread.sleep(1000);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).content(new StringContentProvider("0")).onRequestSuccess(request -> {
        HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
        Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
    }).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
    Assert.assertEquals(0, connectionPool.getConnectionCount());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) InterruptedIOException(java.io.InterruptedIOException) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InterruptedIOException(java.io.InterruptedIOException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 2 with HttpConnectionOverHTTP

use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.

the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerNoConnectionClose_ClientCloses.

@Test
public void test_ClientConnectionClose_ServerNoConnectionClose_ClientCloses() throws Exception {
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setContentLength(0);
            response.flushBuffer();
            try {
                // Delay the server from sending the TCP FIN.
                Thread.sleep(1000);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    ContentResponse response = client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).onRequestSuccess(request -> {
        HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
        Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
    }).onResponseHeaders(r -> r.getHeaders().remove(HttpHeader.CONNECTION)).send();
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(0, connectionPool.getConnectionCount());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) InterruptedIOException(java.io.InterruptedIOException) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InterruptedIOException(java.io.InterruptedIOException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 3 with HttpConnectionOverHTTP

use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.

the class HttpClientExplicitConnectionTest method testExplicitConnectionIsClosedOnRemoteClose.

@Test
public void testExplicitConnectionIsClosedOnRemoteClose() throws Exception {
    start(new EmptyServerHandler());
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    FuturePromise<Connection> futureConnection = new FuturePromise<>();
    destination.newConnection(futureConnection);
    Connection connection = futureConnection.get(5, TimeUnit.SECONDS);
    Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(scheme);
    FutureResponseListener listener = new FutureResponseListener(request);
    connection.send(request, listener);
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    // Wait some time to have the client is an idle state.
    TimeUnit.SECONDS.sleep(1);
    connector.stop();
    // Give the connection some time to process the remote close.
    TimeUnit.SECONDS.sleep(1);
    HttpConnectionOverHTTP httpConnection = (HttpConnectionOverHTTP) connection;
    Assert.assertFalse(httpConnection.getEndPoint().isOpen());
    HttpDestinationOverHTTP httpDestination = (HttpDestinationOverHTTP) destination;
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) httpDestination.getConnectionPool();
    Assert.assertTrue(connectionPool.getActiveConnections().isEmpty());
    Assert.assertTrue(connectionPool.getIdleConnections().isEmpty());
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 4 with HttpConnectionOverHTTP

use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.

the class HttpClientFailureTest method testFailureBeforeRequestCommit.

@Test
public void testFailureBeforeRequestCommit() throws Exception {
    startServer(new EmptyServerHandler());
    final AtomicReference<HttpConnectionOverHTTP> connectionRef = new AtomicReference<>();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        protected HttpConnectionOverHTTP newHttpConnection(EndPoint endPoint, HttpDestination destination, Promise<Connection> promise) {
            HttpConnectionOverHTTP connection = super.newHttpConnection(endPoint, destination, promise);
            connectionRef.set(connection);
            return connection;
        }
    }, null);
    client.start();
    try {
        client.newRequest("localhost", connector.getLocalPort()).onRequestHeaders(request -> connectionRef.get().getEndPoint().close()).timeout(5, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected.
    }
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) connectionRef.get().getHttpDestination().getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnections().size());
    Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Also used : Promise(org.eclipse.jetty.util.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) EndPoint(org.eclipse.jetty.io.EndPoint) ExecutionException(java.util.concurrent.ExecutionException) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Test(org.junit.Test)

Example 5 with HttpConnectionOverHTTP

use of org.eclipse.jetty.client.http.HttpConnectionOverHTTP in project jetty.project by eclipse.

the class HttpClientTest method testStoppingClosesConnections.

@Test
public void testStoppingClosesConnections() throws Exception {
    start(new EmptyServerHandler());
    String host = "localhost";
    int port = connector.getLocalPort();
    String path = "/";
    Response response = client.GET(scheme + "://" + host + ":" + port + path);
    Assert.assertEquals(200, response.getStatus());
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    long start = System.nanoTime();
    HttpConnectionOverHTTP connection = null;
    while (connection == null && TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - start) < 5) {
        connection = (HttpConnectionOverHTTP) connectionPool.getIdleConnections().peek();
        TimeUnit.MILLISECONDS.sleep(10);
    }
    Assert.assertNotNull(connection);
    String uri = destination.getScheme() + "://" + destination.getHost() + ":" + destination.getPort();
    client.getCookieStore().add(URI.create(uri), new HttpCookie("foo", "bar"));
    client.stop();
    Assert.assertEquals(0, client.getDestinations().size());
    Assert.assertEquals(0, connectionPool.getIdleConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnectionCount());
    Assert.assertFalse(connection.getEndPoint().isOpen());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) HttpCookie(java.net.HttpCookie) EndPoint(org.eclipse.jetty.io.EndPoint) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) Test(org.junit.Test)

Aggregations

HttpConnectionOverHTTP (org.eclipse.jetty.client.http.HttpConnectionOverHTTP)10 Test (org.junit.Test)10 HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 TimeUnit (java.util.concurrent.TimeUnit)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)7 Assert (org.junit.Assert)7 IOException (java.io.IOException)6 ByteBuffer (java.nio.ByteBuffer)6 ServletException (javax.servlet.ServletException)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)6 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)6 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)5 HttpHeader (org.eclipse.jetty.http.HttpHeader)5 HttpHeaderValue (org.eclipse.jetty.http.HttpHeaderValue)5 EndPoint (org.eclipse.jetty.io.EndPoint)5 Request (org.eclipse.jetty.server.Request)5 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)5