You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Matt Parker <mp...@voyanttech.com> on 2003/01/04 01:03:27 UTC

[PATCH] forward instead of redirect for welcome files

I'd like to suggest that catalina perform a forward, rather than a
redirect, for requests that end with '/'. With a redirect, special
configuration is necessary for proxy servers to work correctly. Also, a
forward doesn't require an additional round trip to the client--a
redirect must get back to the client and the client then issues a new
request. I've tested this under Linux. Thanks!

Matt


--- DefaultServlet.java	2003-01-03 16:20:23.000000000 -0700
+++ DefaultServlet.java.new	2003-01-03 16:20:18.000000000 -0700
@@ -942,26 +942,18 @@
 
             if (!request.getRequestURI().endsWith("/")) {
                 String redirectPath = path;
-                String contextPath = request.getContextPath();
-                if ((contextPath != null) && (!contextPath.equals("/"))) {
-                    redirectPath = contextPath + redirectPath;
-                }
                 if (!(redirectPath.endsWith("/")))
                     redirectPath = redirectPath + "/";
                 redirectPath = appendParameters(request, redirectPath);
-                response.sendRedirect(redirectPath);
+                request.getRequestDispatcher(redirectPath).forward(request, response);
                 return;
             }
 
             ResourceInfo welcomeFileInfo = checkWelcomeFiles(path, resources);
             if (welcomeFileInfo != null) {
                 String redirectPath = welcomeFileInfo.path;
-                String contextPath = request.getContextPath();
-                if ((contextPath != null) && (!contextPath.equals("/"))) {
-                    redirectPath = contextPath + redirectPath;
-                }
                 redirectPath = appendParameters(request, redirectPath);
-                response.sendRedirect(redirectPath);
+                request.getRequestDispatcher(redirectPath).forward(request, response);
                 return;
             }




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
On Mon, 2003-01-06 at 17:11, Matt Parker wrote:
> On Mon, 2003-01-06 at 17:03, Tim Funk wrote:
> > If a trailing / is not present, then performing a 
> > RequestDispatcher.forward will break all relative references (for the 
> > web browser)
> > 
> > -Tim
> > 
> 
> It doesn't forward until after it appends the trailing slash, so I think
> it's okay on that front.
> 

No, you're right. It's broken. Crud.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
On Mon, 2003-01-06 at 17:03, Tim Funk wrote:
> If a trailing / is not present, then performing a 
> RequestDispatcher.forward will break all relative references (for the 
> web browser)
> 
> -Tim
> 

It doesn't forward until after it appends the trailing slash, so I think
it's okay on that front.




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Tim Funk <fu...@joedog.org>.
If a trailing / is not present, then performing a 
RequestDispatcher.forward will break all relative references (for the 
web browser)

-Tim

Costin Manolache wrote:
> Matt Parker wrote:
> 
> 
>>On Mon, 2003-01-06 at 14:43, Hans Bergsten wrote:
>>
>>>Okay, that's different. Maybe I misread your patch, but to me it looked
>>>as if you changed the behavior when there's no trailing slash.
>>
>>Actually my patch is forwarding under both circumstances, but according
>>to SRV.9.10 of Servlet 2.4, this is actually correct (phew! :)
> 
> 
> Forwarding if there is no ending "/" doesn't seem right. I'm very sure 
> anchors will be broken.
> 
> It may be a good idea to include a modified patch that makes this optional -
> but IMO redirect should remain the default. But please verify that anchors
> work, including cases where the welcome file is in another directory.
> 
> 
> 
> Costin
> 
>  


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
> Verified the following:
> 
> http://foo/bar#anchor
> http://foo/bar/#anchor
> 
> with a welcome-file of:
> test/test.jsp
> 
> and was correctly forwarded to:
> 
> http://foo/bar/test/test.jsp#anchor
> 
> 

okay, I was a little premature (no jokes please). if the welcome file
itself has a relative link off of it, then it breaks. so that <a
href='foo.jsp'> on test.jsp will try to go to http://foo/bar/foo.jsp
rather than http://foo/bar/test/foo.jsp

i'll see if i can fix that. sorry...

Matt



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
On Mon, 2003-01-06 at 15:28, Costin Manolache wrote:
> Matt Parker wrote:
> 
> > On Mon, 2003-01-06 at 14:43, Hans Bergsten wrote:
> >> Okay, that's different. Maybe I misread your patch, but to me it looked
> >> as if you changed the behavior when there's no trailing slash.
> > 
> > Actually my patch is forwarding under both circumstances, but according
> > to SRV.9.10 of Servlet 2.4, this is actually correct (phew! :)
> 
> Forwarding if there is no ending "/" doesn't seem right. I'm very sure 
> anchors will be broken.
> 

