Search in sources :

Example 11 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class AsyncMiddleManServletTest method testAfterContentTransformerClosingFilesOnClientRequestException.

@Test
public void testAfterContentTransformerClosingFilesOnClientRequestException() throws Exception {
    final Path targetTestsDir = prepareTargetTestsDir();
    startServer(new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            IO.copy(request.getInputStream(), IO.getNullStream());
        }
    });
    final CountDownLatch destroyLatch = new CountDownLatch(1);
    startProxy(new AsyncMiddleManServlet() {

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

                {
                    setOverflowDirectory(targetTestsDir);
                    setMaxInputBufferSize(0);
                    setMaxOutputBufferSize(0);
                }

                @Override
                public boolean transform(Source source, Sink sink) throws IOException {
                    IO.copy(source.getInputStream(), sink.getOutputStream());
                    return true;
                }

                @Override
                public void destroy() {
                    super.destroy();
                    destroyLatch.countDown();
                }
            };
        }
    });
    long idleTimeout = 1000;
    proxyConnector.setIdleTimeout(idleTimeout);
    startClient();
    // Send only part of the content; the proxy will idle timeout.
    final byte[] data = new byte[] { 'c', 'a', 'f', 'e' };
    ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort()).content(new BytesContentProvider(data) {

        @Override
        public long getLength() {
            return data.length + 1;
        }
    }).timeout(5 * idleTimeout, TimeUnit.MILLISECONDS).send();
    Assert.assertTrue(destroyLatch.await(5 * idleTimeout, TimeUnit.MILLISECONDS));
    Assert.assertEquals(HttpStatus.REQUEST_TIMEOUT_408, response.getStatus());
}
Also used : Path(java.nio.file.Path) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) 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) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 12 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class ProxyServletFailureTest method testProxyRequestExpired.

@Test
public void testProxyRequestExpired() throws Exception {
    prepareProxy();
    final long timeout = 1000;
    proxyServlet.setTimeout(timeout);
    prepareServer(new HttpServlet() {

        @Override
        protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
            if (request.getHeader("Via") != null)
                response.addHeader(PROXIED_HEADER, "true");
            try {
                TimeUnit.MILLISECONDS.sleep(2 * timeout);
            } catch (InterruptedException x) {
                throw new ServletException(x);
            }
        }
    });
    Response response = client.newRequest("localhost", serverConnector.getLocalPort()).timeout(3 * timeout, TimeUnit.MILLISECONDS).send();
    Assert.assertEquals(504, response.getStatus());
    Assert.assertFalse(response.getHeaders().containsKey(PROXIED_HEADER));
}
Also used : 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) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class ProxyServletLoadTest method test.

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

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
            IO.copy(req.getInputStream(), resp.getOutputStream());
        }
    });
    startProxy();
    startClient();
    // Number of clients to simulate
    int clientCount = Runtime.getRuntime().availableProcessors();
    // Latch for number of clients still active (used to terminate test)
    final CountDownLatch activeClientLatch = new CountDownLatch(clientCount);
    // Atomic Boolean to track that its OK to still continue looping.
    // When this goes false, that means one of the client threads has
    // encountered an error condition, and should allow all remaining
    // client threads to finish cleanly.
    final AtomicBoolean success = new AtomicBoolean(true);
    int iterations = 1000;
    // Start clients
    for (int i = 0; i < clientCount; i++) {
        ClientLoop r = new ClientLoop(activeClientLatch, success, client, "localhost", serverConnector.getLocalPort(), iterations);
        String name = "client-" + i;
        Thread thread = new Thread(r, name);
        thread.start();
    }
    Assert.assertTrue(activeClientLatch.await(Math.max(clientCount * iterations * 10, 5000), TimeUnit.MILLISECONDS));
    Assert.assertTrue(success.get());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 14 with HttpServlet

use of javax.servlet.http.HttpServlet 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 15 with HttpServlet

use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.

the class ProxyServletTest method testClientExcludedHosts.

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

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            if (req.getHeader("Via") != null)
                resp.addHeader(PROXIED_HEADER, "true");
        }
    });
    startProxy();
    startClient();
    int port = serverConnector.getLocalPort();
    client.getProxyConfiguration().getProxies().get(0).getExcludedAddresses().add("127.0.0.1:" + port);
    // Try with a proxied host
    ContentResponse response = client.newRequest("localhost", port).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
    // Try again with an excluded host
    response = client.newRequest("127.0.0.1", port).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertEquals(200, response.getStatus());
    Assert.assertFalse(response.getHeaders().containsKey(PROXIED_HEADER));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpContentResponse(org.eclipse.jetty.client.HttpContentResponse) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpServlet (javax.servlet.http.HttpServlet)207 HttpServletRequest (javax.servlet.http.HttpServletRequest)170 HttpServletResponse (javax.servlet.http.HttpServletResponse)169 IOException (java.io.IOException)140 ServletException (javax.servlet.ServletException)137 Test (org.junit.Test)124 CountDownLatch (java.util.concurrent.CountDownLatch)69 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)61 InterruptedIOException (java.io.InterruptedIOException)55 ServletOutputStream (javax.servlet.ServletOutputStream)37 AsyncContext (javax.servlet.AsyncContext)35 ServletInputStream (javax.servlet.ServletInputStream)32 HttpFields (org.eclipse.jetty.http.HttpFields)32 MetaData (org.eclipse.jetty.http.MetaData)32 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)32 Session (org.eclipse.jetty.http2.api.Session)30 Stream (org.eclipse.jetty.http2.api.Stream)27 Response (org.eclipse.jetty.client.api.Response)26 HttpContentResponse (org.eclipse.jetty.client.HttpContentResponse)24 HashMap (java.util.HashMap)22