amyroh 01/11/28 19:50:48 Modified: catalina/src/share/org/apache/catalina/servlets SsiInvokerServlet.java Log: Server Side Include improvement. Submitted by Paul Speed <[EMAIL PROTECTED]>. Revision Changes Path 1.14 +72 -34 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/SsiInvokerServlet.java Index: SsiInvokerServlet.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/SsiInvokerServlet.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- SsiInvokerServlet.java 2001/11/13 00:09:41 1.13 +++ SsiInvokerServlet.java 2001/11/29 03:50:48 1.14 @@ -1,8 +1,8 @@ /* * SsiInvokerServlet.java - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/SsiInvokerServlet.java,v 1.13 2001/11/13 00:09:41 amyroh Exp $ - * $Revision: 1.13 $ - * $Date: 2001/11/13 00:09:41 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/SsiInvokerServlet.java,v 1.14 2001/11/29 03:50:48 amyroh Exp $ + * $Revision: 1.14 $ + * $Date: 2001/11/29 03:50:48 $ * * ==================================================================== * @@ -99,7 +99,7 @@ * * @author Bip Thelin * @author Amy Roh - * @version $Revision: 1.13 $, $Date: 2001/11/13 00:09:41 $ + * @version $Revision: 1.14 $, $Date: 2001/11/29 03:50:48 $ */ public final class SsiInvokerServlet extends HttpServlet { /** Debug level for this servlet. */ @@ -321,12 +321,19 @@ try { strCmd = parseCmd(command); } catch (IndexOutOfBoundsException ex) { + log( "Error parsing directive name." ); out.print(ssiEnv.getConfiguration("errmsg")); continue; } strParamType = parseParamType(command, strCmd.length()); - strParam = parseParam(command, strCmd.length()); + strParam = parseParam(command, strCmd.length(), + strParamType.length); + if (strParam == null) { + log( "Error parsing directive parameters." ); + out.print(ssiEnv.getConfiguration("errmsg")); + continue; + } if(debug > 0) log("Serving SSI resource: " + strCmd); @@ -334,9 +341,10 @@ // Run the command for the SSI directive ssiDispatcher.runCommand( strCmd, strParamType, strParam, ssiEnv, out ); - + // Check to see if the output has been disabled disableOutput = ssiEnv.isOutputDisabled(); + continue; } command.append((char)unparsed[bIdx]); @@ -359,7 +367,10 @@ throws IndexOutOfBoundsException { String modString = ((cmd.toString()).trim()).toLowerCase(); - return modString.substring(0, modString.indexOf(' ')); + int end = modString.indexOf(' '); + if( end < 0 ) + return modString; + return modString.substring(0, end); } /** @@ -390,20 +401,29 @@ bIdx++; } - retBuf.append('"'); + retBuf.append('='); inside=!inside; quotes=0; - while(bIdx < cmd.length()&"es!=2) { - if(cmd.charAt(bIdx)=='"') - quotes++; + boolean escaped=false; + for ( ; bIdx < cmd.length() && quotes != 2; bIdx++ ) { + char c = cmd.charAt(bIdx); + + // Need to skip escaped characters + if (c=='\\' && !escaped) { + escaped = true; + bIdx++; + continue; + } + escaped = false; - bIdx++; + if (c=='"') + quotes++; } } } - StringTokenizer str = new StringTokenizer(retBuf.toString(), "\""); + StringTokenizer str = new StringTokenizer(retBuf.toString(), "="); String[] retString = new String[str.countTokens()]; while(str.hasMoreTokens()) { @@ -417,17 +437,18 @@ * Parse a StringBuffer and take out the param token. * Called from <code>requestHandler</code> * @param cmd a value of type 'StringBuffer' + * @param start the index from which to start + * @param count the number of param values expected. * @return a value of type 'String[]' */ - private String[] parseParam(StringBuffer cmd, int start) { - int bIdx = start; - int i = 0; - int quotes = 0; + private String[] parseParam(StringBuffer cmd, int start, int count) { + int valIndex = 0; boolean inside = false; - StringBuffer retBuf = new StringBuffer(); + String[] vals = new String[count]; + StringBuffer sb = new StringBuffer(); - while(bIdx < cmd.length()) { - if(!inside) { + for (int bIdx = start; bIdx < cmd.length(); bIdx++ ) { + if (!inside) { while(bIdx < cmd.length()&& cmd.charAt(bIdx)!='"') bIdx++; @@ -437,26 +458,43 @@ inside=!inside; } else { - while(bIdx < cmd.length() && cmd.charAt(bIdx)!='"') { - retBuf.append(cmd.charAt(bIdx)); - bIdx++; - } + boolean escaped=false; + for ( ; bIdx < cmd.length(); bIdx++) { - retBuf.append('"'); - inside=!inside; - } + char c = cmd.charAt(bIdx); - bIdx++; - } + // Check for escapes + if (c=='\\' && !escaped) { + escaped = true; + continue; + } - StringTokenizer str = new StringTokenizer(retBuf.toString(), "\""); - String[] retString = new String[str.countTokens()]; + // If we reach the other " then stop + if (c=='"' && !escaped) + break; + + // Since parsing of attributes and var + // substitution is done in separate places, + // we need to leave escape in the string + if (c=='$' && escaped) + sb.append( '\\' ); - while(str.hasMoreTokens()) { - retString[i++] = str.nextToken(); + escaped = false; + sb.append(c); + } + + // If we hit the end without seeing a quote + // the signal an error + if (bIdx == cmd.length()) + return null; + + vals[valIndex++] = sb.toString(); + sb.delete( 0, sb.length() ); // clear the buffer + inside=!inside; + } } - return retString; + return vals; } /**
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>