Search in sources :

Example 6 with FutureResponseListener

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

the class HttpClientTest method testHTTP10WithKeepAliveAndNoContentLength.

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

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // Send the headers at this point, then write the content
            response.flushBuffer();
            response.getOutputStream().print("TEST");
        }
    });
    FuturePromise<Connection> promise = new FuturePromise<>();
    Destination destination = client.getDestination(scheme, "localhost", connector.getLocalPort());
    destination.newConnection(promise);
    try (Connection connection = promise.get(5, TimeUnit.SECONDS)) {
        long timeout = 5000;
        Request request = client.newRequest(destination.getHost(), destination.getPort()).scheme(destination.getScheme()).version(HttpVersion.HTTP_1_0).header(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.asString()).timeout(timeout, TimeUnit.MILLISECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        ContentResponse response = listener.get(2 * timeout, TimeUnit.MILLISECONDS);
        Assert.assertEquals(200, response.getStatus());
        // The parser notifies end-of-content and therefore the CompleteListener
        // before closing the connection, so we need to wait before checking
        // that the connection is closed to avoid races.
        Thread.sleep(1000);
        Assert.assertTrue(connection.isClosed());
    }
}
Also used : Destination(org.eclipse.jetty.client.api.Destination) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 7 with FutureResponseListener

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

the class HttpClientTest method testCONNECTWithHTTP10.

@Test
public void testCONNECTWithHTTP10() throws Exception {
    try (ServerSocket server = new ServerSocket(0)) {
        startClient();
        String host = "localhost";
        int port = server.getLocalPort();
        Request request = client.newRequest(host, port).method(HttpMethod.CONNECT).version(HttpVersion.HTTP_1_0);
        FuturePromise<Connection> promise = new FuturePromise<>();
        client.getDestination("http", host, port).newConnection(promise);
        Connection connection = promise.get(5, TimeUnit.SECONDS);
        FutureResponseListener listener = new FutureResponseListener(request);
        connection.send(request, listener);
        try (Socket socket = server.accept()) {
            InputStream input = socket.getInputStream();
            consume(input, false);
            // HTTP/1.0 response, the client must not close the connection.
            String httpResponse = "" + "HTTP/1.0 200 OK\r\n" + "\r\n";
            OutputStream output = socket.getOutputStream();
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            ContentResponse response = listener.get(5, TimeUnit.SECONDS);
            Assert.assertEquals(200, response.getStatus());
            // Because the tunnel was successful, this connection will be
            // upgraded to an SslConnection, so it will not be fill interested.
            // This test doesn't upgrade, so it needs to restore the fill interest.
            ((AbstractConnection) connection).fillInterested();
            // Test that I can send another request on the same connection.
            request = client.newRequest(host, port);
            listener = new FutureResponseListener(request);
            connection.send(request, listener);
            consume(input, false);
            httpResponse = "" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n";
            output.write(httpResponse.getBytes(StandardCharsets.UTF_8));
            output.flush();
            listener.get(5, TimeUnit.SECONDS);
        }
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) FuturePromise(org.eclipse.jetty.util.FuturePromise) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) Connection(org.eclipse.jetty.client.api.Connection) ServerSocket(java.net.ServerSocket) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractConnection(org.eclipse.jetty.io.AbstractConnection) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 8 with FutureResponseListener

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

the class SslBytesClientTest method testServerRenegotiationWhenRenegotiationIsForbidden.

@Test
public void testServerRenegotiationWhenRenegotiationIsForbidden() throws Exception {
    sslContextFactory.setRenegotiationAllowed(false);
    Request request = client.newRequest("localhost", proxy.getPort());
    FutureResponseListener listener = new FutureResponseListener(request);
    request.scheme(HttpScheme.HTTPS.asString()).send(listener);
    Assert.assertTrue(proxy.awaitClient(5, TimeUnit.SECONDS));
    final SSLSocket server = (SSLSocket) acceptor.accept();
    server.setUseClientMode(false);
    Future<Object> handshake = threadPool.submit(() -> {
        server.startHandshake();
        return null;
    });
    SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow();
    Assert.assertNull(handshake.get(5, TimeUnit.SECONDS));
    // Read request
    InputStream serverInput = server.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, StandardCharsets.UTF_8));
    String line = reader.readLine();
    Assert.assertTrue(line.startsWith("GET"));
    while (line.length() > 0) line = reader.readLine();
    OutputStream serverOutput = server.getOutputStream();
    byte[] data1 = new byte[1024];
    Arrays.fill(data1, (byte) 'X');
    String content1 = new String(data1, StandardCharsets.UTF_8);
    byte[] data2 = new byte[1024];
    Arrays.fill(data2, (byte) 'Y');
    final String content2 = new String(data2, StandardCharsets.UTF_8);
    // Write first part of the response
    serverOutput.write(("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + content1).getBytes(StandardCharsets.UTF_8));
    serverOutput.flush();
    Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));
    // Renegotiate
    threadPool.submit(() -> {
        server.startHandshake();
        return null;
    });
    // Renegotiation Handshake
    TLSRecord record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToClient(record);
    // Client sends close alert.
    record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.ALERT, record.getType());
    record = proxy.readFromClient();
    Assert.assertNull(record);
    server.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) OutputStream(java.io.OutputStream) Request(org.eclipse.jetty.client.api.Request) BufferedReader(java.io.BufferedReader) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 9 with FutureResponseListener

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

