tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From craig...@locus.apache.org
Subject cvs commit: jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets DefaultServlet.java
Date Sat, 29 Apr 2000 20:25:20 GMT
craigmcc    00/04/29 13:25:20

  Modified:    proposals/catalina/src/share/org/apache/tomcat/servlets
                        DefaultServlet.java
  Log:
  Correct handling of If-Modified-Since headers and URIs with no filename
  part (such as "http://localhost:8080").
  
  Revision  Changes    Path
  1.3       +51 -85    jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java
  
  Index: DefaultServlet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DefaultServlet.java	2000/04/29 18:24:27	1.2
  +++ DefaultServlet.java	2000/04/29 20:25:19	1.3
  @@ -1,13 +1,13 @@
   /*
  - * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java,v
1.2 2000/04/29 18:24:27 craigmcc Exp $
  - * $Revision: 1.2 $
  - * $Date: 2000/04/29 18:24:27 $
  + * $Header: /home/cvs/jakarta-tomcat/proposals/catalina/src/share/org/apache/tomcat/servlets/DefaultServlet.java,v
1.3 2000/04/29 20:25:19 craigmcc Exp $
  + * $Revision: 1.3 $
  + * $Date: 2000/04/29 20:25:19 $
    *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  + * Copyright (c) 1999 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -15,7 +15,7 @@
    * are met:
    *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  + *    notice, this list of conditions and the following disclaimer.
    *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
  @@ -23,15 +23,15 @@
    *    distribution.
    *
    * 3. The end-user documentation included with the redistribution, if
  - *    any, must include the following acknowlegement:  
  - *       "This product includes software developed by the 
  + *    any, must include the following acknowlegement:
  + *       "This product includes software developed by the
    *        Apache Software Foundation (http://www.apache.org/)."
    *    Alternately, this acknowlegement may appear in the software itself,
    *    if and wherever such third-party acknowlegements normally appear.
    *
    * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
    *    Foundation" must not be used to endorse or promote products derived
  - *    from this software without prior written permission. For written 
  + *    from this software without prior written permission. For written
    *    permission, please contact apache@apache.org.
    *
    * 5. Products derived from this software may not be called "Apache"
  @@ -59,7 +59,7 @@
    *
    * [Additional notices, if required by prior licensing conditions]
    *
  - */ 
  + */
   
   
   package org.apache.tomcat.servlets;
  @@ -91,7 +91,7 @@
    * used to serve static resources such as HTML pages and images.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.2 $ $Date: 2000/04/29 18:24:27 $
  + * @version $Revision: 1.3 $ $Date: 2000/04/29 20:25:19 $
    */
   
   public final class DefaultServlet
  @@ -166,25 +166,14 @@
        * @exception IOException if an input/output error occurs
        * @exception ServletException if a servlet-specified error occurs
        */
  -    /*
       public void doHead(HttpServletRequest request,
   		       HttpServletResponse response)
   	throws IOException, ServletException {
   
  -	// Identify the requested resource path
  -	String pathInfo = request.getPathInfo();
  -	if (debug > 0)
  -	    log("DefaultServlet.doHead:  Serving resource '" + pathInfo + "'");
  -	if (pathInfo == null) {
  -	    response.sendError(HttpServletResponse.SC_BAD_REQUEST);
  -	    return;
  -	}
  -
   	// Serve the requested resource, without the data content
   	serveResource(request, response, false);
   
       }
  -    */
   
   
       /**
  @@ -206,55 +195,6 @@
   
   
       /**
  -     * Return the date and time that the specified resource was last modified,
  -     * in milliseconds since midnight January 1, 1970 GMT.  If the time is
  -     * unknown, a negative number is returned.
  -     *
  -     * @param request The servlet request we are processing
  -     */
  -    public long getLastModified(HttpServletRequest request) {
  -
  -	// Identify the requested resource path
  -	String pathInfo = request.getPathInfo();
  -	if (debug > 0)
  -	    log("DefaultServlet.getLastModified:  Serving resource '" +
  -		pathInfo + "'");
  -	if (pathInfo == null)
  -	    return (-1);
  -
  -	// Convert the resource path to a URL
  -	URL resourceURL = null;
  -	try {
  -	    resourceURL = getServletContext().getResource(pathInfo);
  -	} catch (MalformedURLException e) {
  -	    ;
  -	}
  -	if (resourceURL == null)
  -	    return (-1);
  -	if (debug > 0)
  -	    log("DefaultServlet.getLastModified:  Corresponding URL is '" +
  -		resourceURL.toString() + "'");
  -
  -	// Optimized processing for a "file:" URL
  -	if ("file".equals(resourceURL.getProtocol())) {
  -	    File file = new File(resourceURL.getFile());
  -	    if (file == null)
  -		return (-1);
  -	    if (!file.exists() || !file.canRead())
  -		return (-1);
  -	    if (debug > 0)
  -		log("DefaultServlet.getLastModified:  Last modified on '" +
  -		    new Timestamp(file.lastModified()) + "'");
  -	    return (file.lastModified());
  -	}
  -
  -	// Currently do not know how to answer this question for other URLs
  -	return (-1);	// FIXME: getLastModified() for non-file URL
  -
  -    }
  -
  -
  -    /**
        * Initialize this servlet.
        */
       public void init() throws ServletException {
  @@ -364,16 +304,17 @@
        * Serve the specified directory as a set of hyperlinks to the
        * included files.
        *
  -     * @param requestURI Request URI of the directory being served
  -     * @param directory File object representing this directory
  +     * @param request The servlet request we are processing
        * @param response The servlet response we are creating
  +     * @param directory File object representing this directory
        * @param content Should the content be included?
        *
        * @exception IOException if an input/output error occurs
        * @exception ServletException if a servlet-specified error occurs
        */
  -    public void serveDirectory(String requestURI, File directory,
  +    public void serveDirectory(HttpServletRequest request,
   			       HttpServletResponse response,
  +			       File directory,
   			       boolean content)
   	throws IOException, ServletException {
   
  @@ -385,22 +326,24 @@
   	for (int i = 0; i < welcomes.size(); i++) {
   	    File file = new File(directory, (String) welcomes.elementAt(i));
   	    if (file.exists() && file.canRead() && file.isFile()) {
  -		serveFile(requestURI, file.getAbsolutePath(),
  -			  response, content);
  +		serveFile(request, response, file.getAbsolutePath(), content);
   		return;
   	    }
   	}
  -	
   
  +
   	// FIXME - do the directory thing
   	response.setContentType("text/html");
  +	if (!content)
  +	    return;
   	response.setBufferSize(output);
   	PrintWriter writer = response.getWriter();
   
   	writer.println("<html>");
   
   	writer.println("<head>");
  -	writer.println("<title>Directory of " + requestURI + "</title>");
  +	writer.println("<title>Directory of " + request.getRequestURI() +
  +	               "</title>");
   	writer.println("</head>");
   
   	writer.println("<body bgcolor=\"white\">");
  @@ -423,16 +366,17 @@
       /**
        * Serve the specified file, optionally including the data content.
        *
  -     * @param requestURI The request URI we are processing
  -     * @param pathname Pathname of the file to be served
  +     * @param request The servlet request we are processing
        * @param response The servlet response we are creating
  +     * @param pathname Pathname of the file to be served
        * @param content Should the content be included?
        *
        * @exception IOException if an input/output error occurs
        * @exception ServletException if a servlet-specified error occurs
        */
  -    public void serveFile(String requestURI, String pathname,
  +    public void serveFile(HttpServletRequest request,
   			  HttpServletResponse response,
  +			  String pathname,
   			  boolean content)
   	throws IOException, ServletException {
   
  @@ -443,9 +387,26 @@
   	    return;
   	}
   	if (file.isDirectory()) {
  -	    serveDirectory(requestURI, file, response, content);
  +	    serveDirectory(request, response, file, content);
   	    return;
   	}
  +	if (debug > 0)
  +	    log("DefaultServlet.serveFile: Serving '" + pathname + "'");
  +
  +	// Handle conditional GET requests
  +	long lastModified = file.lastModified();
  +	long modifiedSince = request.getDateHeader("If-Modified-Since");
  +	if ("GET".equals(request.getMethod()) && (modifiedSince >= lastModified)) {
  +	    if (debug > 0)
  +	    	log("DefaultServlet.serveFile:  lastModified='" +
  +	    	    (new Timestamp(lastModified)).toString() +
  +	    	    "', modifiedSince='" +
  +	    	    (new Timestamp(modifiedSince)).toString() +
  +	    	    "', returning 304");
  +	    response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
  +	    return;
  +        }
  +
   	InputStream istream =	// FIXME: internationalization???????
   	    new BufferedInputStream(new FileInputStream(pathname), input);
   
  @@ -453,17 +414,21 @@
   	String contentType = getServletContext().getMimeType(pathname);
   	if (contentType != null) {
   	    if (debug > 0)
  -		log("DefaultServlet.serveFile: contentType='" +
  +		log("DefaultServlet.serveFile:  contentType='" +
   		    contentType + "'");
   	    response.setContentType(contentType);
   	}
   	long contentLength = file.length();
   	if (contentLength >= 0) {
   	    if (debug > 0)
  -		log("DefaultServlet.serveFile: contentLength=" +
  +		log("DefaultServlet.serveFile:  contentLength=" +
   		    contentLength);
   	    response.setContentLength((int) contentLength);
   	}
  +	if (debug > 0)
  +	    log("DefaultServlet.serveFile:  lastModified='" +
  +	        (new Timestamp(file.lastModified())).toString() + "'");
  +	response.setDateHeader("Last-Modified", file.lastModified());
   
   	// Copy the input stream to our output stream (if requested)
   	if (content) {
  @@ -491,6 +456,8 @@
   
   	// Identify the requested resource path
   	String pathInfo = request.getPathInfo();
  +	if (pathInfo == null)
  +	    pathInfo = "/";
   	if (debug > 0) {
   	    if (content)
   		log("DefaultServlet.serveResource:  Serving resource '" +
  @@ -527,8 +494,7 @@
   
   	// Optimized processing for a "file:" URL
   	if ("file".equals(resourceURL.getProtocol())) {
  -	    serveFile(request.getRequestURI(), resourceURL.getFile(),
  -		      response, content);
  +	    serveFile(request, response, resourceURL.getFile(),content);
   	    return;
   	}
   
  
  
  

Mime
View raw message