Skip to content

Commit 0516dd2

Browse files
vladakahornace
authored andcommitted
replace X-Forwarded-Host with configuration value
1 parent 2f9f7cb commit 0516dd2

File tree

5 files changed

+52
-37
lines changed

5 files changed

+52
-37
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ public final class Configuration {
300300
private int historyChunkCount;
301301
private boolean historyCachePerPartesEnabled = true;
302302

303+
private String serverName; // for reverse proxy environment
304+
303305
/*
304306
* types of handling history for remote SCM repositories:
305307
* ON - index history and display it in webapp
@@ -1403,6 +1405,14 @@ public void setHistoryCachePerPartesEnabled(boolean historyCachePerPartesEnabled
14031405
this.historyCachePerPartesEnabled = historyCachePerPartesEnabled;
14041406
}
14051407

1408+
public String getServerName() {
1409+
return serverName;
1410+
}
1411+
1412+
public void setServerName(String serverName) {
1413+
this.serverName = serverName;
1414+
}
1415+
14061416
/**
14071417
* Write the current configuration to a file.
14081418
*

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/RuntimeEnvironment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,14 @@ public void setDisabledRepositories(Set<String> disabledRepositories) {
14101410
syncWriteConfiguration(disabledRepositories, Configuration::setDisabledRepositories);
14111411
}
14121412

1413+
public String getServerName() {
1414+
return syncReadConfiguration(Configuration::getServerName);
1415+
}
1416+
1417+
public void setServerName(String serverName) {
1418+
syncWriteConfiguration(serverName, Configuration::setServerName);
1419+
}
1420+
14131421
/**
14141422
* Read an configuration file and set it as the current configuration.
14151423
*

opengrok-web/src/main/java/org/opengrok/web/PageConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,21 @@ public File getDataRoot() {
15081508
return dataRoot;
15091509
}
15101510

1511+
/**
1512+
* Play nice in reverse proxy environment by using pre-configured hostname
1513+
* request to construct the URLs.
1514+
* Will not work well if the scheme or port is different for proxied server
1515+
* and original server.
1516+
* @return server name
1517+
*/
1518+
public String getServerName() {
1519+
if (env.getServerName() != null) {
1520+
return env.getServerName();
1521+
} else {
1522+
return req.getServerName();
1523+
}
1524+
}
1525+
15111526
/**
15121527
* Prepare a search helper with all required information, ready to execute
15131528
* the query implied by the related request parameters and cookies.

opengrok-web/src/main/webapp/opensearch.jsp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818
1919
CDDL HEADER END
2020
21-
Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
21+
Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
2222
Portions Copyright 2011 Jens Elkner.
2323
Portions Copyright (c) 2018, 2020, Chris Fraire <[email protected]>.
2424
@@ -45,30 +45,20 @@ include file="projects.jspf"
4545
4646
// Optimize for URLs up to 128 characters.
4747
StringBuilder url = new StringBuilder(128);
48-
String ForwardedHost = request.getHeader("X-Forwarded-Host");
4948
String scheme = request.getScheme();
5049
int port = request.getServerPort();
5150
5251
url.append(scheme).append("://");
5352
54-
// Play nice in proxy environment by using hostname from the original
55-
// request to construct the URLs.
56-
// Will not work well if the scheme or port is different for proxied server
57-
// and original server. Unfortunately the X-Forwarded-Host does not seem to
58-
// contain the port number so there is no way around it.
59-
if (ForwardedHost != null) {
60-
url.append(ForwardedHost);
61-
} else {
62-
url.append(request.getServerName());
63-
64-
// Append port if needed.
65-
if ((port != 80 && scheme.equals("http")) ||
66-
(port != 443 && scheme.equals("https"))) {
67-
url.append(':').append(port);
68-
}
53+
String serverName = cfg.getServerName();
54+
url.append(serverName);
55+
56+
// Append port if needed.
57+
if ((port != 80 && scheme.equals("http")) || (port != 443 && scheme.equals("https"))) {
58+
url.append(':').append(port);
6959
}
7060
71-
String imgurl = url + cfg.getCssDir() + "/img/icon.png";
61+
String imgUrl = url + cfg.getCssDir() + "/img/icon.png";
7262
7363
/* TODO Bug 11749 ??? */
7464
StringBuilder text = new StringBuilder();
@@ -86,7 +76,7 @@ include file="projects.jspf"
8676
<ShortName>OpenGrok <%= text.toString() %></ShortName>
8777
<Description>Search in OpenGrok <%= text.toString() %></Description>
8878
<InputEncoding>UTF-8</InputEncoding>
89-
<Image height="16" width="16" type="image/png"><%= imgurl %></Image>
79+
<Image height="16" width="16" type="image/png"><%= imgUrl %></Image>
9080
<%-- <Url type="application/x-suggestions+json" template="suggestionURL"/>--%>
9181
<Url template="<%= url.toString() %>&amp;<%= QueryParameters.FULL_SEARCH_PARAM_EQ %>{searchTerms}"
9282
type="text/html"/>

opengrok-web/src/main/webapp/rss.jsp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1818
1919
CDDL HEADER END
2020
21-
Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
22-
21+
Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2322
Portions Copyright 2011 Jens Elkner.
2423
2524
--%><%@page import="
26-
java.io.File,
2725
java.text.SimpleDateFormat,
2826
java.util.Set,
2927

@@ -34,7 +32,9 @@ org.opengrok.indexer.history.HistoryGuru,
3432
org.opengrok.indexer.web.Util,
3533
org.opengrok.indexer.web.Prefix,
3634
org.opengrok.web.PageConfig"
37-
%><%@ page session="false" errorPage="error.jsp"%><%
35+
%>
36+
<%@ page import="jakarta.servlet.http.HttpServletResponse" %>
37+
<%@ page session="false" errorPage="error.jsp"%><%
3838
/* ---------------------- rss.jsp start --------------------- */
3939
{
4040
PageConfig cfg = PageConfig.get(request);
@@ -51,7 +51,6 @@ org.opengrok.web.PageConfig"
5151
}
5252
String path = cfg.getPath();
5353
String dtag = cfg.getDefineTagsIndex();
54-
String ForwardedHost = request.getHeader("X-Forwarded-Host");
5554
response.setContentType("text/xml");
5655
%><?xml version="1.0"?>
5756
<?xml-stylesheet type="text/xsl" href="<%= request.getContextPath()
@@ -90,21 +89,14 @@ org.opengrok.web.PageConfig"
9089
String replaced = entry.getMessage().split("\n")[0];
9190
%><%= Util.htmlize(entry.getRevision()) %> - <%= Util.htmlize(replaced) %></title>
9291
<link><%
93-
// Play nice in proxy environment by using hostname from the original
94-
// request to construct the URLs.
95-
// Will not work well if the scheme or port is different for proxied server
96-
// and original server. Unfortunately the X-Forwarded-Host does not seem to
97-
// contain the port number so there is no way around it.
9892
String requestURL = request.getScheme() + "://";
99-
if (ForwardedHost != null) {
100-
requestURL += ForwardedHost;
101-
} else {
102-
requestURL += request.getServerName();
103-
String port = Integer.toString(request.getLocalPort());
104-
if (!port.isEmpty()) {
105-
requestURL += ":" + port;
106-
}
93+
String serverName = cfg.getServerName();
94+
requestURL += serverName;
95+
String port = Integer.toString(request.getLocalPort());
96+
if (!port.isEmpty()) {
97+
requestURL += ":" + port;
10798
}
99+
108100
requestURL += request.getContextPath();
109101
requestURL += Prefix.HIST_L + cfg.getPath() + "#" + entry.getRevision();
110102
%><%= Util.htmlize(requestURL) %></link>

0 commit comments

Comments
 (0)