org.apache.tomcat.util.buf.ByteChunk Java Examples

The following examples show how to use org.apache.tomcat.util.buf.ByteChunk. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestRequest.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testLoginLogout() throws Exception{
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    LoginConfig config = new LoginConfig();
    config.setAuthMethod("BASIC");
    ctx.setLoginConfig(config);
    ctx.getPipeline().addValve(new BasicAuthenticator());

    Tomcat.addServlet(ctx, "servlet", new LoginLogoutServlet());
    ctx.addServletMappingDecoded("/", "servlet");

    TesterMapRealm realm = new TesterMapRealm();
    realm.addUser(LoginLogoutServlet.USER, LoginLogoutServlet.PWD);
    ctx.setRealm(realm);

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    Assert.assertEquals(LoginLogoutServlet.OK, res.toString());
}
 
Example #2
Source File: TestMapperWebapps.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testWelcomeFileStrict() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp");

    StandardContext ctxt = (StandardContext) tomcat.addWebapp(null, "/test",
            appDir.getAbsolutePath());
    ctxt.setReplaceWelcomeFiles(true);
    ctxt.addWelcomeFile("index.jsp");
    // Mapping for *.do is defined in web.xml
    ctxt.addWelcomeFile("index.do");

    // Simulate STRICT_SERVLET_COMPLIANCE
    ctxt.setResourceOnlyServlets("");

    tomcat.start();
    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/test/welcome-files", bc, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    Assert.assertTrue(bc.toString().contains("JSP"));

    rc = getUrl("http://localhost:" + getPort() + "/test/welcome-files/sub", bc, null);
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
}
 
Example #3
Source File: TestEncodingDetector.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testEncodedJsp() throws Exception {
    getTomcatInstanceTestWebapp(false, true);

    ByteChunk responseBody = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/test/jsp/encoding/" + jspName,
            responseBody, null);

    Assert.assertEquals(expectedResponseCode, rc);

    if (expectedResponseCode == 200) {
        // trim() to remove whitespace like new lines
        String bodyText = responseBody.toString().trim();
        if (responseBodyOK.booleanValue()) {
            Assert.assertEquals("OK", bodyText);
        } else {
            Assert.assertNotEquals("OK", bodyText);
        }
    }
}
 
Example #4
Source File: TestELInJsp.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug56612() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() +
            "/test/bug5nnnn/bug56612.jsp");

    String result = res.toString();
    Assert.assertTrue(result.contains("00-''"));
}
 
Example #5
Source File: AjpProcessor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * @deprecated Unused. Will be removed in Tomcat 9. Use
 *             {@link #doRead(ApplicationBufferHandler)}
 */
@Deprecated
@Override
public int doRead(ByteChunk chunk) throws IOException {

    if (endOfStream) {
        return -1;
    }
    if (empty) {
        if (!refillReadBuffer(true)) {
            return -1;
        }
    }
    ByteChunk bc = bodyBytes.getByteChunk();
    chunk.setBytes(bc.getBuffer(), bc.getStart(), bc.getLength());
    empty = true;
    return chunk.getLength();
}
 
Example #6
Source File: TestMapperWebapps.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testContextRoot_Bug53339() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "Bug53356", new Bug53356Servlet());
    ctx.addServletMapping("", "Bug53356");

    tomcat.start();

    ByteChunk body = getUrl("http://localhost:" + getPort());

    Assert.assertEquals("OK", body.toString());
}
 
Example #7
Source File: TestStandardWrapper.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecurityAnnotationsNoWebXmlConstraints() throws Exception {
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0-servletsecurity");
    tomcat.addWebapp(null, "", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() + "/",
            bc, null, null);

    assertTrue(bc.getLength() > 0);
    assertEquals(403, rc);
}
 
Example #8
Source File: TestAbstractHttp11Processor.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@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.addServletMapping("/test", "Bug59310");

    tomcat.start();

    ByteChunk responseBody = new ByteChunk();
    Map<String,List<String>> responseHeaders = new HashMap<String,List<String>>();

    int rc = headUrl("http://localhost:" + getPort() + "/test", responseBody,
            responseHeaders);

    assertEquals(HttpServletResponse.SC_OK, rc);
    assertEquals(0, responseBody.getLength());
    assertFalse(responseHeaders.containsKey("Content-Length"));
}
 
