tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bugzi...@apache.org
Subject DO NOT REPLY [Bug 47837] New: RequestDispatcher for root context has incorrect requestURI (extra "/" at start)
Date Mon, 14 Sep 2009 16:11:59 GMT
https://issues.apache.org/bugzilla/show_bug.cgi?id=47837

           Summary: RequestDispatcher for root context has incorrect
                    requestURI (extra "/" at start)
           Product: Tomcat 7
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P3
         Component: Catalina
        AssignedTo: dev@tomcat.apache.org
        ReportedBy: david_syer@hotmail.com


RequestDispatcher for root context has incorrect requestURI (extra "/" at
start).  Here's a slightly ugly test case:

{code}
    @Test
    public void testDispatcherWithNonRootContext() throws Exception {
        String value = getRequestURIFor("/bucket", "/bar");
        // Passes
        assertEquals("/bucket/bar", value);
    }

    @Test
    public void testDispatcherWithRootContext() throws Exception {
        String value = getRequestURIFor("/", "/bar");
        // Fails
        assertEquals("/bar", value);
    }

    private String getRequestURIFor(String root, String path) throws
IllegalArgumentException, IllegalAccessException {
        Tomcat tomcat = new Tomcat();
        StandardContext standardContext = tomcat.addContext(root,
System.getProperty("java.io.tmpdir"));
        Tomcat.addServlet(standardContext, "spam", "NoSuchClass");
        standardContext.addServletMapping("/", "spam", false);
        standardContext.getMapper().setContext("spam", null, null);
        ApplicationContext applicationContext = new ApplicationContext("/foo",
standardContext);
        RequestDispatcher requestDispatcher =
applicationContext.getRequestDispatcher(path);
        Field field = findField(requestDispatcher.getClass(), "requestURI");
        field.setAccessible(true);
        return(String) field.get(requestDispatcher);
    }

    private Field findField(Class<?> clazz, String name) {
        Assert.notNull(clazz, "Class must not be null");
        Assert.isTrue(name != null, "Name of the field must be specified");
        Class<?> searchType = clazz;
        while (!Object.class.equals(searchType) && searchType != null) {
            Field[] fields = searchType.getDeclaredFields();
            for (Field field : fields) {
                if (name.equals(field.getName())) {
                    return field;
                }
            }
            searchType = searchType.getSuperclass();
        }
        return null;
    }
{code}

Suggested fix would be to check for the length of the context path in
ApplicationContext.getRequestDispatcher() around line 430:

{code}
if (context.getPath().length()>1) {
    uriCC.append(context.getPath(), 0, context.getPath().length());
}
{code}

(add the if statement).

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message