Merge "Add alternative URL for Gerrit's managed Gitweb."

This commit is contained in:
Shawn Pearce
2011-04-08 13:22:02 -07:00
committed by Android Code Review
3 changed files with 71 additions and 2 deletions

View File

@@ -57,6 +57,8 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
@@ -78,6 +80,7 @@ class GitWebServlet extends HttpServlet {
private final Set<String> deniedActions;
private final int bufferSize = 8192;
private final File gitwebCgi;
private final URI gitwebUrl;
private final LocalDiskRepositoryManager repoManager;
private final ProjectControl.Factory projectControl;
private final EnvList _env;
@@ -92,6 +95,19 @@ class GitWebServlet extends HttpServlet {
this.gitwebCgi = gitWebConfig.getGitwebCGI();
this.deniedActions = new HashSet<String>();
final String url = gitWebConfig.getUrl();
if ((url != null) && (!url.equals("gitweb"))) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
log.error("Invalid gitweb.url: " + url);
}
gitwebUrl = uri;
} else {
gitwebUrl = null;
}
deniedActions.add("forks");
deniedActions.add("opml");
deniedActions.add("project_list");
@@ -508,6 +524,42 @@ class GitWebServlet extends HttpServlet {
}
env.set("REMOTE_USER", remoteUser);
// Override CGI settings using alternative URI provided by gitweb.url.
// This is required to trick Gitweb into thinking that it's served under
// different URL. Setting just $my_uri on the perl's side isn't enough,
// because few actions (atom, blobdiff_plain, commitdiff_plain) rely on
// URL returned by $cgi->self_url().
//
if (gitwebUrl != null) {
int schemePort = -1;
if (gitwebUrl.getScheme() != null) {
if (gitwebUrl.getScheme().equals("http")) {
env.set("HTTPS", "OFF");
schemePort = 80;
} else {
env.set("HTTPS", "ON");
schemePort = 443;
}
}
if (gitwebUrl.getHost() != null) {
env.set("SERVER_NAME", gitwebUrl.getHost());
env.set("HTTP_HOST", gitwebUrl.getHost());
}
if (gitwebUrl.getPort() != -1) {
env.set("SERVER_PORT", Integer.toString(gitwebUrl.getPort()));
} else if (schemePort != -1) {
env.set("SERVER_PORT", Integer.toString(schemePort));
}
if (gitwebUrl.getPath() != null) {
env.set("SCRIPT_NAME", gitwebUrl.getPath().isEmpty()
? "/" : gitwebUrl.getPath());
}
}
return env.getEnvArray();
}