Search in sources :

Example 1 with ServletInputStream

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

the class ProxyServletTest method testExpect100ContinueRespond100ContinueDelayedRequestContent.

@Test
public void testExpect100ContinueRespond100ContinueDelayedRequestContent() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // Send the 100 Continue.
            ServletInputStream input = request.getInputStream();
            // Echo the content.
            IO.copy(input, response.getOutputStream());
        }
    });
    startProxy();
    startClient();
    byte[] content = new byte[1024];
    new Random().nextBytes(content);
    int chunk1 = content.length / 2;
    DeferredContentProvider contentProvider = new DeferredContentProvider();
    contentProvider.offer(ByteBuffer.wrap(content, 0, chunk1));
    CountDownLatch clientLatch = new CountDownLatch(1);
    client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(contentProvider).send(new BufferingResponseListener() {

        @Override
        public void onComplete(Result result) {
            if (result.isSucceeded()) {
                if (result.getResponse().getStatus() == HttpStatus.OK_200) {
                    if (Arrays.equals(content, getContent()))
                        clientLatch.countDown();
                }
            }
        }
    });
    // Wait a while and then offer more content.
    Thread.sleep(1000);
    contentProvider.offer(ByteBuffer.wrap(content, chunk1, content.length - chunk1));
    contentProvider.close();
    Assert.assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) Random(java.util.Random) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Test(org.junit.Test)

Example 2 with ServletInputStream

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

the class AsyncMiddleManServletTest method testDownstreamTransformationBufferedGzipped.

@Test
public void testDownstreamTransformationBufferedGzipped() throws Exception {
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setHeader(HttpHeader.CONTENT_ENCODING.asString(), "gzip");
            ServletInputStream input = request.getInputStream();
            ServletOutputStream output = response.getOutputStream();
            int read;
            while ((read = input.read()) >= 0) {
                output.write(read);
                output.flush();
            }
        }
    });
    startProxy(new AsyncMiddleManServlet() {

        @Override
        protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) {
            return new GZIPContentTransformer(new BufferingContentTransformer());
        }
    });
    startClient();
    byte[] bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(StandardCharsets.UTF_8);
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).header(HttpHeader.CONTENT_ENCODING, "gzip").content(new BytesContentProvider(gzip(bytes))).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertArrayEquals(bytes, response.getContent());
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) Response(org.eclipse.jetty.client.api.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletInputStream(javax.servlet.ServletInputStream) Test(org.junit.Test)

Example 3 with ServletInputStream

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

the class AsyncMiddleManServletTest method testClientRequestReadFailsOnSecondRead.

@Test
public void testClientRequestReadFailsOnSecondRead() throws Exception {
    try (StacklessLogging scope = new StacklessLogging(HttpChannel.class)) {
        startServer(new EchoHttpServlet());
        startProxy(new AsyncMiddleManServlet() {

            private int count;

            @Override
            protected int readClientRequestContent(ServletInputStream input, byte[] buffer) throws IOException {
                if (++count < 2)
                    return super.readClientRequestContent(input, buffer);
                else
                    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) {
                if (result.getResponse().getStatus() == 502)
                    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 : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Result(org.eclipse.jetty.client.api.Result) 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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Test(org.junit.Test)

Example 4 with ServletInputStream

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

the class HttpServerTestBase method testInterruptedRequest.

@Test
public void testInterruptedRequest() throws Exception {
    final AtomicBoolean fourBytesRead = new AtomicBoolean(false);
    final AtomicBoolean earlyEOFException = new AtomicBoolean(false);
    configureServer(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        public void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            int contentLength = request.getContentLength();
            ServletInputStream inputStream = request.getInputStream();
            for (int i = 0; i < contentLength; i++) {
                try {
                    inputStream.read();
                } catch (EofException e) {
                    earlyEOFException.set(true);
                    throw new QuietServletException(e);
                }
                if (i == 3)
                    fourBytesRead.set(true);
            }
        }
    });
    StringBuffer request = new StringBuffer("GET / HTTP/1.0\n");
    request.append("Host: localhost\n");
    request.append("Content-length: 6\n\n");
    request.append("foo");
    Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
    OutputStream os = client.getOutputStream();
    os.write(request.toString().getBytes());
    os.flush();
    client.shutdownOutput();
    String response = readResponse(client);
    client.close();
    assertThat("response contains 500", response, Matchers.containsString(" 500 "));
    assertThat("The 4th byte (-1) has not been passed to the handler", fourBytesRead.get(), is(false));
    assertThat("EofException has been caught", earlyEOFException.get(), is(true));
}
Also used : EofException(org.eclipse.jetty.io.EofException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletInputStream(javax.servlet.ServletInputStream) Socket(java.net.Socket) Test(org.junit.Test)

Example 5 with ServletInputStream

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

the class HttpTrailersTest method testHugeTrailer.

@Test
public void testHugeTrailer() throws Exception {
    start(new AbstractHandler.ErrorDispatchHandler() {

        @Override
        protected void doNonErrorHandle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            jettyRequest.setHandled(true);
            try {
                // EOF will not be reached because of the huge trailer.
                ServletInputStream input = jettyRequest.getInputStream();
                while (true) {
                    int read = input.read();
                    if (read < 0)
                        break;
                }
                Assert.fail();
            } catch (IOException x) {
            // Expected.
            }
        }
    });
    char[] huge = new char[1024 * 1024];
    Arrays.fill(huge, 'X');
    try (Socket client = new Socket("localhost", connector.getLocalPort())) {
        client.setSoTimeout(5000);
        try {
            String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n" + "0\r\n" + "Trailer: " + new String(huge) + "\r\n" + "\r\n";
            OutputStream output = client.getOutputStream();
            output.write(request.getBytes(StandardCharsets.UTF_8));
            output.flush();
            HttpTester.Response response = HttpTester.parseResponse(HttpTester.from(client.getInputStream()));
            Assert.assertNotNull(response);
            Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
        } catch (Exception e) {
        // May be thrown if write fails and error handling is aborted
        }
    }
}
Also used : OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HttpTester(org.eclipse.jetty.http.HttpTester) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) Socket(java.net.Socket) Test(org.junit.Test)

Aggregations

ServletInputStream (javax.servlet.ServletInputStream)140 IOException (java.io.IOException)70 Test (org.junit.Test)56 HttpServletRequest (javax.servlet.http.HttpServletRequest)55 HttpServletResponse (javax.servlet.http.HttpServletResponse)46 ServletException (javax.servlet.ServletException)42 ByteArrayInputStream (java.io.ByteArrayInputStream)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 ReadListener (javax.servlet.ReadListener)25 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)17 PrintWriter (java.io.PrintWriter)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)15 Request (org.eclipse.jetty.server.Request)15 Test (org.junit.jupiter.api.Test)11