markt 2004/08/17 14:54:21 Modified: catalina/src/conf web.xml catalina/src/share/org/apache/catalina/servlets CGIServlet.java webapps/docs cgi-howto.xml Log: Fix bug 18273. Add support for optionally passing the shell environment variables to the CGI script. - Based heavily on <a href="http://www.rgagnon.com/javadetails/java-0150.html"> Read environment variables from an application</a> by Real Gagnon - Ported from TC4 Revision Changes Path 1.41 +13 -10 jakarta-tomcat-catalina/catalina/src/conf/web.xml Index: web.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/conf/web.xml,v retrieving revision 1.40 retrieving revision 1.41 diff -u -r1.40 -r1.41 --- web.xml 16 Aug 2004 22:26:49 -0000 1.40 +++ web.xml 17 Aug 2004 21:54:20 -0000 1.41 @@ -258,19 +258,22 @@ <!-- supports the following initialization parameters (default values --> <!-- are in square brackets): --> <!-- --> - <!-- cgiPathPrefix The CGI search path will start at --> - <!-- webAppRootDir + File.separator + this prefix. --> - <!-- [WEB-INF/cgi] --> + <!-- cgiPathPrefix The CGI search path will start at --> + <!-- webAppRootDir + File.separator + this prefix. --> + <!-- [WEB-INF/cgi] --> <!-- --> - <!-- debug Debugging detail level for messages logged --> - <!-- by this servlet. [0] --> + <!-- debug Debugging detail level for messages logged --> + <!-- by this servlet. [0] --> <!-- --> - <!-- executable Name of the exectuable used to run the script. --> - <!-- [perl] --> + <!-- executable Name of the exectuable used to run the --> + <!-- script. [perl] --> <!-- --> - <!-- parameterEncoding Name of parameter encoding to be used with CGI --> - <!-- servlet. --> - <!-- [System.getProperty("file.encoding","UTF-8")] --> + <!-- parameterEncoding Name of parameter encoding to be used with --> + <!-- CGI servlet. --> + <!-- [System.getProperty("file.encoding","UTF-8")] --> + <!-- --> + <!-- passShellEnvironment Should the shell environment variables (if --> + <!-- any) be passed to the CGI script? [false] --> <!-- --> <!-- IMPORTANT: To use the CGI servlet, you also need to rename the --> <!-- $CATALINA_HOME/server/lib/servlets-cgi.renametojar file --> 1.24 +75 -9 jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/CGIServlet.java Index: CGIServlet.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/catalina/src/share/org/apache/catalina/servlets/CGIServlet.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- CGIServlet.java 16 Aug 2004 22:26:50 -0000 1.23 +++ CGIServlet.java 17 Aug 2004 21:54:20 -0000 1.24 @@ -244,7 +244,6 @@ public final class CGIServlet extends HttpServlet { - /* some vars below copied from Craig R. McClanahan's InvokerServlet */ /** the Context container associated with our web application. */ @@ -261,7 +260,7 @@ */ private String cgiPathPrefix = null; - /** the command to use with the script */ + /** the executable to use with the script */ private String cgiExecutable = "perl"; /** the encoding to use for parameters */ @@ -271,6 +270,9 @@ /** object used to ensure multiple threads don't try to expand same file */ static Object expandFileLock = new Object(); + /** the shell environment variables to be passed to the CGI script */ + static Hashtable shellEnv = new Hashtable(); + /** * Sets instance variables. * <P> @@ -298,6 +300,8 @@ throw new UnavailableException ("Cannot invoke CGIServlet through the invoker"); + boolean passShellEnvironment = false; + // Set our properties from the initialization parameters String value = null; try { @@ -305,16 +309,27 @@ debug = Integer.parseInt(value); cgiPathPrefix = getServletConfig().getInitParameter("cgiPathPrefix"); + value = getServletConfig().getInitParameter("passShellEnvironment"); + passShellEnvironment = Boolean.valueOf(value).booleanValue(); } catch (Throwable t) { //NOOP } log("init: loglevel set to " + debug); + if (passShellEnvironment) { + try { + shellEnv.putAll(getShellEnvironment()); + } catch (IOException ioe) { + ServletException e = new ServletException( + "Unable to read shell environment variables", ioe); + } + } + value = getServletConfig().getInitParameter("executable"); if (value != null) { cgiExecutable = value; } - + // Identify the internal container resources we need context = config.getServletContext(); @@ -523,6 +538,8 @@ out.println("</ul>"); out.println("<hr>"); + + } @@ -585,7 +602,7 @@ if (!cgiEnv.isValid()) { res.setStatus(404); } - + if (debug >= 10) { try { ServletOutputStream out = res.getOutputStream(); @@ -635,6 +652,51 @@ System.out.println("$Header$"); } + /** + * Get all shell environment variables. Have to do it this rather ugly way + * as the API to obtain is not available in 1.4 and earlier APIs. + * + * See <a href="http://www.rgagnon.com/javadetails/java-0150.html">Read environment + * variables from an application</a> for original source and article. + */ + private Hashtable getShellEnvironment() throws IOException { + Hashtable envVars = new Hashtable(); + Process p = null; + Runtime r = Runtime.getRuntime(); + String OS = System.getProperty("os.name").toLowerCase(); + boolean ignoreCase; + + if (OS.indexOf("windows 9") > -1) { + p = r.exec( "command.com /c set" ); + ignoreCase = true; + } else if ( (OS.indexOf("nt") > -1) + || (OS.indexOf("windows 2000") > -1) + || (OS.indexOf("windows xp") > -1) ) { + // thanks to JuanFran for the xp fix! + p = r.exec( "cmd.exe /c set" ); + ignoreCase = true; + } else { + // our last hope, we assume Unix (thanks to H. Ware for the fix) + p = r.exec( "env" ); + ignoreCase = false; + } + + BufferedReader br = new BufferedReader + ( new InputStreamReader( p.getInputStream() ) ); + String line; + while( (line = br.readLine()) != null ) { + int idx = line.indexOf( '=' ); + String key = line.substring( 0, idx ); + String value = line.substring( idx+1 ); + if (ignoreCase) { + key = key.toUpperCase(); + } + envVars.put(key, value); + } + return envVars; + } + + @@ -913,6 +975,10 @@ Hashtable envp = new Hashtable(); + // Add the shell environment variables (if any) + envp.putAll(shellEnv); + + // Add the CGI environment variables String sPathInfoOrig = null; String sPathTranslatedOrig = null; String sPathInfoCGI = null; 1.6 +3 -0 jakarta-tomcat-catalina/webapps/docs/cgi-howto.xml Index: cgi-howto.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-catalina/webapps/docs/cgi-howto.xml,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- cgi-howto.xml 16 Aug 2004 22:26:50 -0000 1.5 +++ cgi-howto.xml 17 Aug 2004 21:54:21 -0000 1.6 @@ -63,6 +63,9 @@ <li><strong>parameterEncoding</strong> - Name of the parameter encoding to be used with the GCI servlet. Default is <code>System.getProperty("file.encoding","UTF-8")</code>.</li> +<li><strong>passShellEnvironment</strong> - Should the shell environment +variables (if any) be passed to the CGI script? Default is +<code>false</code>.</li> </ul> </p>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]