From e83bf8c8d3d8243cd198f14a69bf452e557846d7 Mon Sep 17 00:00:00 2001 From: markt Date: Fri, 8 Aug 2008 14:08:21 +0000 Subject: [PATCH] When a JSP file is deleted, return a 404 for the next request rather than serving the next request as if the file still existed and then returning 404s for the second and subsequent requests. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@683969 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/jasper/JspCompilationContext.java | 5 +- java/org/apache/jasper/compiler/Compiler.java | 2 +- java/org/apache/jasper/servlet/JspServlet.java | 64 +++++++++++++--------- .../apache/jasper/servlet/JspServletWrapper.java | 3 + 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/java/org/apache/jasper/JspCompilationContext.java b/java/org/apache/jasper/JspCompilationContext.java index 8e61efb0b..0037f3ab2 100644 --- a/java/org/apache/jasper/JspCompilationContext.java +++ b/java/org/apache/jasper/JspCompilationContext.java @@ -569,7 +569,7 @@ public class JspCompilationContext { } public boolean isRemoved() { - if (removed > 1 ) { + if (removed > 0 ) { return true; } return false; @@ -580,6 +580,9 @@ public class JspCompilationContext { public void compile() throws JasperException, FileNotFoundException { createCompiler(); if (jspCompiler.isOutDated()) { + if (isRemoved()) { + throw new FileNotFoundException(jspUri); + } try { jspCompiler.removeGeneratedFiles(); jspLoader = null; diff --git a/java/org/apache/jasper/compiler/Compiler.java b/java/org/apache/jasper/compiler/Compiler.java index 353750faa..6e4db637a 100644 --- a/java/org/apache/jasper/compiler/Compiler.java +++ b/java/org/apache/jasper/compiler/Compiler.java @@ -382,7 +382,7 @@ public abstract class Compiler { URL jspUrl = ctxt.getResource(jsp); if (jspUrl == null) { ctxt.incrementRemoved(); - return false; + return true; } URLConnection uc = jspUrl.openConnection(); if (uc instanceof JarURLConnection) { diff --git a/java/org/apache/jasper/servlet/JspServlet.java b/java/org/apache/jasper/servlet/JspServlet.java index 1c1200358..8ec102104 100644 --- a/java/org/apache/jasper/servlet/JspServlet.java +++ b/java/org/apache/jasper/servlet/JspServlet.java @@ -17,6 +17,7 @@ package org.apache.jasper.servlet; +import java.io.FileNotFoundException; import java.io.IOException; import java.lang.reflect.Constructor; import java.util.Enumeration; @@ -297,38 +298,15 @@ public class JspServlet extends HttpServlet implements PeriodicEventListener { Throwable exception, boolean precompile) throws ServletException, IOException { - JspServletWrapper wrapper = - (JspServletWrapper) rctxt.getWrapper(jspUri); + JspServletWrapper wrapper = rctxt.getWrapper(jspUri); if (wrapper == null) { synchronized(this) { - wrapper = (JspServletWrapper) rctxt.getWrapper(jspUri); + wrapper = rctxt.getWrapper(jspUri); if (wrapper == null) { // Check if the requested JSP page exists, to avoid // creating unnecessary directories and files. if (null == context.getResource(jspUri)) { - String includeRequestUri = (String) - request.getAttribute( - "javax.servlet.include.request_uri"); - if (includeRequestUri != null) { - // This file was included. Throw an exception as - // a response.sendError() will be ignored - String msg = Localizer.getMessage( - "jsp.error.file.not.found",jspUri); - // Strictly, filtering this is an application - // responsibility but just in case... - throw new ServletException( - SecurityUtil.filter(msg)); - } else { - try { - response.sendError( - HttpServletResponse.SC_NOT_FOUND, - request.getRequestURI()); - } catch (IllegalStateException ise) { - log.error(Localizer.getMessage( - "jsp.error.file.not.found", - jspUri)); - } - } + handleMissingResource(request, response, jspUri); return; } boolean isErrorPage = exception != null; @@ -339,8 +317,40 @@ public class JspServlet extends HttpServlet implements PeriodicEventListener { } } - wrapper.service(request, response, precompile); + try { + wrapper.service(request, response, precompile); + } catch (FileNotFoundException fnfe) { + handleMissingResource(request, response, jspUri); + } + + } + + + private void handleMissingResource(HttpServletRequest request, + HttpServletResponse response, String jspUri) + throws ServletException, IOException { + String includeRequestUri = + (String)request.getAttribute("javax.servlet.include.request_uri"); + + if (includeRequestUri != null) { + // This file was included. Throw an exception as + // a response.sendError() will be ignored + String msg = + Localizer.getMessage("jsp.error.file.not.found",jspUri); + // Strictly, filtering this is an application + // responsibility but just in case... + throw new ServletException(SecurityUtil.filter(msg)); + } else { + try { + response.sendError(HttpServletResponse.SC_NOT_FOUND, + request.getRequestURI()); + } catch (IllegalStateException ise) { + log.error(Localizer.getMessage("jsp.error.file.not.found", + jspUri)); + } + } + return; } diff --git a/java/org/apache/jasper/servlet/JspServletWrapper.java b/java/org/apache/jasper/servlet/JspServletWrapper.java index 8e3ed9559..2582d63d9 100644 --- a/java/org/apache/jasper/servlet/JspServletWrapper.java +++ b/java/org/apache/jasper/servlet/JspServletWrapper.java @@ -334,6 +334,9 @@ public class JspServletWrapper { } else { throw ex; } + } catch (FileNotFoundException fnfe) { + // File has been removed. Let caller handle this. + throw fnfe; } catch (IOException ex) { if (options.getDevelopment()) { throw handleJspException(ex); -- 2.11.0