The code to append a trailing / before it does anything else (assuming
it's a directory) is already present in the default servlet--my patch
forwards after this happens. Maybe I should have included more context
within the patch to make this clearer--sorry, I can see why this would
be so controversial without that key point. Otherwise it would
definitely break. The patch occurs after the line:

if (resourceInfo.collection) {
...
}

which means that it has already ruled that the request is a directory.

> It may be a good idea to include a modified patch that makes this optional -
> but IMO redirect should remain the default. But please verify that anchors
> work, including cases where the welcome file is in another directory.
> 

Verified the following:

http://foo/bar#anchor
http://foo/bar/#anchor

with a welcome-file of:
test/test.jsp

and was correctly forwarded to:

http://foo/bar/test/test.jsp#anchor


Matt




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Costin Manolache <cm...@yahoo.com>.
Matt Parker wrote:

> On Mon, 2003-01-06 at 14:43, Hans Bergsten wrote:
>> Okay, that's different. Maybe I misread your patch, but to me it looked
>> as if you changed the behavior when there's no trailing slash.
> 
> Actually my patch is forwarding under both circumstances, but according
> to SRV.9.10 of Servlet 2.4, this is actually correct (phew! :)

Forwarding if there is no ending "/" doesn't seem right. I'm very sure 
anchors will be broken.

It may be a good idea to include a modified patch that makes this optional -
but IMO redirect should remain the default. But please verify that anchors
work, including cases where the welcome file is in another directory.



Costin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
On Mon, 2003-01-06 at 14:43, Hans Bergsten wrote:
> Okay, that's different. Maybe I misread your patch, but to me it looked
> as if you changed the behavior when there's no trailing slash.

Actually my patch is forwarding under both circumstances, but according
to SRV.9.10 of Servlet 2.4, this is actually correct (phew! :)

Matt



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Hans Bergsten <ha...@gefionsoftware.com>.
Matt Parker wrote:
> On Mon, 2003-01-06 at 12:14, Hans Bergsten wrote:
> 
>>Matt Parker wrote:
>>
>>>I'd like to suggest that catalina perform a forward, rather than a
>>>redirect, for requests that end with '/'. With a redirect, special
>>>configuration is necessary for proxy servers to work correctly. Also, a
>>>forward doesn't require an additional round trip to the client--a
>>>redirect must get back to the client and the client then issues a new
>>>request. I've tested this under Linux. Thanks!
>>
>>You mean requests that do _not_ end with '/', right? Unfortunatly,
>>you must do a redirect in this case so that the browser can resolve
>>relative paths in the page correctly. If you use a forward, it can't
>>do so correctly.
>>
>>Hans
>>
> 
> 
> 
> No, I mean requests that _do_ end with a trailing slash, but that should
> be resolved to one of the files specified in the web application's
> <welcome-file-list>. This is similar to Apache's DirectoryIndex
> directive. Maybe the following Apache documentation snippet can explain
> it more clearly than I can:
> 
> 	"The DirectoryIndex directive sets the list of resources
> 	to look for, when the client requests an index of the directory
> 	by specifying a / at the end of the a directory name."
> 
> For Apache, this is index.html by default. When Apache receives a
> trailing slash (e.g. /foo/bar/), it resolves index.html and returns it.
> Note that it does _not_ send a redirect to index.html. The redirect
> occurs only when there is no trailing slash at the end of a directory
> request (e.g. /foo/bar is redirected to /foo/bar/, which is then
> resolved to index.html)
> 
> So tomcat's behavior is actually going against what Apache does.
> 
> Hope I'm explaining it correctly.

Okay, that's different. Maybe I misread your patch, but to me it looked
as if you changed the behavior when there's no trailing slash.

Anyway, I leave it to Costin to decide if it can be applied safely or
not.

Hans
-- 
Hans Bergsten                                <ha...@gefionsoftware.com>
Gefion Software                       <http://www.gefionsoftware.com/>
Author of O'Reilly's "JavaServer Pages", covering JSP 1.2 and JSTL 1.0
Details at                                    <http://TheJSPBook.com/>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Costin Manolache <cm...@yahoo.com>.
Matt Parker wrote:

>> The welcome-file-list can include more than index.html - you may have
>> foo/index.html, etc ( i.e. things in other dirs ). That means #anchors
>> would break if we don't do redirect.
> 
> This argument would apply equally to Apache's current implementation.
> You can specify multiple index files with the DirectoryIndex directive.
> So since Apache doesn't redirect, this must not be the case.

Well, it seems I'm wrong - Apache also allows relative paths in 
DirectoryIndex. 

And if the directory ends with "/", the anchors would probably work.

My mistake.


>> A second reason for doing redirects: a redirect will let the web server
>> handle the file serving ( if the index.html is a static file - or some
>> resource that apache can handle ).
> 
> Then wouldn't that be specified in the web server's config? Also,
> redirecting the client to make yet another request couldn't possibly be
> faster than simply serving the static file.

Not necesarily faster - but if the file is a .shtml or .php - apache may
handle it better. 
Probably not a very good reason ( since not too many people are mixing java 
with non-java pages ).


>> A third reason: it's safer :-)
> 
> ?? how is it safer?

More predictible. 


> I don't mean to be argumentative, but I really think that it's The Right
> Way to do it. However I'll defer to the Tomcat team (I guess I don't
> have a choice :)

