1 2 3 27 28 29 package org.apache.catalina.util; 30 31 import java.lang.Process ; 32 import java.io.File ; 33 import java.io.Writer ; 34 import java.io.Reader ; 35 import java.io.PrintWriter ; 36 import java.io.BufferedWriter ; 37 import java.io.BufferedReader ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 import java.io.InputStreamReader ; 41 import java.io.OutputStreamWriter ; 42 import java.io.BufferedInputStream ; 43 import java.io.BufferedOutputStream ; 44 import java.io.IOException ; 45 import java.net.URLEncoder ; 46 import java.util.Hashtable ; 47 import java.util.Vector ; 48 import java.util.Enumeration ; 49 import java.util.StringTokenizer ; 50 import java.util.Locale ; 51 import java.util.Date ; 52 import javax.servlet.ServletException ; 53 import javax.servlet.ServletOutputStream ; 54 import javax.servlet.ServletContext ; 55 import javax.servlet.ServletConfig ; 56 import javax.servlet.http.HttpServlet ; 57 import javax.servlet.http.HttpServletRequest ; 58 import javax.servlet.http.HttpServletResponse ; 59 import javax.servlet.http.HttpSession ; 60 import javax.servlet.http.Cookie ; 61 import org.apache.catalina.Context; 62 import org.apache.catalina.Wrapper; 63 65 66 68 69 94 public class ProcessHelper { 95 96 private static com.sun.org.apache.commons.logging.Log log= 97 com.sun.org.apache.commons.logging.LogFactory.getLog( ProcessHelper.class ); 98 99 100 private String command = null; 101 102 103 private Hashtable env = null; 104 105 106 private File wd = null; 107 108 109 private Hashtable params = null; 110 111 112 private InputStream stdin = null; 113 114 115 private HttpServletResponse response = null; 116 117 118 private boolean readyToRun = false; 119 120 121 private int debug = 0; 122 123 124 private int iClientInputTimeout; 125 126 143 public ProcessHelper(String command, Hashtable env, File wd, 144 Hashtable params) { 145 this.command = command; 146 this.env = env; 147 this.wd = wd; 148 this.params = params; 149 updateReadyStatus(); 150 } 151 152 153 154 157 protected void updateReadyStatus() { 158 if (command != null 159 && env != null 160 && wd != null 161 && params != null 162 && response != null) { 163 readyToRun = true; 164 } else { 165 readyToRun = false; 166 } 167 } 168 169 170 171 177 public boolean isReady() { 178 return readyToRun; 179 } 180 181 182 183 190 public void setResponse(HttpServletResponse response) { 191 this.response = response; 192 updateReadyStatus(); 193 } 194 195 196 197 203 public void setInput(InputStream stdin) { 204 this.stdin = stdin; 205 updateReadyStatus(); 206 } 207 208 209 210 222 private String [] hashToStringArray(Hashtable h) 223 throws NullPointerException { 224 Vector v = new Vector (); 225 Enumeration e = h.keys(); 226 while (e.hasMoreElements()) { 227 String k = e.nextElement().toString(); 228 v.add(k + "=" + h.get(k)); 229 } 230 String [] strArr = new String [v.size()]; 231 v.copyInto(strArr); 232 return strArr; 233 } 234 235 236 237 287 public void run() throws IOException { 288 289 292 293 if (!isReady()) { 294 throw new IOException (this.getClass().getName() 295 + ": not ready to run."); 296 } 297 298 if (debug >= 1 ) { 299 log("runCGI(envp=[" + env + "], command=" + command + ")"); 300 } 301 302 if ((command.indexOf(File.separator + "." + File.separator) >= 0) 303 || (command.indexOf(File.separator + "..") >= 0) 304 || (command.indexOf(".." + File.separator) >= 0)) { 305 throw new IOException (this.getClass().getName() 306 + "Illegal Character in CGI command " 307 + "path ('.' or '..') detected. Not " 308 + "running CGI [" + command + "]."); 309 } 310 311 316 Runtime rt = null; 317 BufferedReader commandsStdOut = null; 318 BufferedReader commandsStdErr = null; 319 BufferedOutputStream commandsStdIn = null; 320 Process proc = null; 321 byte[] bBuf = new byte[1024]; 322 char[] cBuf = new char[1024]; 323 int bufRead = -1; 324 325 Enumeration paramNames = params.keys(); 327 StringBuffer cmdAndArgs = new StringBuffer (command); 328 if (paramNames != null && paramNames.hasMoreElements()) { 329 cmdAndArgs.append(" "); 330 while (paramNames.hasMoreElements()) { 331 String k = (String ) paramNames.nextElement(); 332 String v = params.get(k).toString(); 333 if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) { 334 cmdAndArgs.append(k); 335 cmdAndArgs.append("="); 336 v = java.net.URLEncoder.encode(v); 337 cmdAndArgs.append(v); 338 cmdAndArgs.append(" "); 339 } 340 } 341 } 342 343 String postIn = getPostInput(params); 344 int contentLength = (postIn.length() 345 + System.getProperty("line.separator").length()); 346 if ("POST".equals(env.get("REQUEST_METHOD"))) { 347 env.put("CONTENT_LENGTH", new Integer (contentLength)); 348 } 349 350 rt = Runtime.getRuntime(); 351 proc = rt.exec(cmdAndArgs.toString(), hashToStringArray(env), wd); 352 353 354 359 commandsStdIn = new BufferedOutputStream (proc.getOutputStream()); 360 if (debug >= 2 ) { 361 log("runCGI stdin=[" + stdin + "], qs=" 362 + env.get("QUERY_STRING")); 363 } 364 if ("POST".equals(env.get("REQUEST_METHOD"))) { 365 if (debug >= 2) { 366 log("runCGI: writing ---------------\n"); 367 log(postIn); 368 log("runCGI: new content_length=" + contentLength 369 + "---------------\n"); 370 } 371 commandsStdIn.write(postIn.getBytes()); 372 } 373 if (stdin != null) { 374 378 if (stdin.available() <= 0) { 379 if (debug >= 2 ) { 380 log("runCGI stdin is NOT available [" 381 + stdin.available() + "]"); 382 } 383 try { 384 Thread.currentThread().sleep(iClientInputTimeout); 385 } catch (InterruptedException ignored) { 386 } 387 } 388 if (stdin.available() > 0) { 389 if (debug >= 2 ) { 390 log("runCGI stdin IS available [" 391 + stdin.available() + "]"); 392 } 393 bBuf = new byte[1024]; 394 bufRead = -1; 395 try { 396 while ((bufRead = stdin.read(bBuf)) != -1) { 397 if (debug >= 2 ) { 398 log("runCGI: read [" + bufRead 399 + "] bytes from stdin"); 400 } 401 commandsStdIn.write(bBuf, 0, bufRead); 402 } 403 if (debug >= 2 ) { 404 log("runCGI: DONE READING from stdin"); 405 } 406 } catch (IOException ioe) { 407 log("runCGI: couldn't write all bytes."); 410 ioe.printStackTrace(); 411 } 412 } 413 } 414 commandsStdIn.flush(); 415 commandsStdIn.close(); 416 417 422 423 boolean isRunning = true; 424 commandsStdOut = new BufferedReader 425 (new InputStreamReader (proc.getInputStream())); 426 commandsStdErr = new BufferedReader 427 (new InputStreamReader (proc.getErrorStream())); 428 BufferedWriter servletContainerStdout = null; 429 430 try { 431 if (response.getOutputStream() != null) { 432 servletContainerStdout = 433 new BufferedWriter (new OutputStreamWriter 434 (response.getOutputStream())); 435 } 436 } catch (IOException ignored) { 437 } 439 440 while (isRunning) { 441 442 try { 443 cBuf = new char[1024]; 445 while ((bufRead = commandsStdErr.read(cBuf)) != -1) { 446 if (servletContainerStdout != null) { 447 servletContainerStdout.write(cBuf, 0, bufRead); 448 } 449 } 450 451 String line = null; 453 while (((line = commandsStdOut.readLine()) != null) 454 && !("".equals(line))) { 455 if (debug >= 2) { 456 log("runCGI: addHeader(\"" + line + "\")"); 457 } 458 if (line.startsWith("HTTP")) { 459 463 } else { 464 response.addHeader 465 (line.substring(0, line.indexOf(":")).trim(), 466 line.substring(line.indexOf(":") + 1).trim()); 467 } 468 } 469 470 cBuf = new char[1024]; 472 while ((bufRead = commandsStdOut.read(cBuf)) != -1) { 473 if (servletContainerStdout != null) { 474 if (debug >= 4) { 475 log("runCGI: write(\"" + cBuf + "\")"); 476 } 477 servletContainerStdout.write(cBuf, 0, bufRead); 478 } 479 } 480 481 if (servletContainerStdout != null) { 482 servletContainerStdout.flush(); 483 } 484 485 proc.exitValue(); 487 isRunning = false; 488 489 } catch (IllegalThreadStateException e) { 490 try { 491 Thread.currentThread().sleep(500); 492 } catch (InterruptedException ignored) { 493 } 494 } 495 } 497 498 } 499 500 501 508 509 protected String getPostInput(Hashtable params) { 510 String lineSeparator = System.getProperty("line.separator"); 511 Enumeration paramNames = params.keys(); 512 StringBuffer postInput = new StringBuffer (""); 513 StringBuffer qs = new StringBuffer (""); 514 if (paramNames != null && paramNames.hasMoreElements()) { 515 while (paramNames.hasMoreElements()) { 516 String k = (String ) paramNames.nextElement(); 517 String v = params.get(k).toString(); 518 if ((k.indexOf("=") < 0) && (v.indexOf("=") < 0)) { 519 postInput.append(k); 520 qs.append(k); 521 postInput.append("="); 522 qs.append("="); 523 postInput.append(v); 524 qs.append(v); 525 postInput.append(lineSeparator); 526 qs.append("&"); 527 } 528 } 529 } 530 qs.append(lineSeparator); 531 return qs.append(postInput).toString(); 532 } 533 534 535 private void log(String s) { 536 if (log.isDebugEnabled()) 537 log.debug(s); 538 } 539 540 541 public int getIClientInputTimeout(){ 542 return iClientInputTimeout; 543 } 544 545 546 public void setIClientInputTimeout(int iClientInputTimeout){ 547 this.iClientInputTimeout = iClientInputTimeout; 548 } 549 } 550 551 552 | Popular Tags |