You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Peter Warren <el...@yahoo.com> on 2006/11/03 18:17:46 UTC

virtual host getRealPath

 I'm trying to figure out why I get repeated directory names when calling
"application.getRealPath(request.getRequestURI())" from an index.jsp
file. Clearly there's something about virtual hosts and contexts that
I'm missing.

Using Tomcat 5.5 & 6.0 in standalone mode under Windows XP, I see the
following behavior:

A request for the url: http://www.nomad.org/gallery/

yields a real path of:

C:\Documents and Settings\Peter\My Documents\dev\webapps\www.nomad.org\gallery\gallery

(note the duplication of the gallery directory)

Why?

In my server.xml file I defined this virtual host:

<Host name="www.nomad.org" appBase="C:\Documents and Settings\Peter\My
Documents\dev\webapps\www.nomad.org" unpackWARs="true" autoDeploy="true"
xmlValidationfiltered="false" xmlNamespaceAware="false">
</Host>

If I don't define any contexts, the problem occurs.

The problem also occurs if I have either of the following contexts
defined for this host:

<Context path="/gallery" docBase="gallery" /> or
<Context path="/gallery" docBase="/gallery" />

If I set the gallery context to:

<Context path="/gallery" docBase="" />

the duplicate folder name goes away, but my gallery web.xml does not get
read.

Thanks for any help,
Peter

 
---------------------------------
Cheap Talk? Check out Yahoo! Messenger's low  PC-to-Phone call rates.

Re: virtual host getRealPath

Posted by Peter Warren <el...@yahoo.com>.
Chris,
   
  The application is an image gallery.  I want to be able to drop images into directories anywhere underneath the "gallery" webapp directory and have the images automatically displayed as thumbnails in a table.  Users can click on a thumbnail for the full-size image.  I have an index.jsp file that creates thumbnails for all the images in a directory and then generates the html to display them.  Since I don't want to have to drop an index.jsp file into every image directory, I set up a forwarding filter that forwards any request for a directory to "/index.jsp" (the filter ignores other request urls).  To get the real path of the directory that corresponds to the requested url, I use:
   
  String servletPath = (String) request.getAttribute("javax.servlet.forward.servlet_path");
  String realPath = application.getRealPath(servletPath);
   
  > You still have to add the request's URI to the end of that. 
   
  I don't seem to.  For example, a request for: http://www.nomad.org/gallery/test yields a servlet path of "/test/" and a real path of:
   
  C:\Documents and Settings\Peter\My Documents\dev\webapps\www.nomad.org\gallery\pg
   
  which is just what I need.  Now with the real path, I can process the target image directory by making thumbnails if necessary and laying out the html thumbnail table.
   
  It's up and working at http://www.nomad.org/gallery.
   
  Peter

 
---------------------------------
Cheap Talk? Check out Yahoo! Messenger's low  PC-to-Phone call rates.

Re: virtual host getRealPath

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Peter,

> After poking around a little more, it seems that the call
> 
> 
> application.getRealPath(request.getServletPath())    
> provides the proper local file path for me.  Any reason not to use that?

You still have to add the request's URI to the end of that. You are
basically making the same suggestion that I did.

The only reasons I an think of not to do this are involved with
security. You'd better make sure that remote users can't construct URLs
that can poke around on your disk. My recommendation is that if you
already know where the request is going (say, they request
/foo/bar/baz.jsp and you are processing /foo/bar/baz.jsp, then just load
the resource statically yourself).

What you are doing seems most appropriate when, say, using the "path
info" of the URL to locate a resource that has been PUT in the past. In
this case, I would imagine a mapping layer between the remote user's
request and the actual path on the disk (say, /foo/bar/baz maps to
/user-files/a/b/c/1234.tiff or something like that). This way, remote
users cannot arbitrarily request resources that are on your server.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFTe+X9CaO5/Lv0PARAlZCAKCfwXjkliITKGgMXoF07oEbdHbb2wCePkfc
v7llczHqQhCVR/SHuqcEfU8=
=XbqF
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: virtual host getRealPath

Posted by Peter Warren <el...@yahoo.com>.
Hi Chris,

Thanks for your response.  After poking around a little more, it seems that the call


application.getRealPath(request.getServletPath())    
provides the proper local file path for me.  Any reason not to use that?

Peter


Christopher Schultz <ch...@christopherschultz.net> wrote:  Peter,

> I'm trying to figure out why I get repeated directory names when calling
> "application.getRealPath(request.getRequestURI())" from an index.jsp
> file. Clearly there's something about virtual hosts and contexts that
> I'm missing.

I suppose you /could/ do this, but usually URIs and directory structures
usually don't map exactly to one another.

> A request for the url: http://www.nomad.org/gallery/
> C:\Documents and Settings\Peter\My Documents\dev\webapps\www.nomad.org\gallery\gallery

Yeah. Since your URL contains the prefix "/gallery" already, it's being
added. When you call "getRealPath", you're getting something rooted in
"...\dev\webapps\www.nomad.org\gallery", which is the root of your
webapp. Since "/gallery" in the URI, too, you're doubling it.

You need to clip out the context path.

This ought to do it for you:

String path = request.getRequestURI();
if(path.startsWith(request.getContextPath()))
path = path.substring(request.getContextPath());
path = application.getRealPath(path);

Note that letting users' URIs drive files being loaded from the disk
might be considered a security risk. I don't know your deployment model
or anything like that; I just figured I'd mention it.

Hope that helps,
-chris



 
---------------------------------
Cheap Talk? Check out Yahoo! Messenger's low  PC-to-Phone call rates.

Re: virtual host getRealPath

Posted by Christopher Schultz <ch...@christopherschultz.net>.
Peter,

>  I'm trying to figure out why I get repeated directory names when calling
> "application.getRealPath(request.getRequestURI())" from an index.jsp
> file. Clearly there's something about virtual hosts and contexts that
> I'm missing.

I suppose you /could/ do this, but usually URIs and directory structures
usually don't map exactly to one another.

> A request for the url: http://www.nomad.org/gallery/
> C:\Documents and Settings\Peter\My Documents\dev\webapps\www.nomad.org\gallery\gallery

Yeah. Since your URL contains the prefix "/gallery" already, it's being
added. When you call "getRealPath", you're getting something rooted in
"...\dev\webapps\www.nomad.org\gallery", which is the root of your
webapp. Since "/gallery" in the URI, too, you're doubling it.

You need to clip out the context path.

This ought to do it for you:

String path = request.getRequestURI();
if(path.startsWith(request.getContextPath()))
    path = path.substring(request.getContextPath());
path = application.getRealPath(path);

Note that letting users' URIs drive files being loaded from the disk
might be considered a security risk. I don't know your deployment model
or anything like that; I just figured I'd mention it.

Hope that helps,
-chris