Search in sources :

Example 36 with DeferredContentProvider

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

the class HttpClientTest method testContentDelimitedByEOFWithSlowRequest.

private void testContentDelimitedByEOFWithSlowRequest(int length) throws Exception {
    final byte[] data = new byte[length];
    new Random().nextBytes(data);
    start(new AbstractHandler() {

        @Override
        public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.setHeader("Connection", "close");
            response.getOutputStream().write(data);
        }
    });
    DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.wrap(new byte[] { 0 }));
    Request request = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).content(content);
    FutureResponseListener listener = new FutureResponseListener(request);
    request.send(listener);
    // Wait some time to simulate a slow request.
    Thread.sleep(1000);
    content.close();
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(data, response.getContent());
}
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) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Random(java.util.Random) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) FutureResponseListener(org.eclipse.jetty.client.util.FutureResponseListener)

Example 37 with DeferredContentProvider

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

the class AsyncMiddleManServlet method service.

@Override
protected void service(HttpServletRequest clientRequest, HttpServletResponse proxyResponse) throws ServletException, IOException {
    String rewrittenTarget = rewriteTarget(clientRequest);
    if (_log.isDebugEnabled()) {
        StringBuffer target = clientRequest.getRequestURL();
        if (clientRequest.getQueryString() != null)
            target.append("?").append(clientRequest.getQueryString());
        _log.debug("{} rewriting: {} -> {}", getRequestId(clientRequest), target, rewrittenTarget);
    }
    if (rewrittenTarget == null) {
        onProxyRewriteFailed(clientRequest, proxyResponse);
        return;
    }
    final Request proxyRequest = getHttpClient().newRequest(rewrittenTarget).method(clientRequest.getMethod()).version(HttpVersion.fromString(clientRequest.getProtocol()));
    copyRequestHeaders(clientRequest, proxyRequest);
    addProxyHeaders(clientRequest, proxyRequest);
    final AsyncContext asyncContext = clientRequest.startAsync();
    // We do not timeout the continuation, but the proxy request.
    asyncContext.setTimeout(0);
    proxyRequest.timeout(getTimeout(), TimeUnit.MILLISECONDS);
    // to allow optimization of the Content-Length header.
    if (hasContent(clientRequest)) {
        DeferredContentProvider provider = newProxyContentProvider(clientRequest, proxyResponse, proxyRequest);
        proxyRequest.content(provider);
        if (expects100Continue(clientRequest)) {
            proxyRequest.attribute(CLIENT_REQUEST_ATTRIBUTE, clientRequest);
            proxyRequest.attribute(CONTINUE_ACTION_ATTRIBUTE, (Runnable) () -> {
                try {
                    ServletInputStream input = clientRequest.getInputStream();
                    input.setReadListener(newProxyReadListener(clientRequest, proxyResponse, proxyRequest, provider));
                } catch (Throwable failure) {
                    onClientRequestFailure(clientRequest, proxyRequest, proxyResponse, failure);
                }
            });
            sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
        } else {
            ServletInputStream input = clientRequest.getInputStream();
            input.setReadListener(newProxyReadListener(clientRequest, proxyResponse, proxyRequest, provider));
        }
    } else {
        sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncContext(javax.servlet.AsyncContext)

Example 38 with DeferredContentProvider

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

the class AsyncProxyServlet method proxyRequestContent.

@Override
protected ContentProvider proxyRequestContent(HttpServletRequest request, HttpServletResponse response, Request proxyRequest) throws IOException {
    ServletInputStream input = request.getInputStream();
    DeferredContentProvider provider = new DeferredContentProvider();
    input.setReadListener(newReadListener(request, response, proxyRequest, provider));
    return provider;
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider)

Example 39 with DeferredContentProvider

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

the class AsyncMiddleManServletTest method testProxyRequestHeadersSentWhenDiscardingContent.

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

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

        @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 not be sent.
    ByteBuffer chunk2 = ByteBuffer.allocate(512);
    content.offer(chunk2);
    Assert.assertFalse(proxyRequestLatch.await(1, TimeUnit.SECONDS));
    // Finish the content, request must be sent.
    content.close();
    Assert.assertTrue(proxyRequestLatch.await(1, TimeUnit.SECONDS));
    ContentResponse response = listener.get(5, TimeUnit.SECONDS);
    Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
    Assert.assertEquals(0, 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) 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 40 with DeferredContentProvider

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

the class AsyncMiddleManServletTest method testClientRequestReadFailsOnFirstRead.

@Test
public void testClientRequestReadFailsOnFirstRead() throws Exception {
    startServer(new EchoHttpServlet());
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected int readClientRequestContent(ServletInputStream input, byte[] buffer) throws IOException {
            throw new IOException("explicitly_thrown_by_test");
        }
    });
    startClient();
    final CountDownLatch latch = new CountDownLatch(1);
    DeferredContentProvider content = new DeferredContentProvider();
    client.newRequest("localhost", serverConnector.getLocalPort()).content(content).send(new Response.CompleteListener() {

        @Override
        public void onComplete(Result result) {
            System.err.println(result);
            if (result.getResponse().getStatus() == 500)
                latch.countDown();
        }
    });
    content.offer(ByteBuffer.allocate(512));
    sleep(1000);
    content.offer(ByteBuffer.allocate(512));
    content.close();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) Test(org.junit.Test)

Aggregations

DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)46 Test (org.junit.Test)41 CountDownLatch (java.util.concurrent.CountDownLatch)38 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 HttpServletResponse (javax.servlet.http.HttpServletResponse)34 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)28 ServletInputStream (javax.servlet.ServletInputStream)18 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)18 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)17 Request (org.eclipse.jetty.server.Request)16 Result (org.eclipse.jetty.client.api.Result)15 InterruptedIOException (java.io.InterruptedIOException)14 BufferingResponseListener (org.eclipse.jetty.client.util.BufferingResponseListener)12 AsyncContext (javax.servlet.AsyncContext)11 Request (org.eclipse.jetty.client.api.Request)11 ByteBuffer (java.nio.ByteBuffer)10 Response (org.eclipse.jetty.client.api.Response)9 StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)8 ReadListener (javax.servlet.ReadListener)7