Search in sources :

Example 26 with FutureResponseListener

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

the class AsyncMiddleManServletTest method testProxyRequestHeadersNotSentUntilFirstContent.

@Test
public void testProxyRequestHeadersNotSentUntilFirstContent() throws Exception {
    startServer(new EchoHttpServlet());
    final CountDownLatch proxyRequestLatch = new CountDownLatch(1);
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newClientRequestContentTransformer(HttpServletRequest clientRequest, Request proxyRequest) {
            return new ContentTransformer() {

                private ByteBuffer buffer;

                @Override
                public void transform(ByteBuffer input, boolean finished, List<ByteBuffer> output) throws IOException {
                    // Buffer only the first chunk.
                    if (buffer == null) {
                        buffer = ByteBuffer.allocate(input.remaining());
                        buffer.put(input).flip();
                    } else if (buffer.hasRemaining()) {
                        output.add(buffer);
                        output.add(input);
                    } else {
                        output.add(input);
                    }
                }
            };
        }

        @Override
        protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
            proxyRequestLatch.countDown();
            super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
        }
    });
    startClient();
    DeferredContentProvider content = new DeferredContentProvider();
    Request request = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(5, TimeUnit.SECONDS).content(content);
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    // Send one chunk of content, the proxy request must not be sent.
    ByteBuffer chunk1 = ByteBuffer.allocate(1024);
    content.offer(chunk1);
    Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
    // Send another chunk of content, the proxy request must be sent.
    ByteBuffer chunk2 = ByteBuffer.allocate(512);
    content.offer(chunk2);
    Assert.assertTrue(proxyRequestLatch.await(5, TimeUnit.SECONDS));
    // Finish the content.
    content.close();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(chunk1.capacity() + chunk2.capacity(), response.getContent().length);
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) HttpServletRequest(javax.servlet.http.HttpServletRequest) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 27 with FutureResponseListener

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

the class SslBytesClientTest method testHandshake.

@Test
public void testHandshake() 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;
    });
    // Client Hello
    TLSRecord record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToServer(record);
    // Server Hello + Certificate + Server Done
    record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToClient(record);
    // Client Key Exchange
    record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToServer(record);
    // Change Cipher Spec
    record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.CHANGE_CIPHER_SPEC, record.getType());
    proxy.flushToServer(record);
    // Client Done
    record = proxy.readFromClient();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToServer(record);
    // Change Cipher Spec
    record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.CHANGE_CIPHER_SPEC, record.getType());
    proxy.flushToClient(record);
    // Server Done
    record = proxy.readFromServer();
    Assert.assertEquals(TLSRecord.Type.HANDSHAKE, record.getType());
    proxy.flushToClient(record);
    Assert.assertNull(handshake.get(5, TimeUnit.SECONDS));
    SimpleProxy.AutomaticFlow automaticProxyFlow = proxy.startAutomaticFlow();
    // Read request
    BufferedReader reader = new BufferedReader(new InputStreamReader(server.getInputStream(), StandardCharsets.UTF_8));
    String line = reader.readLine();
    Assert.assertTrue(line.startsWith("GET"));
    while (line.length() > 0) line = reader.readLine();
    // Write response
    OutputStream output = server.getOutputStream();
    output.write(("HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n").getBytes(StandardCharsets.UTF_8));
    output.flush();
    Assert.assertTrue(automaticProxyFlow.stop(5, TimeUnit.SECONDS));
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    server.close();
}
Also used : InputStreamReader(java.io.InputStreamReader) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) 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 28 with FutureResponseListener

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

the class HttpReceiverOverHTTPTest method test_Receive_NoResponseContent.

@Test
public void test_Receive_NoResponseContent() throws Exception {
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: 0\r\n" + "\r\n");
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("OK", response.getReason());
    Assert.assertSame(HttpVersion.HTTP_1_1, response.getVersion());
    HttpFields headers = response.getHeaders();
    Assert.assertNotNull(headers);
    Assert.assertEquals(1, headers.size());
    Assert.assertEquals("0", headers.get(HttpHeader.CONTENT_LENGTH));
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpFields(org.eclipse.jetty.http.HttpFields) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 29 with FutureResponseListener

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

the class HttpReceiverOverHTTPTest method test_FillInterested_RacingWith_BufferRelease.

@Test
public void test_FillInterested_RacingWith_BufferRelease() throws Exception {
    connection = new HttpConnectionOverHTTP(endPoint, destination, new Promise.Adapter<>()) {

        @Override
        protected HttpChannelOverHTTP newHttpChannel() {
            return new HttpChannelOverHTTP(this) {

                @Override
                protected HttpReceiverOverHTTP newHttpReceiver() {
                    return new HttpReceiverOverHTTP(this) {

                        @Override
                        protected void fillInterested() {
                            // Verify that the buffer has been released
                            // before fillInterested() is called.
                            Assert.assertNull(getResponseBuffer());
                            // Fill the endpoint so receive is called again.
                            endPoint.addInput("X");
                            super.fillInterested();
                        }
                    };
                }
            };
        }
    };
    endPoint.setConnection(connection);
    // Partial response to trigger the call to fillInterested().
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-Length: 1\r\n" + "\r\n");
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    Response response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(org.eclipse.jetty.client.api.Response) HttpExchange(org.eclipse.jetty.client.HttpExchange) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

Example 30 with FutureResponseListener

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

the class HttpReceiverOverHTTPTest method test_Receive_BadResponse.

@Test
public void test_Receive_BadResponse() throws Exception {
    endPoint.addInput("" + "HTTP/1.1 200 OK\r\n" + "Content-length: A\r\n" + "\r\n");
    HttpExchange exchange = newExchange();
    FutureResponseListener listener = (FutureResponseListener) exchange.getResponseListeners().get(0);
    connection.getHttpChannel().receive();
    try {
        listener.get(5, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException e) {
        Assert.assertTrue(e.getCause() instanceof HttpResponseException);
    }
}
Also used : HttpExchange(org.eclipse.jetty.client.HttpExchange) HttpResponseException(org.eclipse.jetty.client.HttpResponseException) ExecutionException(java.util.concurrent.ExecutionException) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener) Test(org.junit.Test)

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