1 22 23 package org.jboss.web.php; 24 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.security.cert.X509Certificate ; 30 import java.util.ArrayList ; 31 import java.util.Enumeration ; 32 import java.util.Hashtable ; 33 import java.util.StringTokenizer ; 34 import java.util.Vector ; 35 36 import javax.servlet.ServletContext ; 37 import javax.servlet.http.HttpServletRequest ; 38 39 import org.apache.catalina.Globals; 40 import org.apache.catalina.util.IOTools; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 45 53 public class ScriptEnvironment { 54 55 private static Log log = LogFactory.getLog(ScriptEnvironment.class); 56 57 60 private static final String CERTIFICATE_KEY = "javax.servlet.request.X509Certificate"; 61 private static final String CIPHER_SUITE = "javax.servlet.request.cipher_suite"; 62 private static final String SSL_SESSION = "javax.servlet.request.ssl_session"; 63 private static final String KEY_SIZE = "javax.servlet.request.key_size"; 64 65 66 private ServletContext context = null; 67 68 69 private String scriptFullPath = null; 70 71 72 private String contextPath = null; 73 74 75 private String servletPath = null; 76 77 78 private String pathInfo = null; 79 80 81 private String webAppRootDir = null; 82 83 84 private File tempDir = null; 85 86 87 private Hashtable env = null; 88 89 90 private File workingDirectory = null; 91 92 93 private File scriptFile = null; 94 95 96 private ArrayList queryParameters = new ArrayList (); 97 98 99 private boolean valid = false; 100 101 102 private static Object expandFileLock = new Object (); 103 104 110 private String scriptPathPrefix = null; 111 112 176 protected String [] findScript(String pathInfo, String webAppRootDir, 177 String contextPath, String servletPath, 178 String scriptPathPrefix) 179 { 180 String path = null; 181 String name = null; 182 String scriptName = null; 183 String fullName = null; 184 185 if ((webAppRootDir != null) 186 && (webAppRootDir.lastIndexOf(File.separator) == 187 (webAppRootDir.length() - 1))) { 188 webAppRootDir = 190 webAppRootDir.substring(0, (webAppRootDir.length() - 1)); 191 } 192 193 if (scriptPathPrefix != null) { 194 webAppRootDir = webAppRootDir + File.separator 195 + scriptPathPrefix; 196 } 197 File currentLocation = new File (webAppRootDir); 198 StringTokenizer dirWalker = new StringTokenizer (pathInfo, "/"); 199 200 while (!currentLocation.isFile() && dirWalker.hasMoreElements()) { 201 currentLocation = new File (currentLocation, 202 (String )dirWalker.nextElement()); 203 } 204 if (!currentLocation.isFile()) { 205 return new String [] { null, null, null, null }; 206 } 207 else { 208 209 path = currentLocation.getAbsolutePath(); 210 name = currentLocation.getName(); 211 fullName = 212 currentLocation.getParent().substring(webAppRootDir.length()) 213 + File.separator + name; 214 fullName = fullName.replace(File.separatorChar, '/'); 216 if (!fullName.equals(servletPath)) { 217 if (".".equals(contextPath)) { 218 scriptName = servletPath + fullName; 219 } 220 else { 221 scriptName = contextPath + servletPath + fullName; 222 } 223 } 224 else { 225 scriptName = fullName; 227 } 228 229 } 230 231 return new String [] { path, scriptName, fullName, name }; 232 } 233 234 238 protected void expandScript() 239 { 240 StringBuffer srcPath = new StringBuffer (); 241 StringBuffer dstPath = new StringBuffer (); 242 InputStream is = null; 243 244 if (scriptPathPrefix == null) { 246 srcPath.append(pathInfo); 247 is = context.getResourceAsStream(srcPath.toString()); 248 dstPath.append(tempDir); 249 dstPath.append(pathInfo); 250 } 251 else { 252 srcPath.append(scriptPathPrefix); 254 StringTokenizer dirWalker = new StringTokenizer (pathInfo, "/"); 255 while (dirWalker.hasMoreElements() && (is == null)) { 257 srcPath.append("/"); 258 srcPath.append(dirWalker.nextElement()); 259 is = context.getResourceAsStream(srcPath.toString()); 260 } 261 dstPath.append(tempDir); 262 dstPath.append("/"); 263 dstPath.append(srcPath); 264 } 265 266 if (is == null) { 267 return; 269 } 270 271 File f = new File (dstPath.toString()); 272 if (f.exists()) { 273 return; 275 } 276 277 String dirPath = new String (dstPath.toString().substring( 0, 279 dstPath.toString().lastIndexOf("/"))); 280 File dir = new File (dirPath); 281 dir.mkdirs(); 282 283 try { 284 synchronized (expandFileLock) { 285 if (f.exists()) { 287 return; 288 } 289 290 if (!f.createNewFile()) { 292 return; 293 } 294 FileOutputStream fos = new FileOutputStream (f); 295 296 IOTools.flow(is, fos); 298 is.close(); 299 fos.close(); 300 } 301 } catch (IOException ioe) { 302 if (f.exists()) { 304 f.delete(); 305 } 306 } 307 } 308 309 319 protected boolean setEnvironment(HttpServletRequest req) 320 throws IOException 321 { 322 323 328 329 Hashtable envp = new Hashtable (); 330 331 334 String sPathInfoOrig = null; 336 String sPathTranslatedOrig = null; 337 String sPathInfo = null; 338 String sPathTranslated = null; 339 String sFullPath = null; 340 String sScriptName = null; 341 String sFullName = null; 342 String sName = null; 343 String [] sScriptNames; 344 345 346 sPathInfoOrig = this.pathInfo; 347 sPathInfoOrig = sPathInfoOrig == null ? "" : sPathInfoOrig; 348 349 sPathTranslatedOrig = req.getPathTranslated(); 350 sPathTranslatedOrig = 351 sPathTranslatedOrig == null ? "" : sPathTranslatedOrig; 352 353 if (webAppRootDir == null ) { 354 webAppRootDir = tempDir.toString(); 356 expandScript(); 357 } 358 359 sScriptNames = findScript(sPathInfoOrig, 360 webAppRootDir, 361 contextPath, 362 servletPath, 363 scriptPathPrefix); 364 365 sFullPath = sScriptNames[0]; 366 sScriptName = sScriptNames[1]; 367 sFullName = sScriptNames[2]; 368 sName = sScriptNames[3]; 369 370 if (sFullPath == null 371 || sScriptName == null 372 || sFullName == null 373 || sName == null) { 374 log.error("Invalid script names"); 375 return false; 376 } 377 378 envp.put("SERVER_SOFTWARE", "JBossWebServer"); 379 envp.put("SERVER_NAME", nullsToBlanks(req.getServerName())); 380 envp.put("GATEWAY_INTERFACE", "CGI/1.1"); 381 envp.put("SERVER_PROTOCOL", nullsToBlanks(req.getProtocol())); 382 383 int port = req.getServerPort(); 384 Integer sPort = (port == 0 ? new Integer (-1) : new Integer (port)); 385 envp.put("SERVER_PORT", sPort.toString()); 386 387 390 envp.put("LOCAL_NAME", nullsToBlanks(req.getLocalName())); 391 port = req.getLocalPort(); 392 Integer iPort = (port == 0 ? new Integer (-1) : new Integer (port)); 393 envp.put("LOCAL_PORT", iPort.toString()); 394 envp.put("LOCAL_ADDR", nullsToBlanks(req.getLocalAddr())); 395 396 envp.put("REQUEST_METHOD", nullsToBlanks(req.getMethod())); 397 398 409 if (pathInfo == null 410 || (pathInfo.substring(sFullName.length()).length() <= 0)) { 411 sPathInfo = ""; 412 } 413 else { 414 sPathInfo = pathInfo.substring(sFullName.length()); 415 } 416 envp.put("PATH_INFO", sPathInfo); 417 418 437 if (sPathInfo != null && !("".equals(sPathInfo))) { 438 sPathTranslated = context.getRealPath(sPathInfo); 439 } 440 else { 441 sPathTranslated = null; 442 } 443 if (sPathTranslated == null || "".equals(sPathTranslated)) { 444 } 446 else { 447 envp.put("PATH_TRANSLATED", nullsToBlanks(sPathTranslated)); 448 } 449 450 451 envp.put("SCRIPT_NAME", nullsToBlanks(sScriptName)); 452 envp.put("QUERY_STRING", nullsToBlanks(req.getQueryString())); 453 envp.put("REMOTE_HOST", nullsToBlanks(req.getRemoteHost())); 454 envp.put("REMOTE_ADDR", nullsToBlanks(req.getRemoteAddr())); 455 envp.put("AUTH_TYPE", nullsToBlanks(req.getAuthType())); 456 envp.put("REMOTE_USER", nullsToBlanks(req.getRemoteUser())); 457 envp.put("REMOTE_IDENT", ""); envp.put("CONTENT_TYPE", nullsToBlanks(req.getContentType())); 459 460 461 465 int contentLength = req.getContentLength(); 466 String sContentLength = (contentLength <= 0 ? "" : 467 (new Integer (contentLength)).toString()); 468 envp.put("CONTENT_LENGTH", sContentLength); 469 470 471 Enumeration headers = req.getHeaderNames(); 472 String header = null; 473 474 while (headers.hasMoreElements()) { 475 header = null; 476 header = ((String )headers.nextElement()).toUpperCase(); 477 if ("AUTHORIZATION".equalsIgnoreCase(header) || 481 "PROXY_AUTHORIZATION".equalsIgnoreCase(header)) { 482 } 484 else { 485 envp.put("HTTP_" + header.replace('-', '_'), 486 req.getHeader(header)); 487 } 488 } 489 490 scriptFile = new File (sFullPath); 491 scriptFullPath = scriptFile.getCanonicalPath(); 492 workingDirectory = new File (scriptFullPath.substring(0, 493 scriptFullPath.lastIndexOf(File.separator))); 494 495 envp.put("SCRIPT_FILENAME", scriptFullPath); 496 envp.put("PHP_SELF", nullsToBlanks(sFullName)); 497 498 if (req.isSecure()) { 499 envp.put("HTTPS", "ON"); 500 envp.put("SSL_CIPHER", req.getAttribute(CIPHER_SUITE)); 501 envp.put("SSL_SESSION_ID", req.getAttribute(SSL_SESSION)); 502 envp.put("SSL_CIPHER_USEKEYSIZE", String.valueOf(req.getAttribute(KEY_SIZE))); 503 X509Certificate [] certs = 504 (X509Certificate [])req.getAttribute(CERTIFICATE_KEY); 505 if (certs != null) { 506 envp.put("SSL_SERVER_V_START", certs[0].getNotAfter().toString()); 508 envp.put("SSL_SERVER_V_END", certs[0].getNotBefore().toString()); 509 510 envp.put("SSL_CLIENT_A_KEY", certs[0].getSigAlgName()); 511 512 514 envp.put("SSL_SERVER_M_SERIAL", certs[0].getSerialNumber().toString()); 515 envp.put("SSL_SERVER_M_VERSION", String.valueOf(certs[0].getVersion())); 516 517 envp.put("SSL_CLIENT_S_DN", certs[0].getSubjectX500Principal().getName()); 519 String pr = certs[0].getSubjectX500Principal().getName(); 521 String prs[] = pr.split(", "); 522 for (int c = 0; c < prs.length; c++) { 523 String pprs[] = prs[c].split("="); 524 envp.put("SSL_CLIENT_S_DN_" + pprs[0], pprs[1]); 525 } 526 527 envp.put("SSL_CLIENT_I_DN", certs[0].getIssuerX500Principal().getName()); 529 pr = certs[0].getSubjectX500Principal().getName(); 531 prs = pr.split(", "); 532 for (int c = 0; c < prs.length; c++) { 533 String pprs[] = prs[c].split("="); 534 envp.put("SSL_CLIENT_I_DN_" + pprs[0], pprs[1]); 535 } 536 537 538 } 541 } 542 543 this.env = envp; 544 return true; 545 } 546 547 548 558 public ScriptEnvironment(HttpServletRequest req, 559 ServletContext context, 560 String scriptPathPrefix) 561 throws IOException 562 { 563 this.scriptPathPrefix = scriptPathPrefix; 564 this.context = context; 565 this.webAppRootDir = context.getRealPath("/"); 566 this.tempDir = (File )context.getAttribute(Globals.WORK_DIR_ATTR); 567 this.contextPath = req.getContextPath(); 568 this.servletPath = req.getServletPath(); 569 this.pathInfo = req.getPathInfo(); 570 if (this.pathInfo == null) { 573 this.pathInfo = this.servletPath; 574 } 575 this.valid = setEnvironment(req); 576 } 577 578 579 585 public String getFullPath() 586 { 587 return scriptFullPath; 588 } 589 590 596 public File getScriptFile() 597 { 598 return scriptFile; 599 } 600 601 607 public File getWorkingDirectory() 608 { 609 return workingDirectory; 610 } 611 612 618 public Hashtable getEnvironment() 619 { 620 return env; 621 } 622 623 629 public ArrayList getParameters() 630 { 631 return queryParameters; 632 } 633 634 641 public boolean isValid() 642 { 643 return valid; 644 } 645 646 653 protected String nullsToBlanks(String s) 654 { 655 return nullsToString(s, ""); 656 } 657 658 666 protected String nullsToString(String couldBeNull, 667 String subForNulls) 668 { 669 return (couldBeNull == null ? subForNulls : couldBeNull); 670 } 671 672 680 protected String blanksToString(String couldBeBlank, 681 String subForBlanks) 682 { 683 return (("".equals(couldBeBlank) || couldBeBlank == null) 684 ? subForBlanks 685 : couldBeBlank); 686 } 687 688 694 public String [] getEnvironmentArray() 695 throws NullPointerException 696 { 697 return hashToStringArray(env); 698 } 699 700 708 public static String [] hashToStringArray(Hashtable h) 709 throws NullPointerException 710 { 711 Vector v = new Vector (); 712 Enumeration e = h.keys(); 713 while (e.hasMoreElements()) { 714 String k = e.nextElement().toString(); 715 v.add(k); 716 v.add(h.get(k)); 717 } 718 String [] strArr = new String [v.size()]; 719 v.copyInto(strArr); 720 return strArr; 721 } 722 723 } 724 | Popular Tags |