Skip to content

Commit 9af9908

Browse files
authored
fix link encoding in RSS (#4830)
1 parent 6ac452e commit 9af9908

File tree

3 files changed

+40
-44
lines changed

3 files changed

+40
-44
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ public static void encode(String s, Appendable dest) throws IOException {
660660
char c = s.charAt(i);
661661
if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&' || c == '\'') {
662662
// special html characters
663-
dest.append("&#").append("" + (int) c).append(";");
663+
dest.append("&#").append(Integer.toString(c)).append(";");
664664
} else if (c == ' ') {
665665
// non-breaking space
666666
dest.append("&nbsp;");
@@ -917,24 +917,22 @@ public static void writeHAD(Writer out, String ctxE, String entry) throws IOExce
917917
/**
918918
* Wrapper around UTF-8 URL encoding of a string.
919919
*
920-
* @param q query to be encoded. If {@code null}, an empty string will be used instead.
921-
* @return null if failed, otherwise the encoded string
920+
* @param string to be encoded. If {@code null}, an empty string will be used instead.
921+
* @return {@code null} if failed, otherwise the encoded string
922922
* @see URLEncoder#encode(String, String)
923923
*/
924-
public static String uriEncode(String q) {
925-
return q == null ? "" : URLEncoder.encode(q, StandardCharsets.UTF_8);
924+
public static String uriEncode(String string) {
925+
return string == null ? "" : URLEncoder.encode(string, StandardCharsets.UTF_8);
926926
}
927927

928928
/**
929-
* Append to {@code dest} the UTF-8 URL-encoded representation of
930-
* {@code str}.
929+
* Append to {@code dest} the UTF-8 URL-encoded representation of {@code str}.
931930
* @param str a defined instance
932931
* @param dest a defined target
933932
* @throws IOException I/O
934933
*/
935934
public static void uriEncode(String str, Appendable dest) throws IOException {
936-
String uenc = uriEncode(str);
937-
dest.append(uenc);
935+
dest.append(uriEncode(str));
938936
}
939937

940938
/**
@@ -948,7 +946,6 @@ public static void uriEncode(String str, Appendable dest) throws IOException {
948946
* @see #uriEncode(String)
949947
*/
950948
public static void appendQuery(StringBuilder buf, String key, String value) {
951-
952949
if (value != null) {
953950
buf.append(AMP).append(key).append('=').append(uriEncode(value));
954951
}
@@ -1647,15 +1644,13 @@ public static String linkifyPattern(String text, Pattern pattern, String url) {
16471644
/**
16481645
* Try to complete the given URL part into full URL with server name, port, scheme, ...
16491646
* <dl>
1650-
* <dt>for request http://localhost:8080/source/xref/xxx and part
1651-
* /cgi-bin/user=</dt>
1652-
* <dd>http://localhost:8080/cgi-bin/user=</dd>
1653-
* <dt>for request http://localhost:8080/source/xref/xxx and part
1654-
* cgi-bin/user=</dt>
1655-
* <dd>http://localhost:8080/source/xref/xxx/cgi-bin/user=</dd>
1656-
* <dt>for request http://localhost:8080/source/xref/xxx and part
1657-
* http://users.com/user=</dt>
1658-
* <dd>http://users.com/user=</dd>
1647+
* <dt>for request {@code http://localhost:8080/source/xref/xxx} and part {@code /cgi-bin/user=}</dt>
1648+
* <dd>{@code http://localhost:8080/cgi-bin/user=}</dd>
1649+
* <dt>for request {@code http://localhost:8080/source/xref/xxx} and part {@code cgi-bin/user=}</dt>
1650+
* <dd>{@code http://localhost:8080/source/xref/xxx/cgi-bin/user=}</dd>
1651+
* <dt>for request {@code http://localhost:8080/source/xref/xxx} and part
1652+
* {@code http://users.com/user=}</dt>
1653+
* <dd>{@code http://users.com/user=}</dd>
16591654
* </dl>
16601655
*
16611656
* @param url the given URL part, may be already full URL

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import jakarta.servlet.http.HttpServletRequest;
6767
import jakarta.servlet.http.HttpServletResponse;
6868
import jakarta.ws.rs.core.HttpHeaders;
69+
import org.jetbrains.annotations.NotNull;
6970
import org.jetbrains.annotations.Nullable;
7071
import org.jetbrains.annotations.VisibleForTesting;
7172
import org.opengrok.indexer.Info;
@@ -691,6 +692,7 @@ public EftarFileReader getEftarReader() {
691692
*
692693
* @return an empty string if not found, the tag otherwise.
693694
*/
695+
@NotNull
694696
public String getDefineTagsIndex() {
695697
if (dtag != null) {
696698
return dtag;

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,16 @@ org.opengrok.web.PageConfig"
4040
PageConfig cfg = PageConfig.get(request);
4141
cfg.checkSourceRootExistence();
4242
43-
String redir = cfg.canProcess();
44-
if (redir == null || !redir.isEmpty()) {
45-
if (redir != null) {
46-
response.sendRedirect(redir);
43+
String redirectLocation = cfg.canProcess();
44+
if (redirectLocation == null || !redirectLocation.isEmpty()) {
45+
if (redirectLocation != null) {
46+
response.sendRedirect(redirectLocation);
4747
} else {
4848
response.sendError(HttpServletResponse.SC_NOT_FOUND);
4949
}
5050
return;
5151
}
5252
String path = cfg.getPath();
53-
String dtag = cfg.getDefineTagsIndex();
5453
response.setContentType("text/xml");
5554
%><?xml version="1.0"?>
5655
<?xml-stylesheet type="text/xsl" href="<%= request.getContextPath()
@@ -60,19 +59,19 @@ org.opengrok.web.PageConfig"
6059
<title>Changes in <%= path.isEmpty()
6160
? "Cross Reference"
6261
: Util.htmlize(cfg.getResourceFile().getName()) %></title>
63-
<description><%= Util.htmlize(dtag) %></description>
62+
<description><%= Util.htmlize(cfg.getDefineTagsIndex()) %></description>
6463
<language>en</language>
6564
<copyright>Copyright 2025</copyright>
6665
<generator>Java</generator><%
67-
History hist;
68-
if(cfg.isDir()) {
69-
hist = new DirectoryHistoryReader(cfg.getHistoryDirs()).getHistory();
66+
History history;
67+
if (cfg.isDir()) {
68+
history = new DirectoryHistoryReader(cfg.getHistoryDirs()).getHistory();
7069
} else {
71-
hist = HistoryGuru.getInstance().getHistory(cfg.getResourceFile());
70+
history = HistoryGuru.getInstance().getHistory(cfg.getResourceFile());
7271
}
73-
if (hist != null) {
72+
if (history != null) {
7473
int i = 20;
75-
for (HistoryEntry entry : hist.getHistoryEntries()) {
74+
for (HistoryEntry entry : history.getHistoryEntries()) {
7675
if (i-- <= 0) {
7776
break;
7877
}
@@ -88,17 +87,17 @@ org.opengrok.web.PageConfig"
8887
String replaced = entry.getMessage().split("\n")[0];
8988
%><%= Util.htmlize(entry.getRevision()) %> - <%= Util.htmlize(replaced) %></title>
9089
<link><%
91-
String requestURL = request.getScheme() + "://";
92-
String serverName = cfg.getServerName();
93-
requestURL += serverName;
94-
String port = Integer.toString(request.getLocalPort());
95-
if (!port.isEmpty()) {
96-
requestURL += ":" + port;
97-
}
98-
99-
requestURL += request.getContextPath();
100-
requestURL += Prefix.HIST_L + cfg.getPath() + "#" + entry.getRevision();
101-
%><%= Util.htmlize(requestURL) %></link>
90+
String requestURL = request.getScheme() +
91+
"://" +
92+
cfg.getServerName() +
93+
":" +
94+
request.getLocalPort() +
95+
Util.uriEncodePath(request.getContextPath()) +
96+
Prefix.HIST_L +
97+
Util.uriEncodePath(cfg.getPath()) +
98+
"#" +
99+
Util.uriEncode(entry.getRevision());
100+
%><%= requestURL %></link>
102101
<description><%
103102
for (String e : entry.getMessage().split("\n")) {
104103
%>
@@ -111,9 +110,9 @@ org.opengrok.web.PageConfig"
111110
if (cfg.isDir()) {
112111
Set<String> files = entry.getFiles();
113112
if (files != null) {
114-
for (String ifile : files) {
113+
for (String entryFile : files) {
115114
%>
116-
<%= Util.htmlize(ifile) %><%
115+
<%= Util.htmlize(entryFile) %><%
117116
}
118117
}
119118
} else {

0 commit comments

Comments
 (0)