markt       2004/08/17 14:44:33

  Modified:    catalina/src/conf web.xml
               catalina/src/share/org/apache/catalina/servlets
                        CGIServlet.java
               webapps/tomcat-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
  
  Revision  Changes    Path
  1.58      +14 -12    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.57
  retrieving revision 1.58
  diff -u -r1.57 -r1.58
  --- web.xml   16 Aug 2004 22:29:33 -0000      1.57
  +++ web.xml   17 Aug 2004 21:44:32 -0000      1.58
  @@ -226,19 +226,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   -->
  @@ -256,10 +259,9 @@
             <param-name>cgiPathPrefix</param-name>
             <param-value>WEB-INF/cgi</param-value>
           </init-param>
  -         <load-on-startup>5</load-on-startup>
  +        <load-on-startup>5</load-on-startup>
       </servlet>
   -->
  -
   
     <!-- ================ Built In Servlet Mappings ========================= -->
   
  
  
  
  1.26      +69 -9     
jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/CGIServlet.java
  
  Index: CGIServlet.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/servlets/CGIServlet.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- CGIServlet.java   16 Aug 2004 22:29:33 -0000      1.25
  +++ CGIServlet.java   17 Aug 2004 21:44:32 -0000      1.26
  @@ -315,6 +315,8 @@
       /** 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.
  @@ -343,6 +345,8 @@
               throw new UnavailableException
                   ("Cannot invoke CGIServlet through the invoker");
   
  +        boolean passShellEnvironment = false;
  +        
           // Set our properties from the initialization parameters
           String value = null;
           try {
  @@ -350,11 +354,22 @@
               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;
  @@ -682,8 +697,49 @@
           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;
  +    }
   
   
   
  @@ -960,6 +1016,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.5       +3 -0      jakarta-tomcat-4.0/webapps/tomcat-docs/cgi-howto.xml
  
  Index: cgi-howto.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-4.0/webapps/tomcat-docs/cgi-howto.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- cgi-howto.xml     16 Aug 2004 22:29:34 -0000      1.4
  +++ cgi-howto.xml     17 Aug 2004 21:44:32 -0000      1.5
  @@ -59,6 +59,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]

Reply via email to