Example #9
Source File: TestJspConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testServlet30NoEL() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() +
            "/test/el-as-literal.jsp");

    String result = res.toString();

    Assert.assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
}
 
Example #10
Source File: TestInputBuffer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug60400() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    Context root = tomcat.addContext("", TEMP_DIR);
    Tomcat.addServlet(root, "Bug60400Servlet", new Bug60400Servlet());
    root.addServletMappingDecoded("/", "Bug60400Servlet");

    tomcat.getConnector().setProperty("appReadBufSize", "9000");
    tomcat.start();

    ByteChunk bc = new ByteChunk();
    byte[] requestBody = new byte[9500];
    Arrays.fill(requestBody, (byte) 1);
    int rc = postUrl(requestBody, "http://localhost:" + getPort() + "/", bc, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    Assert.assertEquals(requestBody.length, bc.getLength());
}
 
Example #11
Source File: TestStandardWrapper.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecurityAnnotationsWebXmlPriority() throws Exception {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0-fragments");
    tomcat.addWebapp(null, "", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc;
    rc = getUrl("http://localhost:" + getPort() +
            "/testStandardWrapper/securityAnnotationsWebXmlPriority",
            bc, null, null);

    assertTrue(bc.getLength() > 0);
    assertEquals(403, rc);
}
 
Example #12
Source File: TomcatBaseTest.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
public static int postUrl(final byte[] body, String path, ByteChunk out,
        Map<String, List<String>> reqHead,
        Map<String, List<String>> resHead) throws IOException {
        BytesStreamer s = new BytesStreamer() {
        boolean done = false;
        @Override
        public byte[] next() {
            done = true;
            return body;

        }

        @Override
        public int getLength() {
            return body!=null?body.length:0;
        }

        @Override
        public int available() {
            if (done) return 0;
            else return getLength();
        }
    };
    return postUrl(false,s,path,out,reqHead,resHead);
}
 
Example #13
Source File: TestAsyncContextImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestAsyncRequestURI(String uri) throws Exception{
    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Servlet servlet = new AsyncRequestUriServlet();
    Wrapper wrapper1 = Tomcat.addServlet(ctx, "bug57559", servlet);
    wrapper1.setAsyncSupported(true);
    ctx.addServletMappingDecoded("/", "bug57559");

    tomcat.start();

    ByteChunk body = getUrl("http://localhost:" + getPort() + uri);

    Assert.assertEquals(uri, body.toString());
}
 
Example #14
Source File: TestParserNoStrictWhitespace.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug48668b() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() +
            "/test/bug48nnn/bug48668b.jsp");
    String result = res.toString();
    assertEcho(result, "00-Hello world</p>#{foo.bar}");
    assertEcho(result, "01-Hello world</p>#{foo2");
}
 
Example #15
Source File: TestCompiler.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug49726b() throws Exception {
    getTomcatInstanceTestWebapp(false, true);

    ByteChunk res = new ByteChunk();
    Map<String,List<String>> headers = new HashMap<>();

    getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49726b.jsp",
            res, headers);

    // Check request completed
    String result = res.toString();
    assertEcho(result, "OK");

    // Check content type
    String contentType = getSingleHeader("Content-Type", headers);
    Assert.assertTrue(contentType.startsWith("text/plain"));
}
 
Example #16
Source File: TestWarDirContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Additional test following on from SPR-7350 above to check files that
 * contain JNDI reserved characters can be served when caching is disabled.
 */
@Test
public void testReservedJNDIFileNamesNoCache() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0-fragments");
    // app dir is relative to server home
    StandardContext ctxt = (StandardContext) tomcat.addWebapp(
            null, "/test", appDir.getAbsolutePath());
    ctxt.setCachingAllowed(false);

    tomcat.start();

    // Should be found in resources.jar
    ByteChunk bc = getUrl("http://localhost:" + getPort() +
            "/test/'singlequote.jsp");
    assertEquals("<p>'singlequote.jsp in resources.jar</p>",
            bc.toString());

    // Should be found in file system
    bc = getUrl("http://localhost:" + getPort() +
            "/test/'singlequote2.jsp");
    assertEquals("<p>'singlequote2.jsp in file system</p>",
            bc.toString());
}
 
