Merge "Honor X-FORWARDED-HOST when negociating server name"
diff --git a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/GuiceModule.java b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/GuiceModule.java
index 6a52bf3..ffbb75a 100644
--- a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/GuiceModule.java
+++ b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/GuiceModule.java
@@ -14,6 +14,7 @@
 
 package com.gerritforge.gerrit.modules.virtualhost;
 
+import com.google.gerrit.httpd.HttpCanonicalWebUrlProvider;
 import com.google.gerrit.server.permissions.PermissionBackend;
 import com.google.inject.AbstractModule;
 import com.google.inject.Scopes;
@@ -29,5 +30,6 @@
             .build(WithVirtualHostUser.Factory.class));
 
     bind(PermissionBackend.class).to(VirtualHostPermissionBackend.class).in(Scopes.SINGLETON);
+    bind(HttpCanonicalWebUrlProvider.class).to(VirtualHostHttpCanonicalWebUrlProvider.class);
   }
 }
diff --git a/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
new file mode 100644
index 0000000..d364d62
--- /dev/null
+++ b/src/main/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProvider.java
@@ -0,0 +1,51 @@
+// Copyright (C) 2023 GerritForge Ltd.
+//
+// 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.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;
+
+public class VirtualHostHttpCanonicalWebUrlProvider extends HttpCanonicalWebUrlProvider {
+  private final String protocol;
+
+  @Inject
+  VirtualHostHttpCanonicalWebUrlProvider(@GerritServerConfig Config config) {
+    super(config);
+    protocol = getWebProtocol(super.get());
+  }
+
+  @Override
+  public String get() {
+    return getVirtualHostHttpCanonicalWebUrl(protocol, CurrentServerName.get(), super::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);
+  }
+}
diff --git a/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProviderTest.java b/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProviderTest.java
new file mode 100644
index 0000000..01cb7a9
--- /dev/null
+++ b/src/test/java/com/gerritforge/gerrit/modules/virtualhost/VirtualHostHttpCanonicalWebUrlProviderTest.java
@@ -0,0 +1,50 @@
+// Copyright (C) 2023 GerritForge Ltd.
+//
+// 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.gerritforge.gerrit.modules.virtualhost;
+
+import static com.google.common.truth.Truth.assertThat;
+
+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() {
+    String actual =
+        VirtualHostHttpCanonicalWebUrlProvider.getVirtualHostHttpCanonicalWebUrl(
+            "http://", Optional.of("gerrithub.io"), () -> "failure.com");
+
+    assertThat(actual).isEqualTo("http://gerrithub.io/");
+  }
+
+  @Test
+  public void fallbackToHttpCanonicalUrl() {
+    String actual =
+        VirtualHostHttpCanonicalWebUrlProvider.getVirtualHostHttpCanonicalWebUrl(
+            "https://", Optional.empty(), () -> "https://dom0.com/");
+
+    assertThat(actual).isEqualTo("https://dom0.com/");
+  }
+}