Search in sources :

Example 31 with MessageBytes

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

the class LegacyCookieProcessor method parseCookieHeader.

@Override
public void parseCookieHeader(MimeHeaders headers, ServerCookies serverCookies) {
    if (headers == null) {
        // nothing to process
        return;
    }
    // process each "cookie" header
    int pos = headers.findHeader("Cookie", 0);
    while (pos >= 0) {
        MessageBytes cookieValue = headers.getValue(pos);
        if (cookieValue != null && !cookieValue.isNull()) {
            if (cookieValue.getType() != MessageBytes.T_BYTES) {
                Exception e = new Exception();
                // TODO: Review this in light of HTTP/2
                log.debug("Cookies: Parsing cookie as String. Expected bytes.", e);
                cookieValue.toBytes();
            }
            if (log.isDebugEnabled()) {
                log.debug("Cookies: Parsing b[]: " + cookieValue.toString());
            }
            ByteChunk bc = cookieValue.getByteChunk();
            processCookieHeader(bc.getBytes(), bc.getOffset(), bc.getLength(), serverCookies);
        }
        // search from the next position
        pos = headers.findHeader("Cookie", ++pos);
    }
}
Also used : ByteChunk(org.apache.tomcat.util.buf.ByteChunk) MessageBytes(org.apache.tomcat.util.buf.MessageBytes)

Example 32 with MessageBytes

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

the class TestLegacyCookieProcessor method testV0WithPath.

/*
     * https://bz.apache.org/bugzilla/show_bug.cgi?id=59925
     */
@Test
public void testV0WithPath() {
    LegacyCookieProcessor cp = new LegacyCookieProcessor();
    cp.setAllowHttpSepsInV0(true);
    cp.setForwardSlashIsSeparator(true);
    MimeHeaders mimeHeaders = new MimeHeaders();
    ServerCookies serverCookies = new ServerCookies(4);
    MessageBytes cookieHeaderValue = mimeHeaders.addValue("Cookie");
    byte[] bytes = "$Version=0;cname=cvalue;$Path=/example".getBytes(StandardCharsets.UTF_8);
    cookieHeaderValue.setBytes(bytes, 0, bytes.length);
    cp.parseCookieHeader(mimeHeaders, serverCookies);
    Assert.assertEquals(1, serverCookies.getCookieCount());
    for (int i = 0; i < 1; i++) {
        ServerCookie actual = serverCookies.getCookie(i);
        Assert.assertEquals(0, actual.getVersion());
        Assert.assertEquals("cname", actual.getName().toString());
        actual.getValue().getByteChunk().setCharset(StandardCharsets.UTF_8);
        Assert.assertEquals("cvalue", org.apache.tomcat.util.http.parser.Cookie.unescapeCookieValueRfc2109(actual.getValue().toString()));
        Assert.assertEquals("/example", actual.getPath().toString());
    }
}
Also used : MessageBytes(org.apache.tomcat.util.buf.MessageBytes) Test(org.junit.Test)

Aggregations

MessageBytes (org.apache.tomcat.util.buf.MessageBytes)32 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)7 Context (org.apache.catalina.Context)6 MimeHeaders (org.apache.tomcat.util.http.MimeHeaders)6 Test (org.junit.Test)6 IOException (java.io.IOException)4 Host (org.apache.catalina.Host)4 LoggingBaseTest (org.apache.catalina.startup.LoggingBaseTest)4 Pattern (java.util.regex.Pattern)3 ServletException (javax.servlet.ServletException)3 Cookie (javax.servlet.http.Cookie)3 Wrapper (org.apache.catalina.Wrapper)3 StandardContext (org.apache.catalina.core.StandardContext)3 StandardHost (org.apache.catalina.core.StandardHost)3 CharChunk (org.apache.tomcat.util.buf.CharChunk)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Principal (java.security.Principal)2 MappingData (org.apache.catalina.mapper.MappingData)2 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1