Search in sources :

Example 71 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestHttp11Processor method doTestBug53677.

private void doTestBug53677(boolean flush) throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "LargeHeaderServlet", new LargeHeaderServlet(flush));
    ctx.addServletMappingDecoded("/test", "LargeHeaderServlet");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    Map<String, List<String>> responseHeaders = new HashMap<>();
    int rc = getUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
    assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc);
    if (responseBody.getLength() > 0) {
        // It will be >0 if the standard error page handling has been
        // triggered
        assertFalse(responseBody.toString().contains("FAIL"));
    }
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 72 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestHttp11Processor method testBug59310.

@Test
public void testBug59310() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "Bug59310", new Bug59310Servlet());
    ctx.addServletMappingDecoded("/test", "Bug59310");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    Map<String, List<String>> responseHeaders = new HashMap<>();
    int rc = headUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertEquals(0, responseBody.getLength());
    assertFalse(responseHeaders.containsKey("Content-Length"));
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 73 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestHttp11Processor method testNoChunking11NoContentLengthConnectionClose.

@Test
public void testNoChunking11NoContentLengthConnectionClose() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    Tomcat.addServlet(ctx, "NoContentLengthConnectionCloseFlushingServlet", new NoContentLengthConnectionCloseFlushingServlet());
    ctx.addServletMappingDecoded("/test", "NoContentLengthConnectionCloseFlushingServlet");
    tomcat.start();
    ByteChunk responseBody = new ByteChunk();
    Map<String, List<String>> responseHeaders = new HashMap<>();
    int rc = getUrl("http://localhost:" + getPort() + "/test", responseBody, responseHeaders);
    assertEquals(HttpServletResponse.SC_OK, rc);
    assertTrue(responseHeaders.containsKey("Connection"));
    List<String> connections = responseHeaders.get("Connection");
    assertEquals(1, connections.size());
    assertEquals("close", connections.get(0));
    assertFalse(responseHeaders.containsKey("Transfer-Encoding"));
    assertEquals("OK", responseBody.toString());
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 74 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestHttp11Processor method testRequestBodySwallowing.

/*
     * Tests what happens if a request is completed during a dispatch but the
     * request body has not been fully read.
     */
@Test
public void testRequestBodySwallowing() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    DispatchingServlet servlet = new DispatchingServlet();
    Wrapper w = Tomcat.addServlet(ctx, "Test", servlet);
    w.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/test", "Test");
    tomcat.start();
    // Hand-craft the client so we have complete control over the timing
    SocketAddress addr = new InetSocketAddress("localhost", getPort());
    Socket socket = new Socket();
    socket.setSoTimeout(300000);
    socket.connect(addr, 300000);
    OutputStream os = socket.getOutputStream();
    Writer writer = new OutputStreamWriter(os, "ISO-8859-1");
    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is, "ISO-8859-1");
    BufferedReader reader = new BufferedReader(r);
    // Write the headers
    writer.write("POST /test HTTP/1.1\r\n");
    writer.write("Host: localhost:8080\r\n");
    writer.write("Transfer-Encoding: chunked\r\n");
    writer.write("\r\n");
    writer.flush();
    validateResponse(reader);
    // Write the request body
    writer.write("2\r\n");
    writer.write("AB\r\n");
    writer.write("0\r\n");
    writer.write("\r\n");
    writer.flush();
    // Write the 2nd request
    writer.write("POST /test HTTP/1.1\r\n");
    writer.write("Host: localhost:8080\r\n");
    writer.write("Transfer-Encoding: chunked\r\n");
    writer.write("\r\n");
    writer.flush();
    // Read the 2nd response
    validateResponse(reader);
    // Write the 2nd request body
    writer.write("2\r\n");
    writer.write("AB\r\n");
    writer.write("0\r\n");
    writer.write("\r\n");
    writer.flush();
    // Done
    socket.close();
}
Also used : AsyncContext(javax.servlet.AsyncContext) Context(org.apache.catalina.Context) Wrapper(org.apache.catalina.Wrapper) Tomcat(org.apache.catalina.startup.Tomcat) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 75 with Context

use of org.apache.catalina.Context in project tomcat by apache.

the class TestUpgradeInternalHandler method doUpgrade.

private UpgradeConnection doUpgrade(Class<? extends HttpUpgradeHandler> upgradeHandlerClass) throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    UpgradeServlet servlet = new UpgradeServlet(upgradeHandlerClass);
    Tomcat.addServlet(ctx, "servlet", servlet);
    ctx.addServletMappingDecoded("/", "servlet");
    tomcat.start();
    // Use raw socket so the necessary control is available after the HTTP
    // upgrade
    Socket socket = SocketFactory.getDefault().createSocket("localhost", getPort());
    socket.setSoTimeout(5000);
    UpgradeConnection uc = new UpgradeConnection(socket);
    uc.getWriter().write("GET / HTTP/1.1" + CRLF);
    uc.getWriter().write("Host: whatever" + CRLF);
    uc.getWriter().write(CRLF);
    uc.getWriter().flush();
    String status = uc.getReader().readLine();
    Assert.assertNotNull(status);
    Assert.assertEquals("101", getStatusCode(status));
    // Skip the remaining response headers
    String line = uc.getReader().readLine();
    while (line != null && line.length() > 0) {
        // Skip
        line = uc.getReader().readLine();
    }
    return uc;
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) Socket(java.net.Socket)

Aggregations

Context (org.apache.catalina.Context)376 Tomcat (org.apache.catalina.startup.Tomcat)212 Test (org.junit.Test)180 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)127 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)96 File (java.io.File)77 ServletContext (javax.servlet.ServletContext)74 AsyncContext (javax.servlet.AsyncContext)73 StandardContext (org.apache.catalina.core.StandardContext)65 Wrapper (org.apache.catalina.Wrapper)53 IOException (java.io.IOException)40 TesterContext (org.apache.tomcat.unittest.TesterContext)39 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)37 URI (java.net.URI)33 WebSocketContainer (javax.websocket.WebSocketContainer)32 Session (javax.websocket.Session)31 Host (org.apache.catalina.Host)30 Container (org.apache.catalina.Container)26 ArrayList (java.util.ArrayList)25 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)24