Example #17
Source File: TestOut.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug54144() throws Exception {
    ByteChunk res = new ByteChunk();

    int rc = getUrl("http://localhost:" + getPort() +
            "/test/bug5nnnn/bug54144.jsp", res, null);

    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    String body = res.toString();
    Assert.assertTrue(body.contains("OK - 1"));
    Assert.assertTrue(body.contains("OK - 2"));
    Assert.assertTrue(body.contains("OK - 3"));
    Assert.assertTrue(body.contains("OK - 4"));
    Assert.assertFalse(body.contains("FAIL"));
}
 
Example #18
Source File: TestJspDocumentParser.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug54801() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() +
            "/test/bug5nnnn/bug54801a.jspx", bc, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);

    bc.recycle();
    rc = getUrl("http://localhost:" + getPort() +
            "/test/bug5nnnn/bug54801b.jspx", bc, null);
    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
}
 
Example #19
Source File: TestParser.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug49297DuplicateAttr() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.start();

    int sc = getUrl("http://localhost:" + getPort() +
            "/test/bug49nnn/bug49297DuplicateAttr.jsp", new ByteChunk(),
            new HashMap<String,List<String>>());

    assertEquals(500, sc);
}
 
Example #20
Source File: TestParser.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBug49297MultiplePageEncoding2() throws Exception {

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk res = new ByteChunk();
    int sc = getUrl("http://localhost:" + getPort() +
            "/test/bug49nnn/bug49297MultiplePageEncoding2.jsp", res,
            new HashMap<String,List<String>>());

    assertEquals(500, sc);
}
 
Example #21
Source File: TestApplicationHttpRequest.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Test
public void testParameterImmutability() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    Tomcat.addServlet(ctx, "forward", new ForwardServlet("/modify"));
    ctx.addServletMapping("/forward", "forward");

    Tomcat.addServlet(ctx, "modify", new ModifyParameterServlet());
    ctx.addServletMapping("/modify", "modify");

    tomcat.start();

    ByteChunk response = new ByteChunk();
    StringBuilder target = new StringBuilder("http://localhost:");
    target.append(getPort());
    target.append("/forward");
    int rc = getUrl(target.toString(), response, null);

    Assert.assertEquals(200, rc);
    Assert.assertEquals("OK", response.toString());
}
 
Example #22
Source File: TestNonBlockingAPI.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void doTestNonBlockingRead(boolean ignoreIsReady, boolean async) throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    NBReadServlet servlet = new NBReadServlet(ignoreIsReady, async);
    String servletName = NBReadServlet.class.getName();
    Tomcat.addServlet(ctx, servletName, servlet);
    ctx.addServletMappingDecoded("/", servletName);

    tomcat.start();

    Map<String, List<String>> resHeaders = new HashMap<>();
    int rc = postUrl(true, new DataWriter(async ? 0 : 500, async ? 2000000 : 5),
            "http://localhost:" + getPort() + "/", new ByteChunk(), resHeaders, null);

    Assert.assertEquals(HttpServletResponse.SC_OK, rc);
    if (async) {
        Assert.assertEquals(2000000 * 8, servlet.listener.body.length());
    } else {
        Assert.assertEquals(5 * 8, servlet.listener.body.length());
    }
}
 
Example #23
Source File: TestSsl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testSimpleSsl() throws Exception {
    TesterSupport.configureClientSsl();

    Tomcat tomcat = getTomcatInstance();

    File appDir = new File(getBuildDirectory(), "webapps/examples");
    org.apache.catalina.Context ctxt  = tomcat.addWebapp(
            null, "/examples", appDir.getAbsolutePath());
    ctxt.addApplicationListener(WsContextListener.class.getName());

    TesterSupport.initSsl(tomcat);

    tomcat.start();
    ByteChunk res = getUrl("https://localhost:" + getPort() +
        "/examples/servlets/servlet/HelloWorldExample");
    Assert.assertTrue(res.toString().indexOf("<a href=\"../helloworld.html\">") > 0);
    Assert.assertTrue("Checking no client issuer has been requested",
            TesterSupport.getLastClientAuthRequestedIssuerCount() == 0);
}
 
