xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

RequestDispatcher forward fail if served page is the the welcome file #560

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
If I want that my welcome file be a servlet that forward its rendering to a jsp 
page, the process crashes in 
com.google.inject.servlet.ServletDefinition$2.getPathInfo(ServletDefinition.java
:206)

with :

java.lang.StringIndexOutOfBoundsException: String index out of range: -9
    at java.lang.String.substring(String.java:1937)
    at java.lang.String.substring(String.java:1904)
    at com.google.inject.servlet.ServletDefinition$2.getPathInfo(ServletDefinition.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:359)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
    at com.cartser.metrics.server.core.servlet.GwtServlet.doLoad(GwtServlet.java:89)
    at com.cartser.metrics.server.core.servlet.GwtServlet.doGet(GwtServlet.java:94)

...

The problem is coming from a substring operation in getPathInfo()

  final int servletPathLength = getServletPath().length();
  pathInfo = getRequestURI().substring(getContextPath().length()).replaceAll("[/]{2,}", "/").substring(servletPathLength);

  in my case :
getServletPath() = /index.jsp
getContextPath() = /myApp
getRequestURI() = /myApp (and not /myApp/index.jsp since index.jsp is declared 
as the default welcome file)
servletPathLength = 10

so when executing the substring operation with my URI :
getRequestURI().substring(getContextPath().length()).replaceAll("[/]{2,}", "/") 
==> "/"
and "/".substring(10) ==> Exception.

Maybe that a check could be added on the length before executing the second 
substring() operation.

Original issue reported on code.google.com by nicolas....@gmail.com on 21 Oct 2010 at 8:57

GoogleCodeExporter commented 9 years ago
Just another precision :
if the url is http://localhost:8080/myApp/index.jsp ==> it works
if the url is http://localhost:8080/myApp/ ==> it fails

in my servlet module, I have got something like
serve("/index.jsp").with(GwtServlet.class);

and in my GwtServlet.class I do something like :
req.getRequestDispatcher("/init.jsp").forward(req, resp);

Original comment by nicolas....@gmail.com on 21 Oct 2010 at 9:06

GoogleCodeExporter commented 9 years ago
Hi, I have applied a tentative fix for this (r1299) for now based on your bug 
description.

It would help us a lot if you could submit a test case for this so we can be 
confident I fixed the bug you're referring to. You can place it inside 
ServletDefinitionTest.
Thanks!

Original comment by dha...@gmail.com on 21 Oct 2010 at 12:36

GoogleCodeExporter commented 9 years ago
Thanks for the fix, it works now. Perfect!
I am going to try to write a test case but it is going to be a bit hard since I 
do not really know how works the core of guice, what is the goal of the 
infoPath variable, and the current content of the ServletDefinitionTest is a 
bit obscure to me.

Original comment by nicolas....@gmail.com on 21 Oct 2010 at 2:28

GoogleCodeExporter commented 9 years ago

Original comment by sberlin on 22 Feb 2011 at 1:44