summaryrefslogtreecommitdiffstats
path: root/gerrit-httpd
diff options
context:
space:
mode:
authorShawn Pearce <sop@google.com>2013-05-01 17:11:28 +0100
committerShawn Pearce <sop@google.com>2013-05-14 19:27:04 -0700
commitace2c6d9f4effc5db574f01c5dcdb54caec55316 (patch)
tree83ff7f246d49469839de9abfabdd6266c6e8f34f /gerrit-httpd
parent8ea0c8b12be4076449a8d7e3bc15cbda95d93aca (diff)
Fix login servlets when canonicalWebUrl is not set
Each login servlet knows enough about the incoming request to be able to not need the canonical web address for redirection purposes. In the case gerrit.canonicalWebUrl is not set, use the incoming request to build up the URL. This solution is a work-around for the fact that somewhere before 2.5 Colby broke the HttpServletRequest scope based version of the @CanonicalWebUrl provider. Because Guice cannot supply the request in some contexts we pass the request into the provider as an argument. Long term all of these authentication methods will be ejected into their own plugins and it will be possible to revisit how this configuration is handled. Change-Id: I0e00b89020860a02b5d6ea77da5c784f5f0bb1b8
Diffstat (limited to 'gerrit-httpd')
-rw-r--r--gerrit-httpd/src/main/java/com/google/gerrit/httpd/CanonicalWebUrl.java47
-rw-r--r--gerrit-httpd/src/main/java/com/google/gerrit/httpd/HttpCanonicalWebUrlProvider.java11
-rw-r--r--gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/container/HttpLoginServlet.java8
-rw-r--r--gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/ldap/LdapLoginServlet.java18
4 files changed, 60 insertions, 24 deletions
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/CanonicalWebUrl.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/CanonicalWebUrl.java
new file mode 100644
index 0000000000..61c0cd1e6f
--- /dev/null
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/CanonicalWebUrl.java
@@ -0,0 +1,47 @@
+// Copyright (C) 2013 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.httpd;
+
+import javax.annotation.Nullable;
+import javax.servlet.http.HttpServletRequest;
+
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+public class CanonicalWebUrl {
+ private final Provider<String> configured;
+
+ @Inject
+ CanonicalWebUrl(
+ @com.google.gerrit.server.config.CanonicalWebUrl
+ @Nullable
+ Provider<String> provider) {
+ configured = provider;
+ }
+
+ public String get(HttpServletRequest req) {
+ String url = configured.get();
+ return url != null ? url : computeFromRequest(req);
+ }
+
+ static String computeFromRequest(HttpServletRequest req) {
+ StringBuffer url = req.getRequestURL();
+ url.setLength(url.length() - req.getServletPath().length());
+ if (url.charAt(url.length() - 1) != '/') {
+ url.append('/');
+ }
+ return url.toString();
+ }
+}
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/HttpCanonicalWebUrlProvider.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/HttpCanonicalWebUrlProvider.java
index 5df678b76b..5196e9c3a9 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/HttpCanonicalWebUrlProvider.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/HttpCanonicalWebUrlProvider.java
@@ -14,7 +14,6 @@
package com.google.gerrit.httpd;
-import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.CanonicalWebUrlProvider;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
@@ -26,7 +25,7 @@ import org.eclipse.jgit.lib.Config;
import javax.servlet.http.HttpServletRequest;
-/** Sets {@link CanonicalWebUrl} to current HTTP request if not configured. */
+/** Sets {@code CanonicalWebUrl} to current HTTP request if not configured. */
public class HttpCanonicalWebUrlProvider extends CanonicalWebUrlProvider {
private Provider<HttpServletRequest> requestProvider;
@@ -65,13 +64,7 @@ public class HttpCanonicalWebUrlProvider extends CanonicalWebUrlProvider {
throw noWeb;
}
}
-
- final StringBuffer url = req.getRequestURL();
- url.setLength(url.length() - req.getServletPath().length());
- if (url.charAt(url.length() - 1) != '/') {
- url.append('/');
- }
- return url.toString();
+ return CanonicalWebUrl.computeFromRequest(req);
}
// We have no way of guessing our HTTP url.
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/container/HttpLoginServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/container/HttpLoginServlet.java
index 696a585170..fe7aa23dd5 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/container/HttpLoginServlet.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/container/HttpLoginServlet.java
@@ -15,13 +15,13 @@
package com.google.gerrit.httpd.auth.container;
import com.google.gerrit.common.PageLinks;
+import com.google.gerrit.httpd.CanonicalWebUrl;
import com.google.gerrit.httpd.HtmlDomUtil;
import com.google.gerrit.httpd.WebSession;
import com.google.gerrit.server.account.AccountException;
import com.google.gerrit.server.account.AccountManager;
import com.google.gerrit.server.account.AuthRequest;
import com.google.gerrit.server.account.AuthResult;
-import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gwtexpui.server.CacheHeaders;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -57,13 +57,13 @@ class HttpLoginServlet extends HttpServlet {
LoggerFactory.getLogger(HttpLoginServlet.class);
private final Provider<WebSession> webSession;
- private final Provider<String> urlProvider;
+ private final CanonicalWebUrl urlProvider;
private final AccountManager accountManager;
private final HttpAuthFilter authFilter;
@Inject
HttpLoginServlet(final Provider<WebSession> webSession,
- @CanonicalWebUrl @Nullable final Provider<String> urlProvider,
+ final CanonicalWebUrl urlProvider,
final AccountManager accountManager,
final HttpAuthFilter authFilter) {
this.webSession = webSession;
@@ -121,7 +121,7 @@ class HttpLoginServlet extends HttpServlet {
}
final StringBuilder rdr = new StringBuilder();
- rdr.append(urlProvider.get());
+ rdr.append(urlProvider.get(req));
rdr.append('#');
if (arsp.isNew() && !token.startsWith(PageLinks.REGISTER + "/")) {
rdr.append(PageLinks.REGISTER);
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/ldap/LdapLoginServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/ldap/LdapLoginServlet.java
index cfae86c8ba..9a2a7dad8f 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/ldap/LdapLoginServlet.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/auth/ldap/LdapLoginServlet.java
@@ -17,6 +17,7 @@ package com.google.gerrit.httpd.auth.ldap;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import com.google.gerrit.common.PageLinks;
+import com.google.gerrit.httpd.CanonicalWebUrl;
import com.google.gerrit.httpd.HtmlDomUtil;
import com.google.gerrit.httpd.WebSession;
import com.google.gerrit.httpd.template.SiteHeaderFooter;
@@ -26,7 +27,7 @@ import com.google.gerrit.server.account.AccountUserNameException;
import com.google.gerrit.server.account.AuthRequest;
import com.google.gerrit.server.account.AuthResult;
import com.google.gerrit.server.auth.AuthenticationUnavailableException;
-import com.google.gerrit.server.config.CanonicalWebUrl;
+import com.google.gerrit.server.config.SitePaths;
import com.google.gwtexpui.server.CacheHeaders;
import com.google.inject.Inject;
import com.google.inject.Provider;
@@ -55,28 +56,24 @@ class LdapLoginServlet extends HttpServlet {
private final AccountManager accountManager;
private final Provider<WebSession> webSession;
- private final Provider<String> urlProvider;
+ private final CanonicalWebUrl urlProvider;
private final SiteHeaderFooter headers;
@Inject
LdapLoginServlet(AccountManager accountManager,
Provider<WebSession> webSession,
- @CanonicalWebUrl @Nullable Provider<String> urlProvider,
+ CanonicalWebUrl urlProvider,
SiteHeaderFooter headers) {
this.accountManager = accountManager;
this.webSession = webSession;
this.urlProvider = urlProvider;
this.headers = headers;
-
- if (Strings.isNullOrEmpty(urlProvider.get())) {
- log.error("gerrit.canonicalWebUrl must be set in gerrit.config");
- }
}
private void sendForm(HttpServletRequest req, HttpServletResponse res,
@Nullable String errorMessage) throws IOException {
String self = req.getRequestURI();
- String cancel = Objects.firstNonNull(urlProvider.get(), "/");
+ String cancel = Objects.firstNonNull(urlProvider.get(req), "/");
String token = getToken(req);
if (!token.equals("/")) {
cancel += "#" + token;
@@ -146,11 +143,10 @@ class LdapLoginServlet extends HttpServlet {
return;
}
- String token = getToken(req);
StringBuilder dest = new StringBuilder();
- dest.append(urlProvider.get());
+ dest.append(urlProvider.get(req));
dest.append('#');
- dest.append(token);
+ dest.append(getToken(req));
CacheHeaders.setNotCacheable(res);
webSession.get().login(ares, "1".equals(remember));