markt 2005/04/04 13:26:55 Modified: catalina/src/conf web.xml catalina/src/share/org/apache/catalina/ssi SSIServlet.java SSIServletExternalResolver.java webapps/tomcat-docs ssi-howto.xml Log: Fix bug 10385. Improve support for files that use character encodings other than the platform default with the SSI servlet. Revision Changes Path 1.61 +6 -0 jakarta-tomcat-4.0/catalina/src/conf/web.xml Index: web.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/conf/web.xml,v retrieving revision 1.60 retrieving revision 1.61 diff -u -r1.60 -r1.61 --- web.xml 18 Dec 2004 11:55:37 -0000 1.60 +++ web.xml 4 Apr 2005 20:26:55 -0000 1.61 @@ -189,6 +189,12 @@ <!-- relative to the context root, instead of --> <!-- the server root? (0=false, 1=true) [0] --> <!-- --> + <!-- inputEncoding The encoding to assume for SSI resources if --> + <!-- one is not available from the resource. --> + <!-- [Platform default] --> + <!-- --> + <!-- outputEncoding The encoding to use for the page that results --> + <!-- from the SSI processing. [UTF-8] --> <!-- --> <!-- IMPORTANT: To use the SSI servlet, you also need to rename the --> <!-- $CATALINA_HOME/server/lib/servlets-ssi.renametojar file --> 1.6 +40 -5 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/ssi/SSIServlet.java Index: SSIServlet.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/ssi/SSIServlet.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- SSIServlet.java 3 Apr 2005 18:55:30 -0000 1.5 +++ SSIServlet.java 4 Apr 2005 20:26:55 -0000 1.6 @@ -54,6 +54,12 @@ /** virtual path can be webapp-relative */ protected boolean isVirtualWebappRelative = false; + + /** Input encoding. If not specified, uses platform default */ + protected String inputEncoding = null; + + /** Output encoding. If not specified, uses platform default */ + protected String outputEncoding = "UTF-8"; //----------------- Public methods. @@ -92,6 +98,19 @@ } catch (Throwable t) { ; } + try { + inputEncoding = getServletConfig().getInitParameter("inputEncoding"); + } catch (Throwable t) { + ; + } + try { + value = getServletConfig().getInitParameter("outputEncoding"); + if (value != null) { + outputEncoding = value; + } + } catch (Throwable t) { + ; + } if (debug > 0) log("SSIServlet.init() SSI invoker started with 'debug'=" + debug); @@ -165,6 +184,12 @@ return; } + String resourceMimeType = servletContext.getMimeType(path); + if (resourceMimeType == null) { + resourceMimeType = "text/html"; + } + res.setContentType(resourceMimeType + ";charset=" + outputEncoding); + if (expires != null) { res.setDateHeader("Expires", ( new java.util.Date()).getTime() + expires.longValue() * 1000); @@ -180,8 +205,7 @@ SSIExternalResolver ssiExternalResolver = new SSIServletExternalResolver( this, req, res, - isVirtualWebappRelative, - debug ); + isVirtualWebappRelative, debug, inputEncoding); SSIProcessor ssiProcessor = new SSIProcessor( ssiExternalResolver, debug ); @@ -196,8 +220,19 @@ URLConnection resourceInfo = resource.openConnection(); InputStream resourceInputStream = resourceInfo.getInputStream(); - BufferedReader bufferedReader = - new BufferedReader(new InputStreamReader(resourceInputStream)); + + String encoding = resourceInfo.getContentEncoding(); + if (encoding == null) { + encoding = inputEncoding; + } + InputStreamReader isr; + if (encoding == null) { + isr = new InputStreamReader(resourceInputStream); + } else { + isr = new InputStreamReader(resourceInputStream, encoding); + } + BufferedReader bufferedReader = new BufferedReader(isr); + Date lastModifiedDate = new Date(resourceInfo.getLastModified()); ssiProcessor.process(bufferedReader, lastModifiedDate, printWriter); 1.5 +19 -10 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/ssi/SSIServletExternalResolver.java Index: SSIServletExternalResolver.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/ssi/SSIServletExternalResolver.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- SSIServletExternalResolver.java 30 Mar 2005 21:53:05 -0000 1.4 +++ SSIServletExternalResolver.java 4 Apr 2005 20:26:55 -0000 1.5 @@ -52,15 +52,17 @@ protected HttpServletResponse res; protected boolean isVirtualWebappRelative; protected int debug; - - public SSIServletExternalResolver( HttpServlet servlet, HttpServletRequest req, HttpServletResponse res, - boolean isVirtualWebappRelative, - int debug ) { + protected String inputEncoding; + + public SSIServletExternalResolver( HttpServlet servlet, + HttpServletRequest req, HttpServletResponse res, + boolean isVirtualWebappRelative, int debug, String inputEncoding) { this.servlet = servlet; this.req = req; this.res = res; this.isVirtualWebappRelative = isVirtualWebappRelative; this.debug = debug; + this.inputEncoding = inputEncoding; } public void log( String message, Throwable throwable ) { @@ -343,9 +345,11 @@ return fileSize; } - //We are making lots of unnecessary copies of the included data here. If someone ever complains that this - //is slow, we should connect the included stream to the print writer that SSICommand uses. - public String getFileText( String originalPath, boolean virtual ) throws IOException { + //We are making lots of unnecessary copies of the included data here. If + // someone ever complains that this is slow, we should connect the included + // stream to the print writer that SSICommand uses. + public String getFileText( String originalPath, boolean virtual + ) throws IOException { try { ServletContextAndPath csAndP = getServletContextAndPath( originalPath, virtual ); ServletContext context = csAndP.getServletContext(); @@ -365,8 +369,13 @@ responseIncludeWrapper.flushOutputStreamOrWriter(); byte[] bytes = basos.toByteArray(); - //Assume that the default encoding is what was used to encode the bytes. Questionable. - String retVal = new String( bytes ); + //Assume platform default encoding unless otherwsie specified + String retVal; + if (inputEncoding == null) { + retVal = new String( bytes ); + } else { + retVal = new String (bytes, inputEncoding); + } //make an assumption that an empty response is a failure. This is a problem if a truly empty file //were included, but not sure how else to tell. 1.3 +5 -0 jakarta-tomcat-4.0/webapps/tomcat-docs/ssi-howto.xml Index: ssi-howto.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/tomcat-docs/ssi-howto.xml,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ssi-howto.xml 12 Jan 2003 17:26:46 -0000 1.2 +++ ssi-howto.xml 4 Apr 2005 20:26:55 -0000 1.3 @@ -65,6 +65,11 @@ <li><strong>isVirtualWebappRelative</strong> - Should "virtual" SSI directive paths be interpreted as relative to the context root, instead of the server root? (0=false, 1=true) Default 0 (false).</li> +<li><strong>inputEncoding</strong> - The encoding to be assumed for SSI +resources if one cannot be determined from the resource itself. Default is +the default platform encoding.</li> +<li><strong>outputEncoding</strong> - The encoding to be used for the result +of the SSI processing. Default is UTF-8.</li> </ul> </p>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]