implement cgi api improvements

This commit is contained in:
Sebastian Sdorra
2011-05-05 13:51:10 +02:00
parent 6ae090ae2e
commit 6fd04b19d3
2 changed files with 72 additions and 29 deletions

View File

@@ -199,13 +199,13 @@ public abstract class AbstractCGIExecutor implements CGIExecutor
protected EnvList environment; protected EnvList environment;
/** Field description */ /** Field description */
protected boolean ignoreExitCode; protected boolean ignoreExitCode = false;
/** Field description */ /** Field description */
protected String interpreter; protected String interpreter;
/** Field description */ /** Field description */
protected boolean passShellEnvironment; protected boolean passShellEnvironment = false;
/** Field description */ /** Field description */
protected File workDirectory; protected File workDirectory;

View File

@@ -42,6 +42,7 @@ import sonia.scm.SCMContext;
import sonia.scm.config.ScmConfiguration; import sonia.scm.config.ScmConfiguration;
import sonia.scm.util.HttpUtil; import sonia.scm.util.HttpUtil;
import sonia.scm.util.IOUtil; import sonia.scm.util.IOUtil;
import sonia.scm.util.SystemUtil;
import sonia.scm.util.Util; import sonia.scm.util.Util;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
@@ -54,6 +55,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Map;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream; import javax.servlet.ServletInputStream;
@@ -74,6 +76,9 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
/** Field description */ /** Field description */
public static final int DEFAULT_BUFFER_SIZE = 16264; public static final int DEFAULT_BUFFER_SIZE = 16264;
/** Field description */
public static final String SYSTEM_ROOT_WINDOWS = "C:\\WINDOWS";
/** Field description */ /** Field description */
private static final String SERVER_SOFTWARE_PREFIX = "scm-manager/"; private static final String SERVER_SOFTWARE_PREFIX = "scm-manager/";
@@ -166,8 +171,7 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
try try
{ {
p = Runtime.getRuntime().exec(execCmd, environment.getEnvArray(), p = Runtime.getRuntime().exec(execCmd, environment.getEnvArray(),
workDirectory); workDirectory);
execute(p); execute(p);
} }
finally finally
@@ -179,6 +183,25 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
} }
} }
/**
* Method description
*
*
* @param env
*/
private void apendOsEnvironment(EnvList env)
{
Map<String, String> osEnv = System.getenv();
if (Util.isNotEmpty(osEnv))
{
for (Map.Entry<String, String> e : osEnv.entrySet())
{
env.set(e.getKey(), e.getValue());
}
}
}
/** /**
* Method description * Method description
* *
@@ -194,27 +217,31 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
String scriptPath = context.getRealPath(scriptName); String scriptPath = context.getRealPath(scriptName);
String pathTranslated = request.getPathTranslated(); String pathTranslated = request.getPathTranslated();
int len = request.getContentLength(); int len = request.getContentLength();
EnvList env = new EnvList(); EnvList env = new EnvList();
if (passShellEnvironment)
{
apendOsEnvironment(env);
}
env.set(ENV_AUTH_TYPE, request.getAuthType()); env.set(ENV_AUTH_TYPE, request.getAuthType());
/** /**
* Note CGI spec says CONTENT_LENGTH must be NULL ("") or undefined * Note CGI spec says CONTENT_LENGTH must be NULL ("") or undefined
* if there is no content, so we cannot put 0 or -1 in as per the * if there is no content, so we cannot put 0 or -1 in as per the
* Servlet API spec. * Servlet API spec.
* *
* see org.apache.catalina.servlets.CGIServlet * see org.apache.catalina.servlets.CGIServlet
**/ */
if (len <= 0)
if ( len <= 0 )
{ {
env.set(ENV_CONTENT_LENGTH, ""); env.set(ENV_CONTENT_LENGTH, "");
} }
else else
{ {
env.set(ENV_CONTENT_LENGTH, Integer.toString(len)); env.set(ENV_CONTENT_LENGTH, Integer.toString(len));
} }
env.set(ENV_CONTENT_TYPE, Util.nonNull(request.getContentType())); env.set(ENV_CONTENT_TYPE, Util.nonNull(request.getContentType()));
env.set(ENV_GATEWAY_INTERFACE, CGI_VERSION); env.set(ENV_GATEWAY_INTERFACE, CGI_VERSION);
env.set(ENV_PATH_INFO, pathInfo); env.set(ENV_PATH_INFO, pathInfo);
@@ -255,6 +282,11 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
? ENV_HTTPS_VALUE_ON ? ENV_HTTPS_VALUE_ON
: ENV_HTTPS_VALUE_OFF)); : ENV_HTTPS_VALUE_OFF));
if (SystemUtil.isWindows())
{
env.set(ENV_SYSTEM_ROOT, SYSTEM_ROOT_WINDOWS);
}
return env; return env;
} }
@@ -286,20 +318,6 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
IOUtil.close(processES); IOUtil.close(processES);
} }
} }
private void processErrorStreamAsync( final InputStream errorStream )
{
new Thread(new Runnable() {
@Override
public void run() {
try {
processErrorStream(errorStream);
} catch (IOException ex) {
logger.error( "could not read errorstream", ex );
}
}
}).start();
}
/** /**
* Method description * Method description
@@ -313,7 +331,7 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
private void parseHeaders(InputStream is) throws IOException private void parseHeaders(InputStream is) throws IOException
{ {
String line = null; String line = null;
response.setContentLength(-1); response.setContentLength(-1);
while ((line = getTextLineFromStream(is)).length() > 0) while ((line = getTextLineFromStream(is)).length() > 0)
@@ -413,6 +431,31 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
} }
} }
/**
* Method description
*
*
* @param errorStream
*/
private void processErrorStreamAsync(final InputStream errorStream)
{
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
processErrorStream(errorStream);
}
catch (IOException ex)
{
logger.error("could not read errorstream", ex);
}
}
}).start();
}
/** /**
* Method description * Method description
* *
@@ -481,7 +524,7 @@ public class DefaultCGIExecutor extends AbstractCGIExecutor
{ {
int exitCode = process.waitFor(); int exitCode = process.waitFor();
if (exitCode != 0) if ((exitCode != 0) &&!ignoreExitCode)
{ {
logger.warn("process ends with exit code {}", exitCode); logger.warn("process ends with exit code {}", exitCode);
} }