Copy server port number from paren cannonical URL
When `gerrit.cannonicalWebUrl` contains port number it also must be
copied to the virutal host HTTP cannonical web url, otherwise
redirection URLs in Gerrit will not work.
Bug: Issue 307967275
Change-Id: Ic472438dbad44ae83445c257b7d6a859130dd431
diff --git a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
index d364d62..ffd2c89 100644
--- a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
+++ b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
@@ -14,38 +14,33 @@
package com.gerritforge.gerrit.modules.virtualhost;
-import java.util.Optional;
-import java.util.function.Supplier;
-
-import org.eclipse.jgit.lib.Config;
-
import com.google.common.annotations.VisibleForTesting;
import com.google.gerrit.httpd.HttpCanonicalWebUrlProvider;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
+import java.net.URI;
+import java.util.Optional;
+import org.apache.http.client.utils.URIBuilder;
+import org.eclipse.jgit.lib.Config;
public class VirtualHostHttpCanonicalWebUrlProvider extends HttpCanonicalWebUrlProvider {
- private final String protocol;
+ private final URI serverUri;
@Inject
VirtualHostHttpCanonicalWebUrlProvider(@GerritServerConfig Config config) {
super(config);
- protocol = getWebProtocol(super.get());
+ serverUri = URI.create(super.get());
}
@Override
public String get() {
- return getVirtualHostHttpCanonicalWebUrl(protocol, CurrentServerName.get(), super::get);
+ return getVirtualHostHttpCanonicalWebUrl(serverUri, CurrentServerName.get());
}
@VisibleForTesting
- static String getWebProtocol(String canonicalWebUrl) {
- return canonicalWebUrl.startsWith("https://") ? "https://" : "http://";
- }
-
- @VisibleForTesting
- static String getVirtualHostHttpCanonicalWebUrl(
- String protocol, Optional<String> serverName, Supplier<String> httpCanonicalUrlSupplier) {
- return serverName.map(name -> protocol + name + "/").orElseGet(httpCanonicalUrlSupplier);
+ static String getVirtualHostHttpCanonicalWebUrl(URI baseUri, Optional<String> serverName) {
+ return serverName
+ .map(name -> new URIBuilder(baseUri).setHost(name).toString())
+ .orElse(baseUri.toString());
}
}
diff --git a/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProviderTest.java b/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProviderTest.java
index 01cb7a9..d30ec10 100644
--- a/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProviderTest.java
+++ b/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProviderTest.java
@@ -16,35 +16,38 @@
import static com.google.common.truth.Truth.assertThat;
+import java.net.URI;
import java.util.Optional;
-
import org.junit.Test;
public class VirtualHostHttpCanonicalWebUrlProviderTest {
@Test
- public void getWebProtocol() {
- assertThat(VirtualHostHttpCanonicalWebUrlProvider.getWebProtocol("")).isEqualTo("http://");
- assertThat(VirtualHostHttpCanonicalWebUrlProvider.getWebProtocol("https://localhost:8080/"))
- .isEqualTo("https://");
- assertThat(VirtualHostHttpCanonicalWebUrlProvider.getWebProtocol("http://localhost/"))
- .isEqualTo("http://");
- }
-
- @Test
public void buildFromServerName() {
+ URI baseUri = URI.create("http://failure.com/");
String actual =
VirtualHostHttpCanonicalWebUrlProvider.getVirtualHostHttpCanonicalWebUrl(
- "http://", Optional.of("gerrithub.io"), () -> "failure.com");
+ baseUri, Optional.of("gerrithub.io"));
assertThat(actual).isEqualTo("http://gerrithub.io/");
}
@Test
public void fallbackToHttpCanonicalUrl() {
+ URI baseUri = URI.create("https://dom0.com/");
String actual =
VirtualHostHttpCanonicalWebUrlProvider.getVirtualHostHttpCanonicalWebUrl(
- "https://", Optional.empty(), () -> "https://dom0.com/");
+ baseUri, Optional.empty());
assertThat(actual).isEqualTo("https://dom0.com/");
}
+
+ @Test
+ public void copyPortNumberFromCanonicalUrl() {
+ URI baseUri = URI.create("http://localhost:8080/");
+ String actual =
+ VirtualHostHttpCanonicalWebUrlProvider.getVirtualHostHttpCanonicalWebUrl(
+ baseUri, Optional.of("second.localhost"));
+
+ assertThat(actual).isEqualTo("http://second.localhost:8080/");
+ }
}