Author: markt Date: Mon Jul 10 20:48:32 2017 New Revision: 1801530 URL: http://svn.apache.org/viewvc?rev=1801530&view=rev Log: Correct a regression in the fix for bug 49464 that could cause an incorrect Content-Length header to be sent by the DefaultServlet if the encoding of a static is not consistent with the encoding of the response.
Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java 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=1801530&r1=1801529&r2=1801530&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java (original) +++ tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java Mon Jul 10 20:48:32 2017 @@ -962,6 +962,18 @@ public class DefaultServlet extends Http ranges = FULL; } + String outputEncoding = response.getCharacterEncoding(); + Charset charset = B2CConverter.getCharset(outputEncoding); + boolean conversionRequired; + if (isText(contentType) && !charset.equals(fileEncodingCharset)) { + conversionRequired = true; + // Conversion often results fewer/more/different bytes. + // That does not play nicely with range requests. + ranges = FULL; + } else { + conversionRequired = false; + } + if (resource.isDirectory() || isError || ( (ranges == null || ranges.isEmpty()) @@ -981,8 +993,8 @@ public class DefaultServlet extends Http log("DefaultServlet.serveFile: contentLength=" + contentLength); // Don't set a content length if something else has already - // written to the response. - if (contentWritten == 0) { + // written to the response or if conversion will be taking place + if (contentWritten == 0 && !conversionRequired) { response.setContentLengthLong(contentLength); } } @@ -1010,9 +1022,21 @@ public class DefaultServlet extends Http } else { // Output is content of resource // Check to see if conversion is required - String outputEncoding = response.getCharacterEncoding(); - Charset charset = B2CConverter.getCharset(outputEncoding); - if (!isText(contentType) || charset.equals(fileEncodingCharset)) { + if (conversionRequired) { + // A conversion is required from fileEncoding to + // response encoding + byte[] resourceBody = resource.getContent(); + InputStream source; + if (resourceBody == null) { + source = resource.getInputStream(); + } else { + source = new ByteArrayInputStream(resourceBody); + } + OutputStreamWriter osw = new OutputStreamWriter(ostream, charset); + PrintWriter pw = new PrintWriter(osw); + copy(source, pw, fileEncoding); + pw.flush(); + } else { if (!checkSendfile(request, response, resource, contentLength, null)) { // sendfile not possible so check if resource @@ -1027,20 +1051,6 @@ public class DefaultServlet extends Http ostream.write(resourceBody); } } - } else { - // A conversion is required from fileEncoding to - // response encoding - byte[] resourceBody = resource.getContent(); - InputStream source; - if (resourceBody == null) { - source = resource.getInputStream(); - } else { - source = new ByteArrayInputStream(resourceBody); - } - OutputStreamWriter osw = new OutputStreamWriter(ostream, charset); - PrintWriter pw = new PrintWriter(osw); - copy(source, pw, fileEncoding); - pw.flush(); } } // If a stream was configured, it needs to be copied to Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1801530&r1=1801529&r2=1801530&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Mon Jul 10 20:48:32 2017 @@ -62,6 +62,12 @@ <code>UnsupportedEncodingException</code> if the user agent specifies an unsupported character encoding. (markt) </fix> + <fix> + Correct a regression in the fix for <bug>49464</bug> that could cause an + incorrect <code>Content-Length</code> header to be sent by the + <code>DefaultServlet</code> if the encoding of a static is not + consistent with the encoding of the response. (markt) + </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