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