Search in sources :

Example 11 with MessageBytes

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

the class Rfc6265CookieProcessor 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) {
                if (log.isDebugEnabled()) {
                    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();
            Cookie.parseCookie(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 12 with MessageBytes

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

the class TestMapper method testAddRemoveContextVersion.

@Test
public void testAddRemoveContextVersion() throws Exception {
    final String hostName = "iowejoiejfoiew";
    final int iowPos = 4;
    final String contextPath = "/foo/bar";
    final int contextPos = 2;
    MappingData mappingData = new MappingData();
    MessageBytes hostMB = MessageBytes.newInstance();
    MessageBytes uriMB = MessageBytes.newInstance();
    hostMB.setString(hostName);
    uriMB.setString("/foo/bar/blah/bobou/foo");
    // Verifying configuration created by setUp()
    Mapper.MappedHost mappedHost = mapper.hosts[iowPos];
    assertEquals(hostName, mappedHost.name);
    Mapper.MappedContext mappedContext = mappedHost.contextList.contexts[contextPos];
    assertEquals(contextPath, mappedContext.name);
    assertEquals(1, mappedContext.versions.length);
    assertEquals("0", mappedContext.versions[0].name);
    Host oldHost = mappedHost.object;
    Context oldContext = mappedContext.versions[0].object;
    assertEquals("context2", oldContext.getName());
    Context oldContext1 = mappedHost.contextList.contexts[contextPos - 1].versions[0].object;
    assertEquals("context1", oldContext1.getName());
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("blah7", mappingData.host.getName());
    assertEquals("context2", mappingData.context.getName());
    assertEquals("wrapper5", mappingData.wrapper.getName());
    mappingData.recycle();
    mapper.map(oldContext, uriMB, mappingData);
    assertEquals("wrapper5", mappingData.wrapper.getName());
    Context newContext = createContext("newContext");
    mapper.addContextVersion(hostName, oldHost, contextPath, "1", newContext, null, null, Arrays.asList(new WrapperMappingInfo[] { new WrapperMappingInfo("/", createWrapper("newContext-default"), false, false) }));
    assertEquals(2, mappedContext.versions.length);
    assertEquals("0", mappedContext.versions[0].name);
    assertEquals("1", mappedContext.versions[1].name);
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("newContext", mappingData.context.getName());
    assertEquals("newContext-default", mappingData.wrapper.getName());
    mappingData.recycle();
    mapper.map(newContext, uriMB, mappingData);
    assertEquals("newContext-default", mappingData.wrapper.getName());
    mapper.removeContextVersion(oldContext, hostName, contextPath, "0");
    assertEquals(1, mappedContext.versions.length);
    assertEquals("1", mappedContext.versions[0].name);
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("newContext", mappingData.context.getName());
    assertEquals("newContext-default", mappingData.wrapper.getName());
    mappingData.recycle();
    mapper.map(newContext, uriMB, mappingData);
    assertEquals("newContext-default", mappingData.wrapper.getName());
    mapper.removeContextVersion(oldContext, hostName, contextPath, "1");
    assertNotSame(mappedContext, mappedHost.contextList.contexts[contextPos]);
    assertEquals("/foo/bar/bla", mappedHost.contextList.contexts[contextPos].name);
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("context1", mappingData.context.getName());
    assertEquals("context1-defaultWrapper", mappingData.wrapper.getName());
    mappingData.recycle();
    mapper.map(oldContext1, uriMB, mappingData);
    assertEquals("context1-defaultWrapper", mappingData.wrapper.getName());
    mapper.addContextVersion(hostName, oldHost, contextPath, "0", newContext, null, null, Arrays.asList(new WrapperMappingInfo[] { new WrapperMappingInfo("/", createWrapper("newContext-defaultWrapper2"), false, false) }));
    mappedContext = mappedHost.contextList.contexts[contextPos];
    assertEquals(contextPath, mappedContext.name);
    assertEquals(1, mappedContext.versions.length);
    assertEquals("0", mappedContext.versions[0].name);
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("newContext", mappingData.context.getName());
    assertEquals("newContext-defaultWrapper2", mappingData.wrapper.getName());
    mappingData.recycle();
    mapper.map(newContext, uriMB, mappingData);
    assertEquals("newContext-defaultWrapper2", mappingData.wrapper.getName());
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) Test(org.junit.Test) LoggingBaseTest(org.apache.catalina.startup.LoggingBaseTest)

Example 13 with MessageBytes

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

the class TestMapper method testMap.

@Test
public void testMap() throws Exception {
    MappingData mappingData = new MappingData();
    MessageBytes host = MessageBytes.newInstance();
    host.setString("iowejoiejfoiew");
    MessageBytes wildcard = MessageBytes.newInstance();
    wildcard.setString("foo.net");
    MessageBytes alias = MessageBytes.newInstance();
    alias.setString("iowejoiejfoiew_alias");
    MessageBytes uri = MessageBytes.newInstance();
    uri.setString("/foo/bar/blah/bobou/foo");
    uri.toChars();
    uri.getCharChunk().setLimit(-1);
    mapper.map(host, uri, null, mappingData);
    assertEquals("blah7", mappingData.host.getName());
    assertEquals("context2", mappingData.context.getName());
    assertEquals("wrapper5", mappingData.wrapper.getName());
    assertEquals("/foo/bar", mappingData.contextPath.toString());
    assertEquals("/blah/bobou", mappingData.wrapperPath.toString());
    assertEquals("/foo", mappingData.pathInfo.toString());
    assertTrue(mappingData.redirectPath.isNull());
    mappingData.recycle();
    uri.recycle();
    uri.setString("/foo/bar/bla/bobou/foo");
    uri.toChars();
    uri.getCharChunk().setLimit(-1);
    mapper.map(host, uri, null, mappingData);
    assertEquals("blah7", mappingData.host.getName());
    assertEquals("context3", mappingData.context.getName());
    assertEquals("wrapper7", mappingData.wrapper.getName());
    assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    assertEquals("/bobou", mappingData.wrapperPath.toString());
    assertEquals("/foo", mappingData.pathInfo.toString());
    assertTrue(mappingData.redirectPath.isNull());
    mappingData.recycle();
    uri.recycle();
    uri.setString("/foo/bar/bla/bobou/foo");
    uri.toChars();
    uri.getCharChunk().setLimit(-1);
    mapper.map(wildcard, uri, null, mappingData);
    assertEquals("blah16", mappingData.host.getName());
    assertEquals("context4", mappingData.context.getName());
    assertEquals("context4-defaultWrapper", mappingData.wrapper.getName());
    assertEquals("", mappingData.contextPath.toString());
    assertEquals("/foo/bar/bla/bobou/foo", mappingData.wrapperPath.toString());
    assertTrue(mappingData.pathInfo.isNull());
    assertTrue(mappingData.redirectPath.isNull());
    mappingData.recycle();
    uri.setString("/foo/bar/bla/bobou/foo");
    uri.toChars();
    uri.getCharChunk().setLimit(-1);
    mapper.map(alias, uri, null, mappingData);
    assertEquals("blah7", mappingData.host.getName());
    assertEquals("context3", mappingData.context.getName());
    assertEquals("wrapper7", mappingData.wrapper.getName());
    assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    assertEquals("/bobou", mappingData.wrapperPath.toString());
    assertEquals("/foo", mappingData.pathInfo.toString());
    assertTrue(mappingData.redirectPath.isNull());
}
Also used : MessageBytes(org.apache.tomcat.util.buf.MessageBytes) Test(org.junit.Test) LoggingBaseTest(org.apache.catalina.startup.LoggingBaseTest)

Example 14 with MessageBytes

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

the class TestMapper method testContextListConcurrencyBug56653.

@Test
public void testContextListConcurrencyBug56653() throws Exception {
    final Host host = createHost("localhost");
    final Context contextRoot = createContext("ROOT");
    final Context context1 = createContext("foo");
    final Context context2 = createContext("foo#bar");
    final Context context3 = createContext("foo#bar#bla");
    final Context context4 = createContext("foo#bar#bla#baz");
    mapper.addHost("localhost", new String[] { "alias" }, host);
    mapper.setDefaultHostName("localhost");
    mapper.addContextVersion("localhost", host, "", "0", contextRoot, new String[0], null, null);
    mapper.addContextVersion("localhost", host, "/foo", "0", context1, new String[0], null, null);
    mapper.addContextVersion("localhost", host, "/foo/bar", "0", context2, new String[0], null, null);
    mapper.addContextVersion("localhost", host, "/foo/bar/bla", "0", context3, new String[0], null, null);
    mapper.addContextVersion("localhost", host, "/foo/bar/bla/baz", "0", context4, new String[0], null, null);
    final AtomicBoolean running = new AtomicBoolean(true);
    Thread t = new Thread() {

        @Override
        public void run() {
            for (int i = 0; i < 100000; i++) {
                mapper.removeContextVersion(context4, "localhost", "/foo/bar/bla/baz", "0");
                mapper.addContextVersion("localhost", host, "/foo/bar/bla/baz", "0", context4, new String[0], null, null);
            }
            running.set(false);
        }
    };
    MappingData mappingData = new MappingData();
    MessageBytes hostMB = MessageBytes.newInstance();
    hostMB.setString("localhost");
    MessageBytes aliasMB = MessageBytes.newInstance();
    aliasMB.setString("alias");
    MessageBytes uriMB = MessageBytes.newInstance();
    char[] uri = "/foo/bar/bla/bobou/foo".toCharArray();
    uriMB.setChars(uri, 0, uri.length);
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    mappingData.recycle();
    uriMB.setChars(uri, 0, uri.length);
    mapper.map(aliasMB, uriMB, null, mappingData);
    assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    t.start();
    while (running.get()) {
        mappingData.recycle();
        uriMB.setChars(uri, 0, uri.length);
        mapper.map(hostMB, uriMB, null, mappingData);
        assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
        mappingData.recycle();
        uriMB.setChars(uri, 0, uri.length);
        mapper.map(aliasMB, uriMB, null, mappingData);
        assertEquals("/foo/bar/bla", mappingData.contextPath.toString());
    }
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) Test(org.junit.Test) LoggingBaseTest(org.apache.catalina.startup.LoggingBaseTest)

Example 15 with MessageBytes

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

the class TestMapper method testReloadContextVersion.

@Test
public void testReloadContextVersion() throws Exception {
    final String hostName = "iowejoiejfoiew";
    final int iowPos = 4;
    final String contextPath = "/foo/bar";
    final int contextPos = 2;
    MappingData mappingData = new MappingData();
    MessageBytes hostMB = MessageBytes.newInstance();
    MessageBytes uriMB = MessageBytes.newInstance();
    hostMB.setString(hostName);
    uriMB.setString("/foo/bar/blah/bobou/foo");
    // Verifying configuration created by setUp()
    Mapper.MappedHost mappedHost = mapper.hosts[iowPos];
    assertEquals(hostName, mappedHost.name);
    Mapper.MappedContext mappedContext = mappedHost.contextList.contexts[contextPos];
    assertEquals(contextPath, mappedContext.name);
    assertEquals(1, mappedContext.versions.length);
    assertEquals("0", mappedContext.versions[0].name);
    Host oldHost = mappedHost.object;
    Context oldContext = mappedContext.versions[0].object;
    assertEquals("context2", oldContext.getName());
    Context oldContext1 = mappedHost.contextList.contexts[contextPos - 1].versions[0].object;
    assertEquals("context1", oldContext1.getName());
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("blah7", mappingData.host.getName());
    assertEquals("context2", mappingData.context.getName());
    assertEquals("wrapper5", mappingData.wrapper.getName());
    mappingData.recycle();
    mapper.map(oldContext, uriMB, mappingData);
    assertEquals("wrapper5", mappingData.wrapper.getName());
    // Mark context as paused
    // This is what happens when context reload starts
    mapper.pauseContextVersion(oldContext, hostName, contextPath, "0");
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("blah7", mappingData.host.getName());
    assertEquals("context2", mappingData.context.getName());
    // Wrapper is not mapped for incoming requests if context is paused
    assertNull(mappingData.wrapper);
    mappingData.recycle();
    mapper.map(oldContext, uriMB, mappingData);
    // Wrapper is mapped for mapping method used by forward or include dispatch
    assertEquals("wrapper5", mappingData.wrapper.getName());
    // Re-add the same context, but different list of wrappers
    // This is what happens when context reload completes
    mapper.addContextVersion(hostName, oldHost, contextPath, "0", oldContext, null, null, Arrays.asList(new WrapperMappingInfo[] { new WrapperMappingInfo("/", createWrapper("newDefaultWrapper"), false, false) }));
    mappedContext = mappedHost.contextList.contexts[contextPos];
    assertEquals(contextPath, mappedContext.name);
    assertEquals(1, mappedContext.versions.length);
    assertEquals("0", mappedContext.versions[0].name);
    mappingData.recycle();
    mapper.map(hostMB, uriMB, null, mappingData);
    assertEquals("blah7", mappingData.host.getName());
    assertEquals("context2", mappingData.context.getName());
    assertEquals("newDefaultWrapper", mappingData.wrapper.getName());
    mappingData.recycle();
    mapper.map(oldContext, uriMB, mappingData);
    assertEquals("newDefaultWrapper", mappingData.wrapper.getName());
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) StandardHost(org.apache.catalina.core.StandardHost) Host(org.apache.catalina.Host) Test(org.junit.Test) LoggingBaseTest(org.apache.catalina.startup.LoggingBaseTest)

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