Search in sources :

Example 86 with ServletInputStream

use of javax.servlet.ServletInputStream 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 87 with ServletInputStream

use of javax.servlet.ServletInputStream 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 88 with ServletInputStream

use of javax.servlet.ServletInputStream 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)

Example 89 with ServletInputStream

use of javax.servlet.ServletInputStream in project jetty.project by eclipse.

the class DispatcherForwardTest method testContentCanBeReadViaInputStreamAfterForwardWithoutQuery.

@Test
public void testContentCanBeReadViaInputStreamAfterForwardWithoutQuery() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final String query1 = "a=1%20one";
    final String form = "c=3%20three";
    servlet1 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            req.getRequestDispatcher("/two").forward(req, resp);
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("c"), Matchers.nullValue());
            latch.countDown();
        }
    };
    servlet2 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            ServletInputStream input = req.getInputStream();
            for (int i = 0; i < form.length(); ++i) checkThat(form.charAt(i) & 0xFFFF, Matchers.equalTo(input.read()));
        }
    };
    prepare();
    String request = "" + "POST /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + form.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + form;
    String response = connector.getResponses(request);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 90 with ServletInputStream

use of javax.servlet.ServletInputStream in project jetty.project by eclipse.

the class DispatcherForwardTest method testContentCanBeReadViaInputStreamAfterForwardWithQuery.

@Test
public void testContentCanBeReadViaInputStreamAfterForwardWithQuery() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    final String query1 = "a=1%20one";
    final String query2 = "b=2%20two";
    final String query3 = "b=2%20two&a=1%20one";
    final String form = "c=3%20three";
    servlet1 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            req.getRequestDispatcher("/two?" + query2).forward(req, resp);
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("c"), Matchers.nullValue());
            latch.countDown();
        }
    };
    servlet2 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query3));
            ServletInputStream input = req.getInputStream();
            for (int i = 0; i < form.length(); ++i) checkThat(form.charAt(i) & 0xFFFF, Matchers.equalTo(input.read()));
            checkThat(-1, Matchers.equalTo(input.read()));
        }
    };
    prepare();
    String request = "" + "POST /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + form.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + form;
    String response = connector.getResponses(request);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ServletInputStream (javax.servlet.ServletInputStream)148 IOException (java.io.IOException)73 HttpServletRequest (javax.servlet.http.HttpServletRequest)60 Test (org.junit.Test)59 HttpServletResponse (javax.servlet.http.HttpServletResponse)50 ServletException (javax.servlet.ServletException)42 ByteArrayInputStream (java.io.ByteArrayInputStream)32 ReadListener (javax.servlet.ReadListener)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 HttpServlet (javax.servlet.http.HttpServlet)21 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)20 InterruptedIOException (java.io.InterruptedIOException)18 AsyncContext (javax.servlet.AsyncContext)18 ServletOutputStream (javax.servlet.ServletOutputStream)18 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 PrintWriter (java.io.PrintWriter)16 Request (org.eclipse.jetty.server.Request)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)15 Test (org.junit.jupiter.api.Test)11