Search in sources :

Example 56 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class SlowClientWithPipelinedRequestTest method testSlowClientWithPipelinedRequest.

@Test
public void testSlowClientWithPipelinedRequest() throws Exception {
    final int contentLength = 512 * 1024;
    startServer(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            if ("/content".equals(target)) {
                // We simulate what the DefaultServlet does, bypassing the blocking
                // write mechanism otherwise the test does not reproduce the bug
                OutputStream outputStream = response.getOutputStream();
                HttpOutput output = (HttpOutput) outputStream;
                // Since the test is via localhost, we need a really big buffer to stall the write
                byte[] bytes = new byte[contentLength];
                Arrays.fill(bytes, (byte) '9');
                ByteBuffer buffer = ByteBuffer.wrap(bytes);
                // Do a non blocking write
                output.sendContent(buffer);
            }
        }
    });
    Socket client = new Socket("localhost", connector.getLocalPort());
    OutputStream output = client.getOutputStream();
    output.write(("" + "GET /content HTTP/1.1\r\n" + "Host: localhost:" + connector.getLocalPort() + "\r\n" + "\r\n" + "").getBytes(StandardCharsets.UTF_8));
    output.flush();
    InputStream input = client.getInputStream();
    int read = input.read();
    Assert.assertTrue(read >= 0);
    // As soon as we can read the response, send a pipelined request
    // so it is a different read for the server and it will trigger NIO
    output.write(("" + "GET /pipelined HTTP/1.1\r\n" + "Host: localhost:" + connector.getLocalPort() + "\r\n" + "\r\n" + "").getBytes(StandardCharsets.UTF_8));
    output.flush();
    // Simulate a slow reader
    Thread.sleep(1000);
    Assert.assertThat(handles.get(), lessThan(10));
    // We are sure we are not spinning, read the content
    StringBuilder lines = new StringBuilder().append((char) read);
    int crlfs = 0;
    while (true) {
        read = input.read();
        lines.append((char) read);
        if (read == '\r' || read == '\n')
            ++crlfs;
        else
            crlfs = 0;
        if (crlfs == 4)
            break;
    }
    Assert.assertTrue(lines.toString().contains(" 200 "));
    // Read the body
    for (int i = 0; i < contentLength; ++i) input.read();
    // Read the pipelined response
    lines.setLength(0);
    crlfs = 0;
    while (true) {
        read = input.read();
        lines.append((char) read);
        if (read == '\r' || read == '\n')
            ++crlfs;
        else
            crlfs = 0;
        if (crlfs == 4)
            break;
    }
    Assert.assertTrue(lines.toString().contains(" 200 "));
    client.close();
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) EndPoint(org.eclipse.jetty.io.EndPoint) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Socket(java.net.Socket) Test(org.junit.Test)

Example 57 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientTest method testPOSTWithParameters.

@Test
public void testPOSTWithParameters() throws Exception {
    final String paramName = "a";
    final String paramValue = "€";
    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);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/plain");
                response.getOutputStream().print(value);
            }
        }
    });
    ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort()).param(paramName, paramValue).timeout(5, TimeUnit.SECONDS).send();
    Assert.assertNotNull(response);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(paramValue, new String(response.getContent(), "UTF-8"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 58 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientTest method testEarlyEOF.

@Test
public void testEarlyEOF() throws Exception {
    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);
            // Promise some content, then flush the headers, then fail to send the content.
            response.setContentLength(16);
            response.flushBuffer();
            throw new NullPointerException("Explicitly thrown by test");
        }
    });
    try (StacklessLogging stackless = new StacklessLogging(org.eclipse.jetty.server.HttpChannel.class)) {
        client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).timeout(60, TimeUnit.SECONDS).send();
        Assert.fail();
    } catch (ExecutionException x) {
    // Expected.
    }
}
Also used : 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) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 59 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class HttpClientTest method testPOSTWithParametersWithContent.

@Test
public void testPOSTWithParametersWithContent() throws Exception {
    final byte[] content = { 0, 1, 2, 3 };
    final String paramName = "a";
    final String paramValue = "€";
    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);
            String value = request.getParameter(paramName);
            if (paramValue.equals(value)) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/octet-stream");
                IO.copy(request.getInputStream(), response.getOutputStream());
            }
        }
    });
    for (int i = 0; i < 256; ++i) {
        ContentResponse response = client.POST(scheme + "://localhost:" + connector.getLocalPort() + "/?b=1").param(paramName, paramValue).content(new BytesContentProvider(content)).timeout(5, TimeUnit.SECONDS).send();
        Assert.assertNotNull(response);
        Assert.assertEquals(200, response.getStatus());
        Assert.assertArrayEquals(content, response.getContent());
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Test(org.junit.Test)

Example 60 with AbstractHandler

use of org.eclipse.jetty.server.handler.AbstractHandler in project jetty.project by eclipse.

the class ServletContextHandlerTest method testFallThrough.

@Test
public void testFallThrough() throws Exception {
    HandlerList list = new HandlerList();
    _server.setHandler(list);
    ServletContextHandler root = new ServletContextHandler(list, "/", ServletContextHandler.SESSIONS);
    ServletHandler servlet = root.getServletHandler();
    servlet.setEnsureDefaultServlet(false);
    servlet.addServletWithMapping(HelloServlet.class, "/hello/*");
    list.addHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.sendError(404, "Fell Through");
        }
    });
    _server.start();
    String response = _connector.getResponses("GET /hello HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("200 OK"));
    response = _connector.getResponses("GET /other HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("404 Fell Through"));
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) 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) Test(org.junit.Test)

Aggregations

AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)219 HttpServletRequest (javax.servlet.http.HttpServletRequest)216 HttpServletResponse (javax.servlet.http.HttpServletResponse)216 IOException (java.io.IOException)203 ServletException (javax.servlet.ServletException)203 Test (org.junit.Test)188 Request (org.eclipse.jetty.server.Request)121 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)118 CountDownLatch (java.util.concurrent.CountDownLatch)80 InterruptedIOException (java.io.InterruptedIOException)40 Result (org.eclipse.jetty.client.api.Result)38 InputStream (java.io.InputStream)35 ServletOutputStream (javax.servlet.ServletOutputStream)34 ByteBuffer (java.nio.ByteBuffer)32 Response (org.eclipse.jetty.client.api.Response)32 Request (org.eclipse.jetty.client.api.Request)29 OutputStream (java.io.OutputStream)27 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24