If I remember corectly - this was how the first implementation of welcome 
files worked ( long, long ago ). After several strange bugs it changed to
do redirects. 

Tomcat3.3 has an option - useInternal - that will change the behavior (
I think it defaults to false ).

It could be a good idea to add this option in 5.0 ( or 4.1 ). 

Costin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
On Mon, 2003-01-06 at 12:57, Costin Manolache wrote:

> The problem is that once again the servlet spec defines a behavior that 
> is different from the common practices on web servers.
> 

I don't see that this particular behavior is actually specified, unless
I'm looking in the wrong place. I think it leaves it open for the
"default" servlet to interpret.


> The welcome-file-list can include more than index.html - you may have
> foo/index.html, etc ( i.e. things in other dirs ). That means #anchors 
> would break if we don't do redirect.

This argument would apply equally to Apache's current implementation.
You can specify multiple index files with the DirectoryIndex directive.
So since Apache doesn't redirect, this must not be the case.

> A second reason for doing redirects: a redirect will let the web server
> handle the file serving ( if the index.html is a static file - or some 
> resource that apache can handle ).

Then wouldn't that be specified in the web server's config? Also,
redirecting the client to make yet another request couldn't possibly be
faster than simply serving the static file.

> 
> A third reason: it's safer :-)

?? how is it safer? 

> 
> Costin

I don't mean to be argumentative, but I really think that it's The Right
Way to do it. However I'll defer to the Tomcat team (I guess I don't
have a choice :)

Cheers,

Matt



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Costin Manolache <cm...@yahoo.com>.
Matt Parker wrote:

> 
> On Mon, 2003-01-06 at 12:14, Hans Bergsten wrote:
>> Matt Parker wrote:
>> > I'd like to suggest that catalina perform a forward, rather than a
>> > redirect, for requests that end with '/'. With a redirect, special
>> > configuration is necessary for proxy servers to work correctly. Also, a
>> > forward doesn't require an additional round trip to the client--a
>> > redirect must get back to the client and the client then issues a new
>> > request. I've tested this under Linux. Thanks!
>> 
>> You mean requests that do _not_ end with '/', right? Unfortunatly,
>> you must do a redirect in this case so that the browser can resolve
>> relative paths in the page correctly. If you use a forward, it can't
>> do so correctly.

> No, I mean requests that _do_ end with a trailing slash, but that should
> be resolved to one of the files specified in the web application's
> <welcome-file-list>. This is similar to Apache's DirectoryIndex
> directive. Maybe the following Apache documentation snippet can explain
> it more clearly than I can:
> 
> "The DirectoryIndex directive sets the list of resources
> to look for, when the client requests an index of the directory
> by specifying a / at the end of the a directory name."
> 
> For Apache, this is index.html by default. When Apache receives a
> trailing slash (e.g. /foo/bar/), it resolves index.html and returns it.
> Note that it does _not_ send a redirect to index.html. The redirect
> occurs only when there is no trailing slash at the end of a directory
> request (e.g. /foo/bar is redirected to /foo/bar/, which is then
> resolved to index.html)
> 
> So tomcat's behavior is actually going against what Apache does.


The problem is that once again the servlet spec defines a behavior that 
is different from the common practices on web servers.

The welcome-file-list can include more than index.html - you may have
foo/index.html, etc ( i.e. things in other dirs ). That means #anchors 
would break if we don't do redirect.

A second reason for doing redirects: a redirect will let the web server
handle the file serving ( if the index.html is a static file - or some 
resource that apache can handle ).

A third reason: it's safer :-)

Costin







--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
On Mon, 2003-01-06 at 12:14, Hans Bergsten wrote:
> Matt Parker wrote:
> > I'd like to suggest that catalina perform a forward, rather than a
> > redirect, for requests that end with '/'. With a redirect, special
> > configuration is necessary for proxy servers to work correctly. Also, a
> > forward doesn't require an additional round trip to the client--a
> > redirect must get back to the client and the client then issues a new
> > request. I've tested this under Linux. Thanks!
> 
> You mean requests that do _not_ end with '/', right? Unfortunatly,
> you must do a redirect in this case so that the browser can resolve
> relative paths in the page correctly. If you use a forward, it can't
> do so correctly.
> 
> Hans
> 


