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 64 65 66 68 69 70 77 78 public class CGIProcessEnvironment extends ProcessEnvironment { 79 80 81 private static com.sun.org.apache.commons.logging.Log log= 82 com.sun.org.apache.commons.logging.LogFactory.getLog( CGIProcessEnvironment.class ); 83 84 85 private Hashtable queryParameters = null; 86 87 93 private String cgiPathPrefix = null; 94 95 96 106 public CGIProcessEnvironment(HttpServletRequest req, 107 ServletContext context) { 108 this(req, context, ""); 109 } 110 111 112 113 123 public CGIProcessEnvironment(HttpServletRequest req, 124 ServletContext context, String cgiPathPrefix) { 125 this(req, context, cgiPathPrefix, 0); 126 } 127 128 129 130 139 public CGIProcessEnvironment(HttpServletRequest req, 140 ServletContext context, int debug) { 141 this(req, context, "", 0); 142 } 143 144 145 146 147 158 public CGIProcessEnvironment(HttpServletRequest req, 159 ServletContext context, String cgiPathPrefix, int debug) { 160 super(req, context, debug); 161 this.cgiPathPrefix = cgiPathPrefix; 162 queryParameters = new Hashtable (); 163 Enumeration paramNames = req.getParameterNames(); 164 while (paramNames != null && paramNames.hasMoreElements()) { 165 String param = paramNames.nextElement().toString(); 166 if (param != null) { 167 queryParameters.put(param, 168 URLEncoder.encode(req.getParameter(param))); 169 } 170 } 171 this.valid = deriveProcessEnvironment(req); 172 } 173 174 175 176 183 protected boolean deriveProcessEnvironment(HttpServletRequest req) { 184 189 190 Hashtable envp; 191 super.deriveProcessEnvironment(req); 192 envp = getEnvironment(); 193 194 String sPathInfoOrig = null; 195 String sPathTranslatedOrig = null; 196 String sPathInfoCGI = null; 197 String sPathTranslatedCGI = null; 198 String sCGIFullPath = null; 199 String sCGIScriptName = null; 200 String sCGIFullName = null; 201 String sCGIName = null; 202 String [] sCGINames; 203 sPathInfoOrig = this.pathInfo; 204 sPathInfoOrig = sPathInfoOrig == null ? "" : sPathInfoOrig; 205 sPathTranslatedOrig = req.getPathTranslated(); 206 sPathTranslatedOrig = sPathTranslatedOrig == null ? "" : 207 sPathTranslatedOrig; 208 sCGINames = 209 findCGI(sPathInfoOrig, getWebAppRootDir(), getContextPath(), 210 getServletPath(), cgiPathPrefix); 211 sCGIFullPath = sCGINames[0]; 212 sCGIScriptName = sCGINames[1]; 213 sCGIFullName = sCGINames[2]; 214 sCGIName = sCGINames[3]; 215 if (sCGIFullPath == null || sCGIScriptName == null 216 || sCGIFullName == null || sCGIName == null) { 217 return false; 218 } 219 envp.put("SERVER_SOFTWARE", "TOMCAT"); 220 envp.put("SERVER_NAME", nullsToBlanks(req.getServerName())); 221 envp.put("GATEWAY_INTERFACE", "CGI/1.1"); 222 envp.put("SERVER_PROTOCOL", nullsToBlanks(req.getProtocol())); 223 int port = req.getServerPort(); 224 Integer iPort = (port == 0 ? new Integer (-1) : new Integer (port)); 225 envp.put("SERVER_PORT", iPort.toString()); 226 envp.put("REQUEST_METHOD", nullsToBlanks(req.getMethod())); 227 228 239 240 if (pathInfo == null || 241 (pathInfo.substring(sCGIFullName.length()).length() <= 0)) { 242 sPathInfoCGI = ""; 243 } else { 244 sPathInfoCGI = pathInfo.substring(sCGIFullName.length()); 245 } 246 envp.put("PATH_INFO", sPathInfoCGI); 247 248 267 268 if (sPathInfoCGI != null && !("".equals(sPathInfoCGI))) { 269 sPathTranslatedCGI = getContext().getRealPath(sPathInfoCGI); 270 } else { 271 sPathTranslatedCGI = null; 272 } 273 if (sPathTranslatedCGI == null || "".equals(sPathTranslatedCGI)) { 274 } else { 276 envp.put("PATH_TRANSLATED", nullsToBlanks(sPathTranslatedCGI)); 277 } 278 envp.put("SCRIPT_NAME", nullsToBlanks(sCGIScriptName)); 279 envp.put("QUERY_STRING", nullsToBlanks(req.getQueryString())); 280 envp.put("REMOTE_HOST", nullsToBlanks(req.getRemoteHost())); 281 envp.put("REMOTE_ADDR", nullsToBlanks(req.getRemoteAddr())); 282 envp.put("AUTH_TYPE", nullsToBlanks(req.getAuthType())); 283 envp.put("REMOTE_USER", nullsToBlanks(req.getRemoteUser())); 284 envp.put("REMOTE_IDENT", ""); envp.put("CONTENT_TYPE", nullsToBlanks(req.getContentType())); 286 287 291 292 int contentLength = req.getContentLength(); 293 String sContentLength = (contentLength <= 0 ? "" : ( 294 new Integer (contentLength)).toString()); 295 envp.put("CONTENT_LENGTH", sContentLength); 296 Enumeration headers = req.getHeaderNames(); 297 String header = null; 298 while (headers.hasMoreElements()) { 299 header = null; 300 header = ((String )headers.nextElement()).toUpperCase(); 301 if ("AUTHORIZATION".equalsIgnoreCase(header) 305 || "PROXY_AUTHORIZATION".equalsIgnoreCase(header)) { 306 } else if ("HOST".equalsIgnoreCase(header)) { 308 String host = req.getHeader(header); 309 envp.put("HTTP_" + header.replace('-', '_'), 310 host.substring(0, host.indexOf(":"))); 311 } else { 312 envp.put("HTTP_" + header.replace('-', '_'), 313 req.getHeader(header)); 314 } 315 } 316 command = sCGIFullPath; 317 workingDirectory = new File (command.substring(0, 318 command.lastIndexOf(File.separator))); 319 envp.put("X_TOMCAT_COMMAND_PATH", command); this.setEnvironment(envp); 321 return true; 322 } 323 324 325 371 protected String [] findCGI(String pathInfo, String webAppRootDir, 372 String contextPath, String servletPath, String cgiPathPrefix) { 373 String path = null; 374 String name = null; 375 String scriptname = null; 376 String cginame = null; 377 if ((webAppRootDir != null) 378 && (webAppRootDir.lastIndexOf("/") 379 == (webAppRootDir.length() - 1))) { 380 webAppRootDir = 382 webAppRootDir.substring(0, 383 (webAppRootDir.length() - 1)); 384 } 385 if (cgiPathPrefix != null) { 386 webAppRootDir = webAppRootDir + File.separator 387 + cgiPathPrefix; 388 } 389 if (debug >= 2) { 390 log("findCGI: start = [" + webAppRootDir 391 + "], pathInfo = [" + pathInfo + "] "); 392 } 393 File currentLocation = new File (webAppRootDir); 394 StringTokenizer dirWalker = new StringTokenizer (pathInfo, "/"); 395 while (!currentLocation.isFile() && dirWalker.hasMoreElements()) { 396 currentLocation = new 397 File (currentLocation, (String ) dirWalker.nextElement()); 398 if (debug >= 3) { 399 log("findCGI: traversing to [" + currentLocation + "]"); 400 } 401 } 402 if (!currentLocation.isFile()) { 403 return new String [] { null, null, null, null }; 404 } else { 405 if (debug >= 2) { 406 log("findCGI: FOUND cgi at [" + currentLocation + "]"); 407 } 408 path = currentLocation.getAbsolutePath(); 409 name = currentLocation.getName(); 410 cginame = currentLocation.getParent() 411 .substring(webAppRootDir.length()) 412 + File.separator + name; 413 if (".".equals(contextPath)) { 414 scriptname = servletPath + cginame; 415 } else { 416 scriptname = contextPath + servletPath + cginame; 417 } 418 } 419 if (debug >= 1) { 420 log("findCGI calc: name=" + name + ", path=" + path 421 + ", scriptname=" + scriptname + ", cginame=" + cginame); 422 } 423 return new String [] { path, scriptname, cginame, name }; 424 } 425 426 427 432 public String toString() { 433 StringBuffer sb = new StringBuffer (); 434 sb.append("<TABLE border=2>"); 435 sb.append("<tr><th colspan=2 bgcolor=grey>"); 436 sb.append("ProcessEnvironment Info</th></tr>"); 437 sb.append("<tr><td>Debug Level</td><td>"); 438 sb.append(debug); 439 sb.append("</td></tr>"); 440 sb.append("<tr><td>Validity:</td><td>"); 441 sb.append(isValid()); 442 sb.append("</td></tr>"); 443 if (isValid()) { 444 Enumeration envk = env.keys(); 445 while (envk.hasMoreElements()) { 446 String s = (String )envk.nextElement(); 447 sb.append("<tr><td>"); 448 sb.append(s); 449 sb.append("</td><td>"); 450 sb.append(blanksToString((String )env.get(s), 451 "[will be set to blank]")); 452 sb.append("</td></tr>"); 453 } 454 } 455 sb.append("<tr><td colspan=2><HR></td></tr>"); 456 sb.append("<tr><td>Derived Command</td><td>"); 457 sb.append(nullsToBlanks(command)); 458 sb.append("</td></tr>"); 459 sb.append("<tr><td>Working Directory</td><td>"); 460 if (workingDirectory != null) { 461 sb.append(workingDirectory.toString()); 462 } 463 sb.append("</td></tr>"); 464 sb.append("<tr><td colspan=2>Query Params</td></tr>"); 465 Enumeration paramk = queryParameters.keys(); 466 while (paramk.hasMoreElements()) { 467 String s = paramk.nextElement().toString(); 468 sb.append("<tr><td>"); 469 sb.append(s); 470 sb.append("</td><td>"); 471 sb.append(queryParameters.get(s).toString()); 472 sb.append("</td></tr>"); 473 } 474 475 sb.append("</TABLE><p>end."); 476 return sb.toString(); 477 } 478 479 480 484 public Hashtable getParameters() { 485 return queryParameters; 486 } 487 488 } 489 | Popular Tags |