the class SslBytesClientTest method testServerRenegotiation.

@Test
public void testServerRenegotiation() throws Exception {
    Request request = client.newRequest("localhost", proxy.getPort());
    FutureResponseListener listener = new FutureResponseListener(request);
    request.scheme(HttpScheme.HTTPS.asString()).send(listener);
    Assert.assertTrue(proxy.awaitClient(5, TimeUnit.SECONDS));
    final SSLSocket server = (SSLSocket) acceptor.accept();
    server.setUseClientMode(false);
    Future<Object> handshake = threadPool.submit(() -> {
        server.startHandshake();
        return null;
    });
    SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow();
    Assert.assertNull(handshake.get(5, TimeUnit.SECONDS));
    // Read request
    InputStream serverInput = server.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(serverInput, StandardCharsets.UTF_8));
    String line = reader.readLine();
    Assert.assertTrue(line.startsWith("GET"));
    while (line.length() > 0) line = reader.readLine();
    OutputStream serverOutput = server.getOutputStream();
    byte[] data1 = new byte[1024];
    Arrays.fill(data1, (byte) 'X');
    String content1 = new String(data1, StandardCharsets.UTF_8);
    byte[] data2 = new byte[1024];
    Arrays.fill(data2, (byte) 'Y');
    final String content2 = new String(data2, StandardCharsets.UTF_8);
    // Write first part of the response
    serverOutput.write(("HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + (content1.length() + content2.length()) + "\r\n" + "\r\n" + content1).getBytes(StandardCharsets.UTF_8));
    serverOutput.flush();
    Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));
    // Renegotiate
    Future<Object> renegotiation = threadPool.submit(() -> {
        server.startHandshake();
        return null;
    });
    // Renegotiation Handshake
    TLSRecord record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToClient(record);
    // Renegotiation Handshake
    record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToServer(record);
    // Trigger a read to have the server write the final renegotiation steps
    server.setSoTimeout(100);
    try {
        serverInput.read();
        Assert.fail();
    } catch (SocketTimeoutException x) {
    // Expected
    }
    // Renegotiation Handshake
    record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToClient(record);
    // Renegotiation Change Cipher
    record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.CHANGE_CIPHER_SPEC, record.getType());
    proxy.flushToClient(record);
    // Renegotiation Handshake
    record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToClient(record);
    // Renegotiation Change Cipher
    record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.CHANGE_CIPHER_SPEC, record.getType());
    proxy.flushToServer(record);
    // Renegotiation Handshake
    record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToServer(record);
    Assert.assertNull(renegotiation.get(5, TimeUnit.SECONDS));
    // Complete the response
    automaticProxyFlow = proxy.startAutomaticFlow();
    serverOutput.write(data2);
    serverOutput.flush();
    Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(data1.length + data2.length, response.getContent().length);
    server.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) 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) SocketTimeoutException(java.net.SocketTimeoutException) BufferedReader(java.io.BufferedReader) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 10 with FutureResponseListener

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

the class HttpReceiverOverHTTPTest method newExchange.

protected HttpExchange newExchange() {
    HttpRequest request = (HttpRequest) client.newRequest("http://localhost");
    FutureResponseListener listener = new FutureResponseListener(request);
    HttpExchange exchange = new HttpExchange(destination, request, Collections.<Response.ResponseListener>singletonList(listener));
    boolean associated = connection.getHttpChannel().associate(exchange);
    Assert.assertTrue(associated);
    exchange.requestComplete(null);
    exchange.terminateRequest();
    return exchange;
}
Also used : HttpRequest(org.eclipse.jetty.client.HttpRequest) Response(org.eclipse.jetty.client.api.Response) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Aggregations

FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)33 Test (org.junit.Test)24 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)22 Request (org.eclipse.jetty.client.api.Request)20 HttpServletRequest (javax.servlet.http.HttpServletRequest)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 IOException (java.io.IOException)10 ServletException (javax.servlet.ServletException)9 OutputStream (java.io.OutputStream)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HttpExchange (org.eclipse.jetty.client.HttpExchange)7 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)7 Random (java.util.Random)6 Connection (org.eclipse.jetty.client.api.Connection)6 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)6 InputStream (java.io.InputStream)5 Destination (org.eclipse.jetty.client.api.Destination)5 Response (org.eclipse.jetty.client.api.Response)5 FuturePromise (org.eclipse.jetty.util.FuturePromise)5 ServerSocket (java.net.ServerSocket)4