Search in sources :

Example 6 with HttpDestinationOverHTTP

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

the class ServerConnectionCloseTest method testServerSendsConnectionClose.

private void testServerSendsConnectionClose(boolean shutdownOutput, boolean chunked, String content) throws Exception {
    ServerSocket server = new ServerSocket(0);
    int port = server.getLocalPort();
    startClient();
    Request request = client.newRequest("localhost", port).path("/ctx/path");
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    Socket socket = server.accept();
    InputStream input = socket.getInputStream();
    consumeRequest(input);
    OutputStream output = socket.getOutputStream();
    String serverResponse = "" + "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n";
    if (chunked) {
        serverResponse += "" + "Transfer-Encoding: chunked\r\n" + "\r\n";
        for (int i = 0; i < 2; ++i) {
            serverResponse += Integer.toHexString(content.length()) + "\r\n" + content + "\r\n";
        }
        serverResponse += "" + "0\r\n" + "\r\n";
    } else {
        serverResponse += "Content-Length: " + content.length() + "\r\n";
        serverResponse += "\r\n";
        serverResponse += content;
    }
    output.write(serverResponse.getBytes("UTF-8"));
    output.flush();
    if (shutdownOutput)
        socket.shutdownOutput();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    // Give some time to process the connection.
    Thread.sleep(1000);
    // Connection should have been removed from pool.
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getIdleConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnectionCount());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 7 with HttpDestinationOverHTTP

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

the class TLSServerConnectionCloseTest method testServerSendsConnectionClose.

private void testServerSendsConnectionClose(boolean chunked, String content) throws Exception {
    ServerSocket server = new ServerSocket(0);
    int port = server.getLocalPort();
    startClient();
    Request request = client.newRequest("localhost", port).scheme("https").path("/ctx/path");
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    Socket socket = server.accept();
    SSLContext sslContext = client.getSslContextFactory().getSslContext();
    SSLSocket sslSocket = (SSLSocket) sslContext.getSocketFactory().createSocket(socket, "localhost", port, false);
    sslSocket.setUseClientMode(false);
    sslSocket.startHandshake();
    InputStream input = sslSocket.getInputStream();
    consumeRequest(input);
    OutputStream output = sslSocket.getOutputStream();
    String serverResponse = "" + "HTTP/1.1 200 OK\r\n" + "Connection: close\r\n";
    if (chunked) {
        serverResponse += "" + "Transfer-Encoding: chunked\r\n" + "\r\n";
        for (int i = 0; i < 2; ++i) {
            serverResponse += Integer.toHexString(content.length()) + "\r\n" + content + "\r\n";
        }
        serverResponse += "" + "0\r\n" + "\r\n";
    } else {
        serverResponse += "Content-Length: " + content.length() + "\r\n";
        serverResponse += "\r\n";
        serverResponse += content;
    }
    output.write(serverResponse.getBytes("UTF-8"));
    output.flush();
    switch(closeMode) {
        case NONE:
            {
                break;
            }
        case CLOSE:
            {
                sslSocket.close();
                break;
            }
        case ABRUPT:
            {
                socket.shutdownOutput();
                break;
            }
        default:
            {
                throw new IllegalStateException();
            }
    }
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    // Give some time to process the connection.
    Thread.sleep(1000);
    // Connection should have been removed from pool.
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    Assert.assertEquals(0, connectionPool.getConnectionCount());
    Assert.assertEquals(0, connectionPool.getIdleConnectionCount());
    Assert.assertEquals(0, connectionPool.getActiveConnectionCount());
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) ServerSocket(java.net.ServerSocket) SSLContext(javax.net.ssl.SSLContext) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 8 with HttpDestinationOverHTTP

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

Example 9 with HttpDestinationOverHTTP

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

the class HttpClientTimeoutTest method testIdleTimeout.

@Test
public void testIdleTimeout() throws Throwable {
    long timeout = 1000;
    start(new TimeoutHandler(2 * timeout));
    client.stop();
    final AtomicBoolean sslIdle = new AtomicBoolean();
    client = new HttpClient(new HttpClientTransportOverHTTP() {

        @Override
        public HttpDestination newHttpDestination(Origin origin) {
            return new HttpDestinationOverHTTP(getHttpClient(), origin) {

                @Override
                protected ClientConnectionFactory newSslClientConnectionFactory(ClientConnectionFactory connectionFactory) {
                    HttpClient client = getHttpClient();
                    return new SslClientConnectionFactory(client.getSslContextFactory(), client.getByteBufferPool(), client.getExecutor(), connectionFactory) {

                        @Override
                        protected SslConnection newSslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint endPoint, SSLEngine engine) {
                            return new SslConnection(byteBufferPool, executor, endPoint, engine) {

                                @Override
                                protected boolean onReadTimeout() {
                                    sslIdle.set(true);
                                    return super.onReadTimeout();
                                }
                            };
                        }
                    };
                }
            };
        }
    }, sslContextFactory);
    client.setIdleTimeout(timeout);
    client.start();
    try {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).send();
        Assert.fail();
    } catch (Exception x) {
        Assert.assertFalse(sslIdle.get());
        Assert.assertThat(x.getCause(), Matchers.instanceOf(TimeoutException.class));
    }
}
Also used : ByteBufferPool(org.eclipse.jetty.io.ByteBufferPool) SSLEngine(javax.net.ssl.SSLEngine) SslClientConnectionFactory(org.eclipse.jetty.io.ssl.SslClientConnectionFactory) HttpClientTransportOverHTTP(org.eclipse.jetty.client.http.HttpClientTransportOverHTTP) ClientConnectionFactory(org.eclipse.jetty.io.ClientConnectionFactory) SslClientConnectionFactory(org.eclipse.jetty.io.ssl.SslClientConnectionFactory) EndPoint(org.eclipse.jetty.io.EndPoint) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Executor(java.util.concurrent.Executor) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Example 10 with HttpDestinationOverHTTP

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

the class HttpRequestAbortTest method testAbortOnCommit.

@Test
public void testAbortOnCommit() throws Exception {
    start(new EmptyServerHandler());
    // Test can behave in 2 ways:
    // A) the request is failed before the response arrived
    // B) the request is failed after the response arrived
    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();
        }).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 : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

HttpDestinationOverHTTP (org.eclipse.jetty.client.http.HttpDestinationOverHTTP)30 Test (org.junit.Test)28 CountDownLatch (java.util.concurrent.CountDownLatch)23 HttpServletRequest (javax.servlet.http.HttpServletRequest)22 IOException (java.io.IOException)21 ServletException (javax.servlet.ServletException)21 HttpServletResponse (javax.servlet.http.HttpServletResponse)19 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)18 Request (org.eclipse.jetty.client.api.Request)17 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)14 Connection (org.eclipse.jetty.client.api.Connection)13 Result (org.eclipse.jetty.client.api.Result)12 ByteBuffer (java.nio.ByteBuffer)11 TimeUnit (java.util.concurrent.TimeUnit)11 Assert (org.junit.Assert)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)10 Response (org.eclipse.jetty.client.api.Response)9 ExecutionException (java.util.concurrent.ExecutionException)8 HttpConnectionOverHTTP (org.eclipse.jetty.client.http.HttpConnectionOverHTTP)8