use of javax.servlet.ServletException in project jetty.project by eclipse.
the class ConnectorTimeoutTest method testMaxIdleWithRequest10NoClientClose.
@Test(timeout = 60000)
public void testMaxIdleWithRequest10NoClientClose() throws Exception {
final Exchanger<EndPoint> exchanger = new Exchanger<>();
configureServer(new HelloWorldHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
exchanger.exchange(baseRequest.getHttpChannel().getEndPoint());
} catch (Exception e) {
e.printStackTrace();
}
super.handle(target, baseRequest, request, response);
}
});
Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort());
client.setSoTimeout(10000);
Assert.assertFalse(client.isClosed());
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write(("GET / HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: close\r\n" + "\r\n").getBytes("utf-8"));
os.flush();
// Get the server side endpoint
EndPoint endPoint = exchanger.exchange(null, 10, TimeUnit.SECONDS);
if (endPoint instanceof SslConnection.DecryptedEndPoint)
endPoint = endPoint.getConnection().getEndPoint();
// read the response
String result = IO.toString(is);
Assert.assertThat("OK", result, Matchers.containsString("200 OK"));
// check client reads EOF
Assert.assertEquals(-1, is.read());
// wait for idle timeout
TimeUnit.MILLISECONDS.sleep(3 * MAX_IDLE_TIME);
// further writes will get broken pipe or similar
try {
for (int i = 0; i < 1000; i++) {
os.write(("GET / HTTP/1.0\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "connection: keep-alive\r\n" + "\r\n").getBytes("utf-8"));
os.flush();
}
Assert.fail("half close should have timed out");
} catch (SocketException e) {
// expected
}
// check the server side is closed
Assert.assertFalse(endPoint.isOpen());
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class DumpHandler method doNonErrorHandle.
@Override
protected void doNonErrorHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (!isStarted())
return;
if (Boolean.valueOf(request.getParameter("flush")))
response.flushBuffer();
if (Boolean.valueOf(request.getParameter("empty"))) {
baseRequest.setHandled(true);
response.setStatus(200);
return;
}
StringBuilder read = null;
if (request.getParameter("read") != null) {
read = new StringBuilder();
int len = Integer.parseInt(request.getParameter("read"));
Reader in = request.getReader();
for (int i = len; i-- > 0; ) read.append((char) in.read());
}
if (request.getParameter("date") != null)
response.setHeader("Date", request.getParameter("date"));
if (request.getParameter("ISE") != null) {
throw new IllegalStateException("Testing ISE");
}
if (request.getParameter("error") != null) {
response.sendError(Integer.parseInt(request.getParameter("error")));
return;
}
baseRequest.setHandled(true);
response.setHeader(HttpHeader.CONTENT_TYPE.asString(), MimeTypes.Type.TEXT_HTML.asString());
OutputStream out = response.getOutputStream();
ByteArrayOutputStream buf = new ByteArrayOutputStream(2048);
Writer writer = new OutputStreamWriter(buf, StandardCharsets.ISO_8859_1);
writer.write("<html><h1>" + label + "</h1>");
writer.write("<pre>\npathInfo=" + request.getPathInfo() + "\n</pre>\n");
writer.write("<pre>\ncontentType=" + request.getContentType() + "\n</pre>\n");
writer.write("<pre>\nencoding=" + request.getCharacterEncoding() + "\n</pre>\n");
writer.write("<pre>\nservername=" + request.getServerName() + "\n</pre>\n");
writer.write("<pre>\nlocal=" + request.getLocalAddr() + ":" + request.getLocalPort() + "\n</pre>\n");
writer.write("<pre>\nremote=" + request.getRemoteAddr() + ":" + request.getRemotePort() + "\n</pre>\n");
writer.write("<h3>Header:</h3><pre>");
writer.write(request.getMethod() + " " + request.getRequestURI() + " " + request.getProtocol() + "\n");
Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String name = headers.nextElement();
writer.write(name);
writer.write(": ");
String value = request.getHeader(name);
writer.write(value == null ? "" : value);
writer.write("\n");
}
writer.write("</pre>\n<h3>Parameters:</h3>\n<pre>");
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String[] values = request.getParameterValues(name);
if (values == null || values.length == 0) {
writer.write(name);
writer.write("=\n");
} else if (values.length == 1) {
writer.write(name);
writer.write("=");
writer.write(values[0]);
writer.write("\n");
} else {
for (int i = 0; i < values.length; i++) {
writer.write(name);
writer.write("[" + i + "]=");
writer.write(values[i]);
writer.write("\n");
}
}
}
String cookie_name = request.getParameter("CookieName");
if (cookie_name != null && cookie_name.trim().length() > 0) {
String cookie_action = request.getParameter("Button");
try {
String val = request.getParameter("CookieVal");
val = val.replaceAll("[ \n\r=<>]", "?");
Cookie cookie = new Cookie(cookie_name.trim(), val);
if ("Clear Cookie".equals(cookie_action))
cookie.setMaxAge(0);
response.addCookie(cookie);
} catch (IllegalArgumentException e) {
writer.write("</pre>\n<h3>BAD Set-Cookie:</h3>\n<pre>");
writer.write(e.toString());
}
}
writer.write("</pre>\n<h3>Cookies:</h3>\n<pre>");
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (int c = 0; c < cookies.length; c++) {
Cookie cookie = cookies[c];
writer.write(cookie.getName());
writer.write("=");
writer.write(cookie.getValue());
writer.write("\n");
}
}
writer.write("</pre>\n<h3>Attributes:</h3>\n<pre>");
Enumeration<String> attributes = request.getAttributeNames();
if (attributes != null && attributes.hasMoreElements()) {
while (attributes.hasMoreElements()) {
String attr = attributes.nextElement().toString();
writer.write(attr);
writer.write("=");
writer.write(request.getAttribute(attr).toString());
writer.write("\n");
}
}
writer.write("</pre>\n<h3>Content:</h3>\n<pre>");
if (read != null) {
writer.write(read.toString());
} else {
char[] content = new char[4096];
int len;
try {
Reader in = request.getReader();
while ((len = in.read(content)) >= 0) writer.write(new String(content, 0, len));
} catch (IOException e) {
if (LOG.isDebugEnabled())
LOG.warn(e);
else
LOG.warn(e.toString());
writer.write(e.toString());
}
}
writer.write("</pre>\n");
writer.write("</html>\n");
writer.flush();
// commit now
if (!Boolean.valueOf(request.getParameter("no-content-length")))
response.setContentLength(buf.size() + 1000);
response.addHeader("Before-Flush", response.isCommitted() ? "Committed???" : "Not Committed");
buf.writeTo(out);
out.flush();
response.addHeader("After-Flush", "These headers should not be seen in the response!!!");
response.addHeader("After-Flush", response.isCommitted() ? "Committed" : "Not Committed?");
// write remaining content after commit
try {
buf.reset();
writer.flush();
for (int pad = 998; pad-- > 0; ) writer.write(" ");
writer.write("\r\n");
writer.flush();
buf.writeTo(out);
} catch (Exception e) {
LOG.ignore(e);
}
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class HttpConnectionTest method testEmptyFlush.
@Test
public void testEmptyFlush() throws Exception {
server.stop();
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setStatus(200);
OutputStream out = response.getOutputStream();
out.flush();
out.flush();
}
});
server.start();
String response = connector.getResponse("GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n");
assertThat(response, Matchers.containsString("200 OK"));
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class HostHeaderCustomizerTest method testHostHeaderCustomizer.
@Test
public void testHostHeaderCustomizer() throws Exception {
Server server = new Server();
HttpConfiguration httpConfig = new HttpConfiguration();
final String serverName = "test_server_name";
final int serverPort = 13;
final String redirectPath = "/redirect";
httpConfig.addCustomizer(new HostHeaderCustomizer(serverName, serverPort));
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
server.addConnector(connector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
Assert.assertEquals(serverName, request.getServerName());
Assert.assertEquals(serverPort, request.getServerPort());
response.sendRedirect(redirectPath);
}
});
server.start();
try {
try (Socket socket = new Socket("localhost", connector.getLocalPort())) {
try (OutputStream output = socket.getOutputStream()) {
String request = "" + "GET / HTTP/1.0\r\n" + "\r\n";
output.write(request.getBytes(StandardCharsets.UTF_8));
output.flush();
HttpTester.Input input = HttpTester.from(socket.getInputStream());
HttpTester.Response response = HttpTester.parseResponse(input);
String location = response.get("location");
Assert.assertNotNull(location);
String schemePrefix = "http://";
Assert.assertTrue(location.startsWith(schemePrefix));
Assert.assertTrue(location.endsWith(redirectPath));
String hostPort = location.substring(schemePrefix.length(), location.length() - redirectPath.length());
Assert.assertEquals(serverName + ":" + serverPort, hostPort);
}
}
} finally {
server.stop();
}
}
use of javax.servlet.ServletException in project jetty.project by eclipse.
the class ConcatServletTest method testConcatenation.
@Test
public void testConcatenation() throws Exception {
String contextPath = "";
ServletContextHandler context = new ServletContextHandler(server, contextPath);
server.setHandler(context);
String concatPath = "/concat";
context.addServlet(ConcatServlet.class, concatPath);
ServletHolder resourceServletHolder = new ServletHolder(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String includedURI = (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);
response.getOutputStream().println(includedURI);
}
});
context.addServlet(resourceServletHolder, "/resource/*");
server.start();
String resource1 = "/resource/one.js";
String resource2 = "/resource/two.js";
String uri = contextPath + concatPath + "?" + resource1 + "&" + resource2;
String request = "" + "GET " + uri + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
String response = connector.getResponses(request);
try (BufferedReader reader = new BufferedReader(new StringReader(response))) {
while (true) {
String line = reader.readLine();
if (line == null)
Assert.fail();
if (line.trim().isEmpty())
break;
}
Assert.assertEquals(resource1, reader.readLine());
Assert.assertEquals(resource2, reader.readLine());
Assert.assertNull(reader.readLine());
}
}
Aggregations