No, I mean requests that _do_ end with a trailing slash, but that should
be resolved to one of the files specified in the web application's
<welcome-file-list>. This is similar to Apache's DirectoryIndex
directive. Maybe the following Apache documentation snippet can explain
it more clearly than I can:

	"The DirectoryIndex directive sets the list of resources
	to look for, when the client requests an index of the directory
	by specifying a / at the end of the a directory name."

For Apache, this is index.html by default. When Apache receives a
trailing slash (e.g. /foo/bar/), it resolves index.html and returns it.
Note that it does _not_ send a redirect to index.html. The redirect
occurs only when there is no trailing slash at the end of a directory
request (e.g. /foo/bar is redirected to /foo/bar/, which is then
resolved to index.html)

So tomcat's behavior is actually going against what Apache does.

Hope I'm explaining it correctly.

Matt



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Hans Bergsten <ha...@gefionsoftware.com>.
Matt Parker wrote:
> I'd like to suggest that catalina perform a forward, rather than a
> redirect, for requests that end with '/'. With a redirect, special
> configuration is necessary for proxy servers to work correctly. Also, a
> forward doesn't require an additional round trip to the client--a
> redirect must get back to the client and the client then issues a new
> request. I've tested this under Linux. Thanks!

You mean requests that do _not_ end with '/', right? Unfortunatly,
you must do a redirect in this case so that the browser can resolve
relative paths in the page correctly. If you use a forward, it can't
do so correctly.

Hans

> --- DefaultServlet.java	2003-01-03 16:20:23.000000000 -0700
> +++ DefaultServlet.java.new	2003-01-03 16:20:18.000000000 -0700
> @@ -942,26 +942,18 @@
>  
>              if (!request.getRequestURI().endsWith("/")) {
>                  String redirectPath = path;
> -                String contextPath = request.getContextPath();
> -                if ((contextPath != null) && (!contextPath.equals("/"))) {
> -                    redirectPath = contextPath + redirectPath;
> -                }
>                  if (!(redirectPath.endsWith("/")))
>                      redirectPath = redirectPath + "/";
>                  redirectPath = appendParameters(request, redirectPath);
> -                response.sendRedirect(redirectPath);
> +                request.getRequestDispatcher(redirectPath).forward(request, response);
>                  return;
>              }
>  
>              ResourceInfo welcomeFileInfo = checkWelcomeFiles(path, resources);
>              if (welcomeFileInfo != null) {
>                  String redirectPath = welcomeFileInfo.path;
> -                String contextPath = request.getContextPath();
> -                if ((contextPath != null) && (!contextPath.equals("/"))) {
> -                    redirectPath = contextPath + redirectPath;
> -                }
>                  redirectPath = appendParameters(request, redirectPath);
> -                response.sendRedirect(redirectPath);
> +                request.getRequestDispatcher(redirectPath).forward(request, response);
>                  return;
>              }
> 
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 

-- 
Hans Bergsten                                <ha...@gefionsoftware.com>
Gefion Software                       <http://www.gefionsoftware.com/>
Author of O'Reilly's "JavaServer Pages", covering JSP 1.2 and JSTL 1.0
Details at                                    <http://TheJSPBook.com/>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Matt Parker <mp...@voyanttech.com>.
On Fri, 2003-01-03 at 17:14, Jon Scott Stevens wrote:
> on 2003/1/3 4:03 PM, "Matt Parker" <mp...@voyanttech.com> wrote:
> 
> > I'd like to suggest that catalina perform a forward, rather than a
> > redirect, for requests that end with '/'. With a redirect, special
> > configuration is necessary for proxy servers to work correctly. Also, a
> > forward doesn't require an additional round trip to the client--a
> > redirect must get back to the client and the client then issues a new
> > request. I've tested this under Linux. Thanks!
> > 
> > Matt
> 
> That goes against the behavior of most HTTP servers, including Apache HTTPd.
> 
> -jon
> 

It appears that Apache returns 301 only when there isn't a trailing
slash. When there is a trailing slash, it returns a 200.

Matt



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [PATCH] forward instead of redirect for welcome files

Posted by Jon Scott Stevens <jo...@latchkey.com>.
on 2003/1/3 4:03 PM, "Matt Parker" <mp...@voyanttech.com> wrote:

> I'd like to suggest that catalina perform a forward, rather than a
> redirect, for requests that end with '/'. With a redirect, special
> configuration is necessary for proxy servers to work correctly. Also, a
> forward doesn't require an additional round trip to the client--a
> redirect must get back to the client and the client then issues a new
> request. I've tested this under Linux. Thanks!
> 
> Matt

That goes against the behavior of most HTTP servers, including Apache HTTPd.

-jon


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>