📄 cgiservlet.java
字号:
/**
* Encapsulates the CGI environment and rules to derive
* that environment from the servlet container and request information.
*
* <p>
* </p>
*
* @author Martin Dengler [root@martindengler.com]
* @version $Revision: 1.14 $, $Date: 2004/01/26 19:47:39 $
* @since Tomcat 4.0
*
*/
protected class CGIEnvironment {
/** context of the enclosing servlet */
private ServletContext context = null;
/** context path of enclosing servlet */
private String contextPath = null;
/** servlet URI of the enclosing servlet */
private String servletPath = null;
/** pathInfo for the current request */
private String pathInfo = null;
/** real file system directory of the enclosing servlet's web app */
private String webAppRootDir = null;
/** tempdir for context - used to expand scripts in unexpanded wars */
private File tmpDir = null;
/** derived cgi environment */
private Hashtable env = null;
/** cgi command to be invoked */
private String command = null;
/** cgi command's desired working directory */
private File workingDirectory = null;
/** cgi command's query parameters */
private Hashtable queryParameters = null;
/** whether or not this object is valid or not */
private boolean valid = false;
/**
* Creates a CGIEnvironment and derives the necessary environment,
* query parameters, working directory, cgi command, etc.
*
* @param req HttpServletRequest for information provided by
* the Servlet API
* @param context ServletContext for information provided by the
* Servlet API
*
*/
protected CGIEnvironment(HttpServletRequest req,
ServletContext context) throws IOException {
setupFromContext(context);
setupFromRequest(req);
queryParameters = new Hashtable();
Enumeration paramNames = req.getParameterNames();
while (paramNames != null && paramNames.hasMoreElements()) {
String param = paramNames.nextElement().toString();
if (param != null) {
queryParameters.put(
param, URLEncoder.encode(req.getParameter(param)));
}
}
this.valid = setCGIEnvironment(req);
if (this.valid) {
workingDirectory = new File(command.substring(0,
command.lastIndexOf(File.separator)));
}
}
/**
* Uses the ServletContext to set some CGI variables
*
* @param context ServletContext for information provided by the
* Servlet API
*/
protected void setupFromContext(ServletContext context) {
this.context = context;
this.webAppRootDir = context.getRealPath("/");
this.tmpDir = (File) context.getAttribute(Globals.WORK_DIR_ATTR);
}
/**
* Uses the HttpServletRequest to set most CGI variables
*
* @param req HttpServletRequest for information provided by
* the Servlet API
*/
protected void setupFromRequest(HttpServletRequest req) {
this.contextPath = req.getContextPath();
this.servletPath = req.getServletPath();
this.pathInfo = req.getPathInfo();
// If getPathInfo() returns null, must be using extension mapping
// In this case, pathInfo should be same as servletPath
if (this.pathInfo == null) {
this.pathInfo = this.servletPath;
}
}
/**
* Resolves core information about the cgi script.
*
* <p>
* Example URI:
* <PRE> /servlet/cgigateway/dir1/realCGIscript/pathinfo1 </PRE>
* <ul>
* <LI><b>path</b> = $CATALINA_HOME/mywebapp/dir1/realCGIscript
* <LI><b>scriptName</b> = /servlet/cgigateway/dir1/realCGIscript
* <LI><b>cgiName</b> = /dir1/realCGIscript
* <LI><b>name</b> = realCGIscript
* </ul>
* </p>
* <p>
* CGI search algorithm: search the real path below
* <my-webapp-root> and find the first non-directory in
* the getPathTranslated("/"), reading/searching from left-to-right.
*</p>
*<p>
* The CGI search path will start at
* webAppRootDir + File.separator + cgiPathPrefix
* (or webAppRootDir alone if cgiPathPrefix is
* null).
*</p>
*<p>
* cgiPathPrefix is defined by setting
* this servlet's cgiPathPrefix init parameter
*
*</p>
*
* @param pathInfo String from HttpServletRequest.getPathInfo()
* @param webAppRootDir String from context.getRealPath("/")
* @param contextPath String as from
* HttpServletRequest.getContextPath()
* @param servletPath String as from
* HttpServletRequest.getServletPath()
* @param cgiPathPrefix subdirectory of webAppRootDir below which
* the web app's CGIs may be stored; can be null.
* The CGI search path will start at
* webAppRootDir + File.separator + cgiPathPrefix
* (or webAppRootDir alone if cgiPathPrefix is
* null). cgiPathPrefix is defined by setting
* the servlet's cgiPathPrefix init parameter.
*
*
* @return
* <ul>
* <li>
* <code>path</code> - full file-system path to valid cgi script,
* or null if no cgi was found
* <li>
* <code>scriptName</code> -
* CGI variable SCRIPT_NAME; the full URL path
* to valid cgi script or null if no cgi was
* found
* <li>
* <code>cgiName</code> - servlet pathInfo fragment corresponding to
* the cgi script itself, or null if not found
* <li>
* <code>name</code> - simple name (no directories) of the
* cgi script, or null if no cgi was found
* </ul>
*
* @author Martin Dengler [root@martindengler.com]
* @since Tomcat 4.0
*/
protected String[] findCGI(String pathInfo, String webAppRootDir,
String contextPath, String servletPath,
String cgiPathPrefix) {
String path = null;
String name = null;
String scriptname = null;
String cginame = null;
if ((webAppRootDir != null)
&& (webAppRootDir.lastIndexOf(File.separator) ==
(webAppRootDir.length() - 1))) {
//strip the trailing "/" from the webAppRootDir
webAppRootDir =
webAppRootDir.substring(0, (webAppRootDir.length() - 1));
}
if (cgiPathPrefix != null) {
webAppRootDir = webAppRootDir + File.separator
+ cgiPathPrefix;
}
if (debug >= 2) {
log("findCGI: path=" + pathInfo + ", " + webAppRootDir);
}
File currentLocation = new File(webAppRootDir);
StringTokenizer dirWalker =
new StringTokenizer(pathInfo, "/");
if (debug >= 3) {
log("findCGI: currentLoc=" + currentLocation);
}
while (!currentLocation.isFile() && dirWalker.hasMoreElements()) {
if (debug >= 3) {
log("findCGI: currentLoc=" + currentLocation);
}
currentLocation = new File(currentLocation,
(String) dirWalker.nextElement());
}
if (!currentLocation.isFile()) {
return new String[] { null, null, null, null };
} else {
if (debug >= 2) {
log("findCGI: FOUND cgi at " + currentLocation);
}
path = currentLocation.getAbsolutePath();
name = currentLocation.getName();
cginame =
currentLocation.getParent().substring(webAppRootDir.length())
+ File.separator
+ name;
if (".".equals(contextPath)) {
scriptname = servletPath + cginame;
} else {
scriptname = contextPath + servletPath + cginame;
}
}
if (debug >= 1) {
log("findCGI calc: name=" + name + ", path=" + path
+ ", scriptname=" + scriptname + ", cginame=" + cginame);
}
return new String[] { path, scriptname, cginame, name };
}
/**
* Constructs the CGI environment to be supplied to the invoked CGI
* script; relies heavliy on Servlet API methods and findCGI
*
* @param HttpServletRequest request associated with the CGI
* invokation
*
* @return true if environment was set OK, false if there
* was a problem and no environment was set
*/
protected boolean setCGIEnvironment(HttpServletRequest req) throws IOException {
/*
* This method is slightly ugly; c'est la vie.
* "You cannot stop [ugliness], you can only hope to contain [it]"
* (apologies to Marv Albert regarding MJ)
*/
Hashtable envp = new Hashtable();
String sPathInfoOrig = null;
String sPathTranslatedOrig = null;
String sPathInfoCGI = null;
String sPathTranslatedCGI = null;
String sCGIFullPath = null;
String sCGIScriptName = null;
String sCGIFullName = null;
String sCGIName = null;
String[] sCGINames;
sPathInfoOrig = this.pathInfo;
sPathInfoOrig = sPathInfoOrig == null ? "" : sPathInfoOrig;
sPathTranslatedOrig = req.getPathTranslated();
sPathTranslatedOrig =
sPathTranslatedOrig == null ? "" : sPathTranslatedOrig;
if (webAppRootDir == null ) {
// The app has not been deployed in exploded form
webAppRootDir = tmpDir.toString();
expandCGIScript();
}
sCGINames = findCGI(sPathInfoOrig,
webAppRootDir,
contextPath,
servletPath,
cgiPathPrefix);
sCGIFullPath = sCGINames[0];
sCGIScriptName = sCGINames[1];
sCGIFullName = sCGINames[2];
sCGIName = sCGINames[3];
if (sCGIFullPath == null
|| sCGIScriptName == null
|| sCGIFullName == null
|| sCGIName == null) {
return false;
}
envp.put("SERVER_SOFTWARE", "TOMCAT");
envp.put("SERVER_NAME", nullsToBlanks(req.getServerName()));
envp.put("GATEWAY_INTERFACE", "CGI/1.1");
envp.put("SERVER_PROTOCOL", nullsToBlanks(req.getProtocol()));
int port = req.getServerPort();
Integer iPort = (port == 0 ? new Integer(-1) : new Integer(port));
envp.put("SERVER_PORT", iPort.toString());
envp.put("REQUEST_METHOD", nullsToBlanks(req.getMethod()));
/*-
* PATH_INFO should be determined by using sCGIFullName:
* 1) Let sCGIFullName not end in a "/" (see method findCGI)
* 2) Let sCGIFullName equal the pathInfo fragment which
* corresponds to the actual cgi script.
* 3) Thus, PATH_INFO = request.getPathInfo().substring(
* sCGIFullName.length())
*
* (see method findCGI, where the real work is done)
*
*/
if (pathInfo == null
|| (pathInfo.substring(sCGIFullName.length()).length() <= 0)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -