Skip to content

Commit a4b0cad

Browse files
committed
diff.jsp and DiffData cleanup
1 parent 3105d63 commit a4b0cad

File tree

2 files changed

+71
-61
lines changed

2 files changed

+71
-61
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private boolean getFileRevision(DiffData data, String context, String[] filepath
276276
*/
277277
public DiffData getDiffData() {
278278
DiffData data = new DiffData(getPath().substring(0, getPath().lastIndexOf(PATH_SEPARATOR)),
279-
Util.htmlize(getResourceFile().getName()));
279+
getResourceFile().getName());
280280

281281
String context = req.getContextPath();
282282
String[] filepath = new String[2];
@@ -372,7 +372,7 @@ private void populateRevisionData(DiffData data) {
372372
try {
373373
data.revision = Diff.diff(data.file[0], data.file[1]);
374374
} catch (DifferentiationFailedException e) {
375-
data.errorMsg = "Unable to get diffs: " + Util.htmlize(e.getMessage());
375+
data.errorMsg = "Unable to get diffs: " + e.getMessage();
376376
}
377377
}
378378

@@ -384,7 +384,7 @@ private void populateRevisionURLDetails(DiffData data, String[] filePath) {
384384
filePath[i] + "@" + data.rev[i], null);
385385
data.param[i] = u.getRawQuery();
386386
} catch (URISyntaxException e) {
387-
LOGGER.log(Level.WARNING, "Failed to create URI: ", e);
387+
LOGGER.log(Level.WARNING, "Failed to create URI: ", Laundromat.launderLog(e.toString()));
388388
}
389389
});
390390
}

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

