Author: violetagg Date: Tue Jan 5 13:24:30 2016 New Revision: 1723068 URL: http://svn.apache.org/viewvc?rev=1723068&view=rev Log: Ensure that the proper file encoding if specified will be used when a readme file is served by DefaultServlet. Update findbugs false positives.
Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java tomcat/trunk/res/findbugs/filter-false-positives.xml tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java?rev=1723068&r1=1723067&r2=1723068&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java (original) +++ tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java Tue Jan 5 13:24:30 2016 @@ -936,7 +936,7 @@ public class DefaultServlet extends Http // Output via a writer so can't use sendfile or write // content directly. if (resource.isDirectory()) { - renderResult = render(getPathPrefix(request), resource); + renderResult = render(getPathPrefix(request), resource, encoding); } else { renderResult = resource.getInputStream(); } @@ -944,7 +944,7 @@ public class DefaultServlet extends Http } else { // Output is via an InputStream if (resource.isDirectory()) { - renderResult = render(getPathPrefix(request), resource); + renderResult = render(getPathPrefix(request), resource, encoding); } else { // Output is content of resource if (!checkSendfile(request, response, resource, @@ -1248,13 +1248,18 @@ public class DefaultServlet extends Http */ protected InputStream render(String contextPath, WebResource resource) throws IOException, ServletException { + return render(contextPath, resource, null); + } + + protected InputStream render(String contextPath, WebResource resource, String encoding) + throws IOException, ServletException { Source xsltSource = findXsltSource(resource); if (xsltSource == null) { - return renderHtml(contextPath, resource); + return renderHtml(contextPath, resource, encoding); } - return renderXml(contextPath, resource, xsltSource); + return renderXml(contextPath, resource, xsltSource, encoding); } @@ -1265,9 +1270,13 @@ public class DefaultServlet extends Http * @param contextPath Context path to which our internal paths are * relative */ - protected InputStream renderXml(String contextPath, - WebResource resource, - Source xsltSource) + protected InputStream renderXml(String contextPath, WebResource resource, Source xsltSource) + throws IOException, ServletException { + return renderXml(contextPath, resource, xsltSource, null); + } + + protected InputStream renderXml(String contextPath, WebResource resource, Source xsltSource, + String encoding) throws IOException, ServletException { StringBuilder sb = new StringBuilder(); @@ -1333,7 +1342,7 @@ public class DefaultServlet extends Http } sb.append("</entries>"); - String readme = getReadme(resource); + String readme = getReadme(resource, encoding); if (readme!=null) { sb.append("<readme><![CDATA["); @@ -1393,6 +1402,11 @@ public class DefaultServlet extends Http */ protected InputStream renderHtml(String contextPath, WebResource resource) throws IOException { + return renderHtml(contextPath, resource, null); + } + + protected InputStream renderHtml(String contextPath, WebResource resource, String encoding) + throws IOException { // Prepare a writer to a buffered area ByteArrayOutputStream stream = new ByteArrayOutputStream(); @@ -1512,7 +1526,7 @@ public class DefaultServlet extends Http sb.append("<HR size=\"1\" noshade=\"noshade\">"); - String readme = getReadme(resource); + String readme = getReadme(resource, encoding); if (readme!=null) { sb.append(readme); sb.append("<HR size=\"1\" noshade=\"noshade\">"); @@ -1553,17 +1567,33 @@ public class DefaultServlet extends Http * Get the readme file as a string. */ protected String getReadme(WebResource directory) { + return getReadme(directory, null); + } + + protected String getReadme(WebResource directory, String encoding) { if (readmeFile != null) { WebResource resource = resources.getResource( directory.getWebappPath() + readmeFile); if (resource.isFile()) { StringWriter buffer = new StringWriter(); - try (InputStream is = resource.getInputStream(); - InputStreamReader reader = new InputStreamReader(is)) { + InputStreamReader reader = null; + try (InputStream is = resource.getInputStream();){ + if (encoding != null) { + reader = new InputStreamReader(is, encoding); + } else { + reader = new InputStreamReader(is); + } copyRange(reader, new PrintWriter(buffer)); } catch (IOException e) { log("Failure to close reader", e); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + } + } } return buffer.toString(); } else { Modified: tomcat/trunk/res/findbugs/filter-false-positives.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/res/findbugs/filter-false-positives.xml?rev=1723068&r1=1723067&r2=1723068&view=diff ============================================================================== --- tomcat/trunk/res/findbugs/filter-false-positives.xml (original) +++ tomcat/trunk/res/findbugs/filter-false-positives.xml Tue Jan 5 13:24:30 2016 @@ -222,6 +222,16 @@ <Bug pattern="HRS_REQUEST_PARAMETER_TO_HTTP_HEADER" /> </Match> <Match> + <!-- If encoding is specified it will be used, + otherwise platform default encoding will be used --> + <Class name="org.apache.catalina.servlets.DefaultServlet"/> + <Or> + <Method name="copy"/> + <Method name="getReadme"/> + </Or> + <Bug code="Dm" /> + </Match> + <Match> <!-- Non-constant strings are configuration settings rather than client supplied --> <Class name="org.apache.catalina.session.JDBCStore" /> @@ -450,6 +460,16 @@ <Bug code="ST" /> </Match> <Match> + <!-- If encoding is specified it will be used, + otherwise platform default encoding will be used --> + <Class name="org.apache.jasper.JspC"/> + <Or> + <Method name="openWebxmlReader"/> + <Method name="openWebxmlWriter"/> + </Or> + <Bug code="Dm" /> + </Match> + <Match> <!-- Node constructors add node to parent. Local variable is used to silence an Eclipse warning --> <Class name="org.apache.jasper.compiler.ELFunctionMapper"/> Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1723068&r1=1723067&r2=1723068&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Tue Jan 5 13:24:30 2016 @@ -171,6 +171,10 @@ error page. Includes a test case based on code provided by Andy Wilkinson.(markt) </fix> + <fix> + Ensure that the proper file encoding if specified will be used when + a readme file is served by DefaultServlet. (violetagg) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org