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;
}
}
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();
}
}
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());
}
}
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());
}
}
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);
}
}
Aggregations