Lines changed: 68 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ information: Portions Copyright [yyyy] [name of copyright owner]
1616
1717
CDDL HEADER END
1818
19-
Copyright (c) 2006, 2023, Oracle and/or its affiliates. All rights reserved.
19+
Copyright (c) 2006, 2025, Oracle and/or its affiliates. All rights reserved.
2020
Portions Copyright 2011 Jens Elkner.
2121
Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2222
--%>
@@ -48,10 +48,8 @@ private String getAnnotateRevision(DiffData data) {
4848
PageConfig cfg = PageConfig.get(request);
4949
cfg.addScript("diff");
5050
cfg.checkSourceRootExistence();
51-
/**
52-
* This block must be the first block before any other output in the
53-
* response.
54-
*
51+
/*
52+
* This block must be the first block before any other output in the response.
5553
* If there is already any output written into the response, and we
5654
* use the same response and reset the content and the headers then we have
5755
* a collision with the response streams and the "getOutputStream() has
@@ -64,8 +62,8 @@ private String getAnnotateRevision(DiffData data) {
6462
&& request.getParameter("action").equals("download")) {
6563
try (OutputStream o = response.getOutputStream()) {
6664
for (int i = 0; i < data.getRevision().size(); i++) {
67-
Delta d = data.getRevision().getDelta(i);
68-
try (InputStream in = new ByteArrayInputStream(d.toString().getBytes(StandardCharsets.UTF_8))) {
65+
Delta delta = data.getRevision().getDelta(i);
66+
try (InputStream in = new ByteArrayInputStream(delta.toString().getBytes(StandardCharsets.UTF_8))) {
6967
response.setHeader("content-disposition", "attachment; filename="
7068
+ cfg.getResourceFile().getName() + "@" + data.getRev(0)
7169
+ "-" + data.getRev(1) + ".diff");
@@ -98,24 +96,27 @@ include file="/mast.jsp"
9896
%>
9997
<div class="src">
10098
<h3 class="error">Error:</h3>
101-
<p><%= data.getErrorMsg() %></p>
99+
<p><%= Util.htmlize(data.getErrorMsg()) %></p>
102100
</div><%
103101
} else if (data.getGenre() == AbstractAnalyzer.Genre.IMAGE) {
104102
105-
String link = request.getContextPath() + Prefix.DOWNLOAD_P
106-
+ Util.htmlize(cfg.getPath());
103+
String link = request.getContextPath() + Prefix.DOWNLOAD_P + Util.htmlize(cfg.getPath());
107104
%>
108105
<div id="difftable">
109106
<table class="image" aria-label="table with old and new image">
110107
<thead>
111-
<tr><th><%= data.getFilename() %> (revision <%= data.getRev(0) %>)</th>
112-
<th><%= data.getFilename() %> (revision <%= data.getRev(1) %>)</th>
108+
<tr>
109+
<th><%= Util.htmlize(data.getFilename()) %> (revision <%= Util.htmlize(data.getRev(0)) %>)</th>
110+
<th><%= Util.htmlize(data.getFilename()) %> (revision <%= Util.htmlize(data.getRev(1)) %>)</th>
113111
</tr>
114112
</thead>
115113
<tbody>
116-
<tr><td><img src="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= data.getRev(0) %>" alt="previous image"/>
114+
<tr>
115+
<td>
116+
<img src="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= Util.uriEncode(data.getRev(0)) %>" alt="previous image"/>
117117
</td>
118-
<td><img src="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= data.getRev(1) %>" alt="new image"/>
118+
<td>
119+
<img src="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= Util.uriEncode(data.getRev(1)) %>" alt="new image"/>
119120
</td>
120121
</tr>
121122
</tbody>
@@ -124,14 +125,15 @@ include file="/mast.jsp"
124125
125126
} else if (data.getGenre() != AbstractAnalyzer.Genre.PLAIN && data.getGenre() != AbstractAnalyzer.Genre.HTML) {
126127
127-
String link = request.getContextPath() + Prefix.DOWNLOAD_P
128-
+ Util.htmlize(cfg.getPath());
128+
String link = request.getContextPath() + Prefix.DOWNLOAD_P + Util.uriEncodePath(cfg.getPath());
129129
%>
130-
<div id="src">Diffs for binary files cannot be displayed! Files are <a
131-
href="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= data.getRev(0) %>"><%=
132-
data.getFilename() %>(revision <%= data.getRev(0) %>)</a> and <a
133-
href="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= data.getRev(1) %>"><%=
134-
data.getFilename() %>(revision <%= data.getRev(1) %>)</a>.
130+
<div id="src">Diffs for binary files cannot be displayed! Files are
131+
<a href="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= Util.uriEncode(data.getRev(0)) %>">
132+
<%= Util.htmlize(data.getFilename()) %>(revision <%= Util.htmlize(data.getRev(0)) %>)
133+
</a> and
134+
<a href="<%= link %>?<%= QueryParameters.REVISION_PARAM_EQ %><%= Util.uriEncode(data.getRev(1)) %>">
135+
<%= Util.htmlize(data.getFilename()) %>(revision <%= Util.htmlize(data.getRev(1)) %>)
136+
</a>.
135137
</div><%
136138
137139
} else if (data.getRevision().size() == 0) {
@@ -163,9 +165,9 @@ include file="/mast.jsp"
163165
if (type == t) {
164166
%> <span class="active"><%= t.toString() %><%
165167
if (t == DiffType.OLD) {
166-
%> (<%= data.getRev(0) %>)<%
168+
%> (<%= Util.htmlize(data.getRev(0)) %>)<%
167169
} else if (t == DiffType.NEW) {
168-
%> (<%= data.getRev(1) %>)<%
170+
%> (<%= Util.htmlize(data.getRev(1)) %>)<%
169171
}
170172
%></span><%
171173
} else {
@@ -175,9 +177,9 @@ include file="/mast.jsp"
175177
<%= QueryParameters.DIFF_LEVEL_PARAM_EQ %><%= full ? '1' : '0'%>"><%= t.toString() %>
176178
<%
177179
if (t == DiffType.OLD) {
178-
%> (<%= data.getShortRev(0) %>)<%
180+
%> (<%= Util.htmlize(data.getShortRev(0)) %>)<%
179181
} else if (t == DiffType.NEW) {
180-
%> (<%= data.getShortRev(1) %>)<%
182+
%> (<%= Util.htmlize(data.getShortRev(1)) %>)<%
181183
}
182184
%></a></span><%
183185
}
@@ -186,22 +188,22 @@ include file="/mast.jsp"
186188
<div class="ctype"><%
187189
if (!full) {
188190
%>
189-
<span><a href="<%= reqURI %>?<%= QueryParameters.REVISION_1_PARAM_EQ %><%= rp1 %>&amp;
190-
<%= QueryParameters.REVISION_2_PARAM_EQ %><%= rp2 %>&amp;
191+
<span><a href="<%= reqURI %>?<%= QueryParameters.REVISION_1_PARAM_EQ %><%= Util.uriEncode(rp1) %>&amp;
192+
<%= QueryParameters.REVISION_2_PARAM_EQ %><%= Util.uriEncode(rp2) %>&amp;
191193
<%= QueryParameters.FORMAT_PARAM_EQ %><%= type.getAbbrev() %>&amp;
192194
<%= QueryParameters.DIFF_LEVEL_PARAM_EQ %>1">full</a></span>
193195
<span class="active">compact</span><%
194196
} else {
195197
%>
196198
<span class="active">full</span>
197-
<span> <a href="<%= reqURI %>?<%= QueryParameters.REVISION_1_PARAM_EQ %><%= rp1 %>&amp;
198-
<%= QueryParameters.REVISION_2_PARAM_EQ %><%= rp2 %>&amp;
199+
<span> <a href="<%= reqURI %>?<%= QueryParameters.REVISION_1_PARAM_EQ %><%= Util.uriEncode(rp1) %>&amp;
200+
<%= QueryParameters.REVISION_2_PARAM_EQ %><%= Util.uriEncode(rp2) %>&amp;
199201
<%= QueryParameters.FORMAT_PARAM_EQ %><%= type.getAbbrev() %>&amp;
200202
<%= QueryParameters.DIFF_LEVEL_PARAM_EQ %>0">compact</a></span><%
201203
}
202204
%><span><a href="#" id="toggle-jumper">jumper</a></span>
203-
<span><a href="<%= reqURI %>?<%= QueryParameters.REVISION_1_PARAM_EQ %><%= rp1 %>&amp;
204-
<%= QueryParameters.REVISION_2_PARAM_EQ %><%= rp2 %>&amp;
205+
<span><a href="<%= reqURI %>?<%= QueryParameters.REVISION_1_PARAM_EQ %><%= Util.uriEncode(rp1) %>&amp;
206+
<%= QueryParameters.REVISION_2_PARAM_EQ %><%= Util.uriEncode(rp2) %>&amp;
205207
<%= QueryParameters.FORMAT_PARAM_EQ %><%= DiffType.TEXT %>&amp;
206208
action=download">download diff</a></span><%
207209
%></div>
@@ -212,25 +214,33 @@ action=download">download diff</a></span><%
212214
if (type == DiffType.SIDEBYSIDE || type == DiffType.UNIFIED) {
213215
%><table class="plain" aria-label="table with old and new content"><%
214216
if (type == DiffType.SIDEBYSIDE) {
215-
String linkPrefix = request.getContextPath() + Prefix.XREF_P + Util.htmlize(cfg.getPath()) +
217+
String linkPrefix = request.getContextPath() + Prefix.XREF_P + Util.uriEncodePath(cfg.getPath()) +
216218
"?" + QueryParameters.REVISION_PARAM_EQ;
217219
%>
218220
<thead><tr>
219-
<th><a href="<%= linkPrefix %><%= data.getRev(0) %>"><%= data.getFilename() %> (<%= data.getRev(0) %>)</a></th>
220-
<th><a href="<%= linkPrefix %><%= data.getRev(1) %>"><%= data.getFilename() %> (<%= data.getRev(1) %>)</a></th>
221+
<th>
222+
<a href="<%= linkPrefix %><%= Util.uriEncode(data.getRev(0)) %>">
223+
<%= Util.htmlize(data.getFilename()) %> (<%= Util.htmlize(data.getRev(0)) %>)
224+
</a>
225+
</th>
226+
<th>
227+
<a href="<%= linkPrefix %><%= Util.uriEncode(data.getRev(1)) %>">
228+
<%= Util.htmlize(data.getFilename()) %> (<%= Util.htmlize(data.getRev(1)) %>)
229+
</a>
230+
</th>
221231
</tr></thead><%
222232
}
223233
%>
224234
<tbody><%
225235
}
226236
227-
for (int i=0; i < data.getRevision().size(); i++) {
228-
Delta d = data.getRevision().getDelta(i);
237+
for (int i = 0; i < data.getRevision().size(); i++) {
238+
Delta delta = data.getRevision().getDelta(i);
229239
if (type == DiffType.TEXT) {
230-
%><%= Util.htmlize(d.toString()) %><%
240+
%><%= Util.htmlize(delta.toString()) %><%
231241
} else {
232-
Chunk c1 = d.getOriginal();
233-
Chunk c2 = d.getRevised();
242+
Chunk c1 = delta.getOriginal();
243+
Chunk c2 = delta.getRevised();
234244
int cn1 = c1.first();
235245
int cl1 = c1.last();
236246
int cn2 = c2.first();
@@ -295,7 +305,7 @@ action=download">download diff</a></span><%
295305
if (cn1 <= cl1) {
296306
%>
297307
<tr class="chunk"><td><%
298-
for (int j = cn1; j <= cl1 ; j++) {
308+
for (int j = cn1; j <= cl1 ; j++) {
299309
%><del class="d"><%= ++ln1 %></del><%= file1[j]
300310
%><br/><%
301311
}
@@ -309,12 +319,12 @@ action=download">download diff</a></span><%
309319
%> chunk<%
310320
}
311321
%>"><td><%
312-
for (int j = cn2; j < cl2; j++) {
322+
for (int j = cn2; j < cl2; j++) {
313323
%><span class="a it"><%= ++ln2 %></span><%= file2[j]
314324
%><br/><%
315325
}
316326
%><span class="a it"><%= ++ln2 %></span><%= file2[cl2] %><%
317-
if(full) {
327+
if (full) {
318328
%><a id="<%= ln2 %>" /><%
319329
}
320330
%></td>
@@ -331,7 +341,7 @@ action=download">download diff</a></span><%
331341
Util.htmlize(file1[j]) %><br/><%
332342
}
333343
%></td><td><%
334-
for (int j = ln2; j < cn2 ; j++) {
344+
for (int j = ln2; j < cn2 ; j++) {
335345
%><span class="it"><%= ++ln2 %></span><%=
336346
Util.htmlize(file2[j]) %><br/><%
337347
}
@@ -375,11 +385,11 @@ action=download">download diff</a></span><%
375385
}
376386
%>
377387
<tr class="k chunk"><td><%
378-
for (int j = cn1; j <= cl1; j++) {
388+
for (int j = cn1; j <= cl1; j++) {
379389
%><span class="it"><%= ++ln1 %></span><%= file1[j] %><br/><%
380390
}
381391
%></td><td><%
382-
for (int j = cn2; j <= cl2; j++) {
392+
for (int j = cn2; j <= cl2; j++) {
383393
%><span class="it"><%= ++ln2 %></span><a id="<%= ln2 %>"></a><%=
384394
file2[j] %><br/><%
385395
}
@@ -412,7 +422,7 @@ action=download">download diff</a></span><%
412422
}
413423
}
414424
}
415-
for (int j = cn1; j <= cl1 ; j++) {
425+
for (int j = cn1; j <= cl1 ; j++) {
416426
%><span class="it"><%= ++ln1 %></span><%= file1[j] %><br/><%
417427
}
418428
if (full) {
@@ -422,7 +432,7 @@ action=download">download diff</a></span><%
422432
} else if (type == DiffType.NEW) {
423433
if (cn2 > ln2) {
424434
if (full || cn2 - ln2 < 20) {
425-
for (int j = ln2; j < cn2 ; j++) {
435+
for (int j = ln2; j < cn2 ; j++) {
426436
%><span class="it"><%= ++ln2 %></span><%=
427437
Util.htmlize(file2[j]) %><br/><%
428438
}
@@ -444,7 +454,7 @@ action=download">download diff</a></span><%
444454
}
445455
}
446456
}
447-
for (int j = cn2; j <= cl2 ; j++) {
457+
for (int j = cn2; j <= cl2 ; j++) {
448458
%><span class="it"><%= ++ln2 %></span><%= file2[j] %><br/><%
449459
}
450460
if (full) {
@@ -459,11 +469,11 @@ action=download">download diff</a></span><%
459469
if (full || file1.length - ln1 < 20) {
460470
%>
461471
<tr><td><%
462-
for (int j = ln1; j < file1.length ; j++) {
472+
for (int j = ln1; j < file1.length; j++) {
463473
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file1[j]) %><br/><%
464474
}
465475
%></td><td><%
466-
for (int j = ln2; j < file2.length ; j++) {
476+
for (int j = ln2; j < file2.length; j++) {
467477
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file2[j]) %><br/><%
468478
}
469479
%></td>
@@ -473,12 +483,12 @@ action=download">download diff</a></span><%
473483
} else {
474484
%>
475485
<tr><td><%
476-
for (int j = ln1; j < ln1 + 8 ; j++) {
486+
for (int j = ln1; j < ln1 + 8; j++) {
477487
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file1[j]) %><br/><%
478488
}
479489
%><br/> --- <strong><%= file1.length - ln1 - 8
480490
%> unchanged lines hidden</strong> --- </td><td><%
481-
for (int j = ln2; j < ln2 + 8 ; j++) {
491+
for (int j = ln2; j < ln2 + 8; j++) {
482492
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file2[j]) %><br/><%
483493
}
484494
%><br/>--- <strong><%= file1.length - ln1 - 8
@@ -491,7 +501,7 @@ action=download">download diff</a></span><%
491501
if (full || file2.length - ln2 < 20) {
492502
%>
493503
<tr><td><%
494-
for (int j = ln2; j < file2.length ; j++) {
504+
for (int j = ln2; j < file2.length; j++) {
495505
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file2[j]) %><br/><%
496506
}
497507
%></td>
@@ -501,7 +511,7 @@ action=download">download diff</a></span><%
501511
} else {
502512
%>
503513
<tr><td><%
504-
for (int j = ln2; j < ln2 + 8 ; j++) {
514+
for (int j = ln2; j < ln2 + 8; j++) {
505515
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file2[j]) %><br/><%
506516
}
507517
%><br/>--- <strong><%= file2.length - ln2 - 8
@@ -512,23 +522,23 @@ action=download">download diff</a></span><%
512522
}
513523
} else if (type == DiffType.OLD) {
514524
if (full || file1.length - ln1 < 20) {
515-
for (int j = ln1; j < file1.length ; j++) {
525+
for (int j = ln1; j < file1.length; j++) {
516526
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file1[j]) %><br/><%
517527
}
518528
} else {
519-
for (int j = ln1; j < ln1 + 8 ; j++) {
529+
for (int j = ln1; j < ln1 + 8; j++) {
520530
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file1[j]) %><br/><%
521531
}
522532
%><br/> --- <strong><%= file1.length - ln1 - 8
523533
%> unchanged lines hidden</strong> ---<br/><%
524534
}
525535
} else if (type == DiffType.NEW) {
526536
if (full || file2.length - ln2 < 20) {
527-
for (int j = ln2; j < file2.length ; j++) {
537+
for (int j = ln2; j < file2.length; j++) {
528538
%><span class="it"><%= j+1 %></span><%=Util.htmlize(file2[j])%><br/><%
529539
}
530540
} else {
531-
for (int j = ln2; j < ln2 + 8 ; j++) {
541+
for (int j = ln2; j < ln2 + 8; j++) {
532542
%><span class="it"><%= j+1 %></span><%= Util.htmlize(file2[j]) %><br/><%
533543
}
534544
%><br/> --- <strong><%= file2.length - ln2 - 8

0 commit comments

Comments
 (0)