Example #24
Source File: TestReplicatedContext.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug57425() throws LifecycleException, IOException {
    Tomcat tomcat = getTomcatInstance();
    Host host = tomcat.getHost();
    if (host instanceof StandardHost) {
        ((StandardHost) host).setContextClass(ReplicatedContext.class.getName());
    }

    File root = new File("test/webapp");
    Context context = tomcat.addWebapp(host, "", root.getAbsolutePath());

    Tomcat.addServlet(context, "test", new AccessContextServlet());
    context.addServletMappingDecoded("/access", "test");

    tomcat.start();

    ByteChunk result = getUrl("http://localhost:" + getPort() + "/access");

    Assert.assertEquals("OK", result.toString());

}
 
Example #25
Source File: TestWarDirContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Additional test following on from SPR-7350 above to check files that
 * contain JNDI reserved characters can be served when caching is enabled.
 */
@Test
public void testReservedJNDIFileNamesWithCache() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir = new File("test/webapp-3.0-fragments");
    // app dir is relative to server home
    StandardContext ctxt = (StandardContext) tomcat.addWebapp(
            null, "/test", appDir.getAbsolutePath());
    ctxt.setCachingAllowed(true);

    tomcat.start();

    // Should be found in resources.jar
    ByteChunk bc = getUrl("http://localhost:" + getPort() +
            "/test/'singlequote.jsp");
    assertEquals("<p>'singlequote.jsp in resources.jar</p>",
            bc.toString());

    // Should be found in file system
    bc = getUrl("http://localhost:" + getPort() +
            "/test/'singlequote2.jsp");
    assertEquals("<p>'singlequote2.jsp in file system</p>",
            bc.toString());
}
 
Example #26
Source File: TestBasicAuthParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private void prefix(String method) {
    authHeader = new ByteChunk();
    authHeader.setBytes(HEADER, 0, HEADER.length);
    initialOffset = HEADER.length;

    String methodX = method + " ";
    byte[] methodBytes = methodX.getBytes(StandardCharsets.ISO_8859_1);

    try {
        authHeader.append(methodBytes, 0, methodBytes.length);
    }
    catch (IOException ioe) {
        throw new IllegalStateException("unable to extend ByteChunk:"
                + ioe.getMessage());
    }
}
 
Example #27
Source File: TestJspConfig.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Test
public void testServlet25NoEL() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    File appDir =
        new File("test/webapp-2.5");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() +
            "/test/el-as-literal.jsp");

    String result = res.toString();

    assertTrue(result.indexOf("<p>00-hello world</p>") > 0);
}
 
Example #28
Source File: TestRequest.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
private void doBug56501(String deployPath, String requestPath, String expected)
        throws Exception {

    // Setup Tomcat instance
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext(deployPath, null);

    Tomcat.addServlet(ctx, "servlet", new Bug56501Servelet());
    ctx.addServletMapping("/*", "servlet");

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + requestPath);
    String resultPath = res.toString();
    if (resultPath == null) {
        resultPath = "";
    }
    assertEquals(expected, resultPath);
}
 
Example #29
Source File: TestParser.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug56265() throws Exception {
    getTomcatInstanceTestWebapp(true, true);

    ByteChunk res = getUrl("http://localhost:" + getPort() +
            "/test/bug5nnnn/bug56265.jsp");

    String result = res.toString();

    Assert.assertTrue(result,
            result.contains("[1: [data-test]: [window.alert('Hello World <&>!')]]"));
    Assert.assertTrue(result,
            result.contains("[2: [data-test]: [window.alert('Hello World <&>!')]]"));
    Assert.assertTrue(result,
            result.contains("[3: [data-test]: [window.alert('Hello 'World <&>'!')]]"));
    Assert.assertTrue(result,
            result.contains("[4: [data-test]: [window.alert('Hello 'World <&>'!')]]"));
}
 
Example #30
Source File: TestGenerator.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug49799() throws Exception {

    String[] expected = { "<p style=\"color:red\">00-Red</p>",
                          "<p>01-Not Red</p>",
                          "<p style=\"color:red\">02-Red</p>",
                          "<p>03-Not Red</p>",
                          "<p style=\"color:red\">04-Red</p>",
                          "<p>05-Not Red</p>"};

    getTomcatInstanceTestWebapp(false, true);

    ByteChunk res = new ByteChunk();
    getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49799.jsp", res, null);

    // Check request completed
    String result = res.toString();
    String[] lines = result.split("\n|\r|\r\n");
    int i = 0;
    for (String line : lines) {
        if (line.length() > 0) {
            Assert.assertEquals(expected[i], line);
            i++;
        }
    }
}