tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Libbrecht <p...@math.uqam.ca>
Subject About serving zips, here it is !
Date Sun, 04 Jun 2000 00:42:28 GMT

Hi there,

A month or so ago, I asked questions about serving zipped directories as
files... could find no good answer. In the meantime, I found and made it, 
here's the very tiny little servlet (note: it's complete) :


import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
public class ServeFromResource extends HttpServlet { 
 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException 
    { 

	String resourceName = "public" + request.getPathInfo();
	String type = "?/?";
	if ( resourceName.endsWith(".html") || resourceName.endsWith(".htm") )
		type = "text/html";
	if ( resourceName.endsWith(".xml") )
		type = "text/xml";
	if ( resourceName.endsWith(".xsl") )
		type = "text/xsl";
	if ( resourceName.endsWith(".dtd") )
		type = "text/dtd";
	else if (resourceName.endsWith(".txt") || resourceName.endsWith(".text") )
		type = "text/plain";
	else if (resourceName.endsWith(".gif")  )
		type = "image/gif";
	else if (resourceName.endsWith(".jpeg") || resourceName.endsWith(".jpg") )
		type = "image/jpeg";
		
	System.out.println("Serving for resource : -->" + resourceName + "<--");
	InputStream in = getClass().getResourceAsStream( resourceName  );
	if ( in == null ) {
		response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		System.out.println("-------- The resource did not exit. -------------");
		}
	else {
	        response.setContentType(type); 
	        OutputStream out = response.getOutputStream(); 
		int r=0;
		while( ( r = in.read() ) != -1 ) {
			out.write((byte) r);
			}
		System.out.println("-------- Served " + resourceName);
		}
	

	}
}


Some remarks here:
- how to make it work ?? First make sure the zip-file you want to publish is in 
the classpath and that everything inside that zip file is contained in a 
top-level folder named "public". Then simply invoke that servlet with 
supplementary path the path to your desired file, inside the zip, not including 
the public path.
Something of the form: http://youserv.er/myServlet/index.html.

- this can be viewed as a huge security hole as it serves any files accessible 
to the classpath. Well, not any, just the one starting at a directory called 
"public". I'd like some comments on the possibility of backtracking here along 
the folders hierarchy... I do not think that's true, but just in case...

- one drawback: no folder listing

- this has an advantage however: the zip utilities used here are the native ones 
of the VM which, I guess, are faster than the java.zip package. Just a guess.


Paul

Wishing you good disks saving
for your documentation...

Mime
View raw message