Search in sources :

Example 56 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestELInJsp method testBug57441.

/*
     * BZ https://bz.apache.org/bugzilla/show_bug.cgi?id=57441
     * Can't validate function names defined in lambdas (or via imports)
     */
@Test
public void testBug57441() throws Exception {
    getTomcatInstanceTestWebapp(false, true);
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug57441.jsp");
    String result = res.toString();
    assertEcho(result, "00-11");
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 57 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestClientCert method doTestClientCertPost.

private void doTestClientCertPost(int bodySize, boolean expectProtectedFail) throws Exception {
    Assume.assumeTrue("SSL renegotiation has to be supported for this test", TesterSupport.isRenegotiationSupported(getTomcatInstance()));
    getTomcatInstance().start();
    byte[] body = new byte[bodySize];
    Arrays.fill(body, TesterSupport.DATA);
    // Unprotected resource
    ByteChunk res = postUrl(body, "https://localhost:" + getPort() + "/unprotected");
    assertEquals("OK-" + bodySize, res.toString());
    // Protected resource
    res.recycle();
    int rc = postUrl(body, "https://localhost:" + getPort() + "/protected", res, null);
    if (expectProtectedFail) {
        assertEquals(401, rc);
    } else {
        assertEquals("OK-" + bodySize, res.toString());
    }
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Example 58 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestCookieProcessorGenerationHttp method testUtf8CookieValue.

@Test
public void testUtf8CookieValue() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.setCookieProcessor(new Rfc6265CookieProcessor());
    Tomcat.addServlet(ctx, "test", new CookieServlet("Ġ"));
    ctx.addServletMappingDecoded("/test", "test");
    tomcat.start();
    Map<String, List<String>> headers = new HashMap<>();
    ByteChunk res = new ByteChunk();
    getUrl("http://localhost:" + getPort() + "/test", res, headers);
    List<String> cookieHeaders = headers.get("Set-Cookie");
    Assert.assertEquals("There should only be one Set-Cookie header in this test", 1, cookieHeaders.size());
    // Client is assuming header is ISO-8859-1 encoding which it isn't. Turn
    // the header value back into the received bytes (this isn't guaranteed
    // to work with all values but it will for this test value)
    byte[] headerBytes = cookieHeaders.get(0).getBytes(StandardCharsets.ISO_8859_1);
    // Now convert those bytes to a String using UTF-8
    String utf8Header = new String(headerBytes, StandardCharsets.UTF_8);
    Assert.assertEquals("Test=Ġ", utf8Header);
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) HashMap(java.util.HashMap) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) List(java.util.List) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 59 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class TestCookiesDefaultSysProps method testCookiesInstance.

@Override
@Test
public void testCookiesInstance() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    addServlets(tomcat);
    tomcat.start();
    ByteChunk res = getUrl("http://localhost:" + getPort() + "/invalid");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/null");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/blank");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/invalidFwd");
    assertEquals("Cookie name fail", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/invalidStrict");
    assertEquals("Cookie name ok", res.toString());
    res = getUrl("http://localhost:" + getPort() + "/valid");
    assertEquals("Cookie name ok", res.toString());
    // Need to read response headers to test version switching
    Map<String, List<String>> headers = new HashMap<>();
    getUrl("http://localhost:" + getPort() + "/switch", res, headers);
    List<String> cookieHeaders = headers.get("Set-Cookie");
    for (String cookieHeader : cookieHeaders) {
        assertEquals("name=\"val?ue\"; Version=1", cookieHeader);
    }
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) List(java.util.List) Test(org.junit.Test)

Example 60 with ByteChunk

use of org.apache.tomcat.util.buf.ByteChunk in project tomcat by apache.

the class Request method readChunkedPostBody.

/**
     * Read chunked post body.
     *
     * @return the post body as a bytes array
     * @throws IOException if an IO exception occurred
     */
protected byte[] readChunkedPostBody() throws IOException {
    ByteChunk body = new ByteChunk();
    byte[] buffer = new byte[CACHED_POST_LEN];
    int len = 0;
    while (len > -1) {
        len = getStream().read(buffer, 0, CACHED_POST_LEN);
        if (connector.getMaxPostSize() >= 0 && (body.getLength() + len) > connector.getMaxPostSize()) {
            // Too much data
            checkSwallowInput();
            throw new IllegalStateException(sm.getString("coyoteRequest.chunkedPostTooLarge"));
        }
        if (len > 0) {
            body.append(buffer, 0, len);
        }
    }
    if (body.getLength() == 0) {
        return null;
    }
    if (body.getLength() < body.getBuffer().length) {
        int length = body.getLength();
        byte[] result = new byte[length];
        System.arraycopy(body.getBuffer(), 0, result, 0, length);
        return result;
    }
    return body.getBuffer();
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk)

Aggregations

ByteChunk (org.apache.tomcat.util.buf.ByteChunk)274 Test (org.junit.Test)201 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)180 Tomcat (org.apache.catalina.startup.Tomcat)129 Context (org.apache.catalina.Context)98 File (java.io.File)49 List (java.util.List)48 AsyncContext (javax.servlet.AsyncContext)40 HashMap (java.util.HashMap)35 Wrapper (org.apache.catalina.Wrapper)22 StandardContext (org.apache.catalina.core.StandardContext)21 ArrayList (java.util.ArrayList)20 TesterContext (org.apache.tomcat.unittest.TesterContext)18 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)16 ServletRequestWrapper (javax.servlet.ServletRequestWrapper)13 ServletResponseWrapper (javax.servlet.ServletResponseWrapper)13 ServletContext (javax.servlet.ServletContext)10 WsContextListener (org.apache.tomcat.websocket.server.WsContextListener)10 TesterAccessLogValve (org.apache.catalina.valves.TesterAccessLogValve)9 InitialContext (javax.naming.InitialContext)8