/* Copyright Paul James Mutton, 2001-2004, http://www.jibble.org/ This file is part of Jibble Web Server / WebServerLite. This software is dual-licensed, allowing you to choose between the GNU General Public License (GPL) and the www.jibble.org Commercial License. Since the GPL may be too restrictive for use in a proprietary application, a commercial license is also provided. Full license information can be found at http://www.jibble.org/licenses/ $Author: pjm2 $ $Id: CartoonStripBot.java,v 1.4 2004/02/01 13:19:54 pjm2 Exp $ */ import java.io.*; import java.lang.reflect.*; import java.util.*; /** * Provides limited support for running server side scripts. * The HashMap of server variables are sent to the process * when it is executed. While the process is outputting * data to standard output, this will be issued to the connecting * client. * * @author Copyright Paul Mutton, http://www.jibble.org/ */ public class ServerSideScriptEngine { // This could be a lot better. Consider server side scripting a beta feature // for now. public static void execute(BufferedOutputStream out, String serverVars, File file, String path) throws Throwable { System.out.println("plee file["+file+"]path["+path+"]args["+serverVars+"]"); // Execute the external command String filename = file.toString(); String[] cmdarray = null; String[] envs = new String[1]; envs[0] = serverVars; String cmd=null; if (filename.toLowerCase().endsWith(".pl")) { cmdarray = new String[]{"perl", filename}; cmd = "perl " + filename + " " + serverVars; System.out.println("plee cmd[" + cmd + "]"); } else if (filename.toLowerCase().endsWith(".php")) { cmdarray = new String[]{"php", filename}; } // added two file ext. NOT working now else if (filename.toLowerCase().endsWith(".cgi")) { cmdarray = new String[]{"perl", filename}; } else if (filename.toLowerCase().endsWith(".class")) { String noext = filename.substring(0,filename.length()-6); cmdarray = new String[]{"java", noext}; } else { cmdarray = new String[]{filename}; } try { //Process process = Runtime.getRuntime().exec(cmdarray, envs); Process process = Runtime.getRuntime().exec(cmd); System.out.println("plee after exec"); // Send the process output to the connecting client. InputStream in = process.getInputStream(); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buffer, 0, 4096)) != -1) { out.write(buffer, 0, bytesRead); System.out.print("plee "); for (int i=0;i<4095;i++) System.out.print((char) buffer[i]); System.out.println(); } in.close(); } catch (Exception e) { System.out.println("plee e[" + e + "]"); } } }