Search in sources :

Example 76 with Request

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

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

the class HttpClientTimeoutTest method testTimeoutIsCancelledOnSuccess.

@Slow
@Test
public void testTimeoutIsCancelledOnSuccess() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(timeout));
    final CountDownLatch latch = new CountDownLatch(1);
    final byte[] content = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).content(new InputStreamContentProvider(new ByteArrayInputStream(content))).timeout(2 * timeout, TimeUnit.MILLISECONDS);
    request.send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            Assert.assertArrayEquals(content, getContent());
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(3 * timeout, TimeUnit.MILLISECONDS));
    TimeUnit.MILLISECONDS.sleep(2 * timeout);
    Assert.assertNull(request.getAbortCause());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) InputStreamContentProvider(org.eclipse.jetty.client.util.InputStreamContentProvider) CountDownLatch(java.util.concurrent.CountDownLatch) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 78 with Request

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

the class HttpClientTimeoutTest method testTimeoutOnQueuedRequest.

@Slow
@Test
public void testTimeoutOnQueuedRequest() throws Exception {
    long timeout = 1000;
    start(new TimeoutHandler(3 * timeout));
    // Only one connection so requests get queued
    client.setMaxConnectionsPerDestination(1);
    // The first request has a long timeout
    final CountDownLatch firstLatch = new CountDownLatch(1);
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(4 * timeout, TimeUnit.MILLISECONDS);
    request.send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertFalse(result.isFailed());
            firstLatch.countDown();
        }
    });
    // Second request has a short timeout and should fail in the queue
    final CountDownLatch secondLatch = new CountDownLatch(1);
    request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(timeout, TimeUnit.MILLISECONDS);
    request.send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            Assert.assertTrue(result.isFailed());
            secondLatch.countDown();
        }
    });
    Assert.assertTrue(secondLatch.await(2 * timeout, TimeUnit.MILLISECONDS));
    // The second request must fail before the first request has completed
    Assert.assertTrue(firstLatch.getCount() > 0);
    Assert.assertTrue(firstLatch.await(5 * timeout, TimeUnit.MILLISECONDS));
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 79 with Request

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

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

Aggregations

Request (org.eclipse.jetty.client.api.Request)223 Test (org.junit.Test)137 HttpServletRequest (javax.servlet.http.HttpServletRequest)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 HttpServletResponse (javax.servlet.http.HttpServletResponse)63 IOException (java.io.IOException)62 CountDownLatch (java.util.concurrent.CountDownLatch)58 ServletException (javax.servlet.ServletException)53 HttpClient (org.eclipse.jetty.client.HttpClient)48 Response (org.eclipse.jetty.client.api.Response)37 Result (org.eclipse.jetty.client.api.Result)37 InputStream (java.io.InputStream)36 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)35 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)30 FutureResponseListener (org.eclipse.jetty.client.util.FutureResponseListener)28 ByteBuffer (java.nio.ByteBuffer)27 SalesforceException (org.apache.camel.component.salesforce.api.SalesforceException)26 Connection (org.eclipse.jetty.client.api.Connection)26 BytesContentProvider (org.eclipse.jetty.client.util.BytesContentProvider)25 URI (java.net.URI)24