Search in sources :

Example 91 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.

the class HttpConnectionTest method testAsterisk.

@Test
public void testAsterisk() throws Exception {
    String response = null;
    try (StacklessLogging stackless = new StacklessLogging(HttpParser.LOG)) {
        int offset = 0;
        response = connector.getResponse("OPTIONS * HTTP/1.1\r\n" + "Host: localhost\r\n" + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Connection: close\r\n" + "\r\n" + "5;\r\n" + "12345\r\n" + "0;\r\n" + "\r\n");
        checkContains(response, offset, "HTTP/1.1 200");
        offset = 0;
        response = connector.getResponse("GET * HTTP/1.1\r\n" + "Host: localhost\r\n" + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Connection: close\r\n" + "\r\n" + "5;\r\n" + "12345\r\n" + "0;\r\n" + "\r\n");
        checkContains(response, offset, "HTTP/1.1 400");
        offset = 0;
        response = connector.getResponse("GET ** HTTP/1.1\r\n" + "Host: localhost\r\n" + "Transfer-Encoding: chunked\r\n" + "Content-Type: text/plain; charset=utf-8\r\n" + "Connection: close\r\n" + "\r\n" + "5;\r\n" + "12345\r\n" + "0;\r\n" + "\r\n");
        checkContains(response, offset, "HTTP/1.1 400 Bad Request");
    } catch (Exception e) {
        if (response != null)
            System.err.println(response);
        throw e;
    }
}
Also used : StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) LocalEndPoint(org.eclipse.jetty.server.LocalConnector.LocalEndPoint) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Test(org.junit.Test)

Example 92 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.

the class ThreadStarvationTest method testFailureStarvation.

@Test
public void testFailureStarvation() throws Exception {
    try (StacklessLogging stackless = new StacklessLogging(HttpChannel.class)) {
        int acceptors = 0;
        int selectors = 1;
        int maxThreads = 10;
        final int barried = maxThreads - acceptors - selectors * 2;
        final CyclicBarrier barrier = new CyclicBarrier(barried);
        QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, maxThreads);
        threadPool.setDetailedDump(true);
        _server = new Server(threadPool);
        ServerConnector connector = new ServerConnector(_server, acceptors, selectors) {

            @Override
            protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
                return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {

                    @Override
                    public boolean flush(ByteBuffer... buffers) throws IOException {
                        super.flush(buffers[0]);
                        throw new IOException("TEST FAILURE");
                    }
                };
            }
        };
        connector.setIdleTimeout(Long.MAX_VALUE);
        _server.addConnector(connector);
        final AtomicInteger count = new AtomicInteger(0);
        _server.setHandler(new AbstractHandler() {

            @Override
            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                int c = count.getAndIncrement();
                try {
                    if (c < barried) {
                        barrier.await(10, TimeUnit.SECONDS);
                    }
                } catch (InterruptedException | BrokenBarrierException | TimeoutException e) {
                    throw new ServletException(e);
                }
                baseRequest.setHandled(true);
                response.setStatus(200);
                response.setContentLength(13);
                response.getWriter().print("Hello World!\n");
                response.getWriter().flush();
            }
        });
        _server.start();
        List<Socket> sockets = new ArrayList<>();
        for (int i = 0; i < maxThreads * 2; ++i) {
            Socket socket = new Socket("localhost", connector.getLocalPort());
            sockets.add(socket);
            OutputStream output = socket.getOutputStream();
            String request = "" + "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + //                    "Connection: close\r\n" +
            "\r\n";
            output.write(request.getBytes(StandardCharsets.UTF_8));
            output.flush();
        }
        byte[] buffer = new byte[48 * 1024];
        List<Exchanger<Integer>> totals = new ArrayList<>();
        for (Socket socket : sockets) {
            final Exchanger<Integer> x = new Exchanger<>();
            totals.add(x);
            final InputStream input = socket.getInputStream();
            new Thread() {

                @Override
                public void run() {
                    int read = 0;
                    try {
                        // look for CRLFCRLF
                        StringBuilder header = new StringBuilder();
                        int state = 0;
                        while (state < 4 && header.length() < 2048) {
                            int ch = input.read();
                            if (ch < 0)
                                break;
                            header.append((char) ch);
                            switch(state) {
                                case 0:
                                    if (ch == '\r')
                                        state = 1;
                                    break;
                                case 1:
                                    if (ch == '\n')
                                        state = 2;
                                    else
                                        state = 0;
                                    break;
                                case 2:
                                    if (ch == '\r')
                                        state = 3;
                                    else
                                        state = 0;
                                    break;
                                case 3:
                                    if (ch == '\n')
                                        state = 4;
                                    else
                                        state = 0;
                                    break;
                            }
                        }
                        read = input.read(buffer);
                    } catch (IOException e) {
                    // e.printStackTrace();
                    } finally {
                        try {
                            x.exchange(read);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
        for (Exchanger<Integer> x : totals) {
            Integer read = x.exchange(-1, 10, TimeUnit.SECONDS);
            Assert.assertEquals(-1, read.intValue());
        }
        // We could read everything, good.
        for (Socket socket : sockets) socket.close();
        _server.stop();
    }
}
Also used : ManagedSelector(org.eclipse.jetty.io.ManagedSelector) SocketChannel(java.nio.channels.SocketChannel) Server(org.eclipse.jetty.server.Server) Exchanger(java.util.concurrent.Exchanger) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) SelectionKey(java.nio.channels.SelectionKey) InputStream(java.io.InputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) ChannelEndPoint(org.eclipse.jetty.io.ChannelEndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Socket(java.net.Socket) Test(org.junit.Test)

Example 93 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.

the class MultipartFilterTest method testFinalBoundaryOnly.

@Test
public void testFinalBoundaryOnly() throws Exception {
    tester.addServlet(NullServlet.class, "/null");
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    // test GET
    request.setMethod("POST");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setURI("/context/null");
    String delimiter = "\r\n";
    final String boundary = "MockMultiPartTestBoundary";
    String content = delimiter + "Hello world" + // Two delimiter markers, which make an empty line.
    delimiter + delimiter + "--" + boundary + "--" + delimiter;
    request.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    request.setContent(content);
    try (StacklessLogging stackless = new StacklessLogging(ServletHandler.class, HttpChannel.class)) {
        response = HttpTester.parseResponse(tester.getResponses(request.generate()));
        assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 94 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.

the class MultipartFilterTest method testBufferOverflowNoCRLF.

@Test
public void testBufferOverflowNoCRLF() throws Exception {
    String boundary = "XyXyXy";
    // generated and parsed test
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    tester.addServlet(BoundaryServlet.class, "/testb");
    tester.setAttribute("fileName", "abc");
    tester.setAttribute("desc", "123");
    tester.setAttribute("title", "ttt");
    request.setMethod("POST");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setURI("/context/testb");
    request.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    String content = "--XyXyXy";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(content.getBytes());
    for (//create content that will overrun default buffer size of BufferedInputStream
    int i = 0; //create content that will overrun default buffer size of BufferedInputStream
    i < 8500; //create content that will overrun default buffer size of BufferedInputStream
    i++) {
        baos.write('a');
    }
    request.setContent(baos.toString());
    try (StacklessLogging stackless = new StacklessLogging(ServletHandler.class, HttpChannel.class)) {
        response = HttpTester.parseResponse(tester.getResponses(request.generate()));
        assertTrue(response.getContent().contains("Buffer size exceeded"));
        assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatus());
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 95 with StacklessLogging

use of org.eclipse.jetty.util.log.StacklessLogging in project jetty.project by eclipse.

the class MultipartFilterTest method testWhitespaceBody.

@Test
public void testWhitespaceBody() throws Exception {
    String whitespace = " ";
    String boundary = "XyXyXy";
    // generated and parsed test
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod("POST");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setURI("/context/dump");
    request.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
    request.setContent(whitespace);
    try (StacklessLogging stackless = new StacklessLogging(ServletHandler.class, HttpChannel.class)) {
        response = HttpTester.parseResponse(tester.getResponses(request.generate()));
        assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, response.getStatus());
        assertTrue(response.getContent().indexOf("Missing initial") >= 0);
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Aggregations

StacklessLogging (org.eclipse.jetty.util.log.StacklessLogging)143 Test (org.junit.Test)134 CloseInfo (org.eclipse.jetty.websocket.common.CloseInfo)55 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)51 ArrayList (java.util.ArrayList)46 Fuzzer (org.eclipse.jetty.websocket.common.test.Fuzzer)45 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)35 ByteBuffer (java.nio.ByteBuffer)29 IOException (java.io.IOException)26 HttpServletRequest (javax.servlet.http.HttpServletRequest)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 ContinuationFrame (org.eclipse.jetty.websocket.common.frames.ContinuationFrame)21 ServletException (javax.servlet.ServletException)20 CountDownLatch (java.util.concurrent.CountDownLatch)17 PingFrame (org.eclipse.jetty.websocket.common.frames.PingFrame)17 Matchers.containsString (org.hamcrest.Matchers.containsString)17 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)14 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)13 OutputStream (java.io.OutputStream)12 Socket (java.net.Socket)12