/
DirectoryUtils.java
442 lines (401 loc) · 19 KB
/
DirectoryUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 io.undertow.server.handlers.resource;
import io.undertow.UndertowLogger;
import io.undertow.predicate.PathPrefixPredicate;
import io.undertow.predicate.Predicate;
import io.undertow.predicate.PathPrefixPredicate.PathPrefixMatchRecord;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.DateUtils;
import io.undertow.util.ETag;
import io.undertow.util.ETagUtils;
import io.undertow.util.FlexBase64;
import io.undertow.util.Headers;
import io.undertow.util.Methods;
import io.undertow.util.RedirectBuilder;
import io.undertow.util.StatusCodes;
import org.xnio.channels.Channels;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
/**
* @author Stuart Douglas
*/
public class DirectoryUtils {
/**
* Serve static resource for the directory listing
*
* @param exchange The exchange
* @return true if resources were served
*/
public static boolean sendRequestedBlobs(HttpServerExchange exchange) {
ByteBuffer buffer = null;
String type = null;
String etag = null;
String quotedEtag = null;
if ("css".equals(exchange.getQueryString())) {
buffer = Blobs.FILE_CSS_BUFFER.duplicate();
type = "text/css";
etag = Blobs.FILE_CSS_ETAG;
quotedEtag = Blobs.FILE_CSS_ETAG_QUOTED;
} else if ("js".equals(exchange.getQueryString())) {
buffer = Blobs.FILE_JS_BUFFER.duplicate();
type = "application/javascript";
etag = Blobs.FILE_JS_ETAG;
quotedEtag = Blobs.FILE_JS_ETAG_QUOTED;
}
if (buffer != null) {
if(!ETagUtils.handleIfNoneMatch(exchange, new ETag(false, etag), false)) {
exchange.setStatusCode(StatusCodes.NOT_MODIFIED);
return true;
}
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, String.valueOf(buffer.limit()));
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, type);
exchange.getResponseHeaders().put(Headers.ETAG, quotedEtag);
if (Methods.HEAD.equals(exchange.getRequestMethod())) {
exchange.endExchange();
return true;
}
exchange.getResponseSender().send(buffer);
return true;
}
return false;
}
/**
*
* @param path
* @param resource
* @return
*/
@Deprecated(forRemoval = true, since = "2.3.6.Final")
public static StringBuilder renderDirectoryListing(String path, final Resource resource) {
return renderDirectoryListing(null, path, resource);
}
public static StringBuilder renderDirectoryListing(final HttpServerExchange exchange, String path, final Resource resource) {
if (!path.endsWith("/")){
path += "/";
}
StringBuilder builder = new StringBuilder();
builder.append("<html>\n<head>\n<script src='").append(path).append("?js'></script>\n")
.append("<link rel='stylesheet' type='text/css' href='").append(path).append("?css' />\n</head>\n");
builder.append("<body onresize='growit()' onload='growit()'>\n<table id='thetable'>\n<thead>\n");
builder.append("<tr><th class='loc' colspan='3'>Directory Listing - ").append(path).append("</th></tr>\n")
.append("<tr><th class='label offset'>Name</th><th class='label'>Last Modified</th><th class='label'>Size</th></tr>\n</thead>\n")
.append("<tfoot>\n<tr><th class=\"loc footer\" colspan=\"3\">Powered by Undertow</th></tr>\n</tfoot>\n<tbody>\n");
int state = 0;
String parent = null;
if(path.length() > 1) {
for (int i = path.length() - 1; i >= 0; i--) {
if (state == 1) {
if (path.charAt(i) == '/') {
state = 2;
}
} else if (path.charAt(i) != '/') {
if (state == 2) {
parent = path.substring(0, i + 1);
break;
}
state = 1;
}
}
if(parent == null) {
parent = "/";
}
}
String relative = null;
if (exchange != null) {
final Map<String, Object> context = exchange.getAttachment(Predicate.PREDICATE_CONTEXT);
if (context != null) {
final PathPrefixPredicate.PathPrefixMatchRecord trans = (PathPrefixMatchRecord) context
.get(PathPrefixPredicate.PREFIX_MATCH_RECORD);
if (trans != null) {
if (trans.isOverWritten()) {
relative = trans.getPrefix();
if (!relative.endsWith("/") && !path.startsWith("/")) {
relative += "/";
}
}
}
}
}
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss", Locale.US);
int i = 0;
if (parent != null) {
i++;
builder.append("<tr class='odd'><td><a class='icon up' href='").append(relative == null ? parent : relative + parent).append(parent.endsWith("/") ? "" : "/").append("'>[..]</a></td><td>");
builder.append(format.format((resource.getLastModified() == null ? new Date(0L) : resource.getLastModified())))
.append("</td><td>--</td></tr>\n");
}
for (Resource entry : resource.list()) {
builder.append("<tr class='").append((++i & 1) == 1 ? "odd" : "even").append("'><td><a class='icon ");
builder.append(entry.isDirectory() ? "dir" : "file");
builder.append("' href='").append(relative == null ? path : relative + path).append(entry.getName()).append(entry.isDirectory() ? "/" : "").append("'>").append(entry.getName()).append("</a></td><td>");
builder.append(format.format((entry.getLastModified() == null) ? new Date(0L) : entry.getLastModified()))
.append("</td><td>");
if (entry.isDirectory()) {
builder.append("--");
} else {
formatSize(builder, entry.getContentLength());
}
builder.append("</td></tr>\n");
}
builder.append("</tbody>\n</table>\n</body>\n</html>");
return builder;
}
public static void renderDirectoryListing(HttpServerExchange exchange, Resource resource) {
String requestPath = exchange.getRequestPath();
if (! requestPath.endsWith("/")) {
exchange.setStatusCode(StatusCodes.FOUND);
exchange.getResponseHeaders().put(Headers.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true));
exchange.endExchange();
return;
}
StringBuilder builder = renderDirectoryListing(exchange, requestPath, resource);
try {
ByteBuffer output = ByteBuffer.wrap(builder.toString().getBytes(StandardCharsets.UTF_8));
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html; charset=UTF-8");
exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, String.valueOf(output.limit()));
exchange.getResponseHeaders().put(Headers.LAST_MODIFIED, DateUtils.toDateString(new Date()));
exchange.getResponseHeaders().put(Headers.CACHE_CONTROL, "must-revalidate");
Channels.writeBlocking(exchange.getResponseChannel(), output);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
} catch (IOException e) {
UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
}
exchange.endExchange();
}
private static StringBuilder formatSize(StringBuilder builder, Long size) {
if(size == null) {
builder.append("???");
return builder;
}
int n = 1024 * 1024 * 1024;
int type = 0;
while (size < n && n >= 1024) {
n /= 1024;
type++;
}
long top = (size * 100) / n;
long bottom = top % 100;
top /= 100;
builder.append(top);
if (bottom > 0) {
builder.append(".").append(bottom / 10);
bottom %= 10;
if (bottom > 0) {
builder.append(bottom);
}
}
switch (type) {
case 0: builder.append(" GB"); break;
case 1: builder.append(" MB"); break;
case 2: builder.append(" KB"); break;
}
return builder;
}
private DirectoryUtils() {
}
/**
* Constant Content
*
* @author Jason T. Greene
*/
public static class Blobs {
public static final String FILE_JS="function growit() {\n" +
" var table = document.getElementById(\"thetable\");\n" +
"\n" +
" var i = table.rows.length - 1;\n" +
" while (i-- > 0) {\n" +
" if (table.rows[i].id == \"eraseme\") {\n" +
" table.deleteRow(i);\n" +
" } else {\n" +
" break;\n" +
" }\n" +
" }\n" +
" table.style.height=\"\";\n" +
" var i = 0;\n" +
" while (table.offsetHeight < window.innerHeight - 24) {\n" +
" i++;\n" +
" var tbody = table.tBodies[0];\n" +
" var row = tbody.insertRow(tbody.rows.length);\n" +
" row.id=\"eraseme\";\n" +
" var cell = row.insertCell(0);\n" +
" if (table.rows.length % 2 != 0) {\n" +
" row.className=\"even eveninvis\";\n" +
" } else {\n" +
" row.className=\"odd oddinvis\";\n" +
" }\n" +
"\n" +
" cell.colSpan=3;\n" +
" cell.appendChild(document.createTextNode(\"i\"));\n" +
" }\n" +
" table.style.height=\"100%\";\n" +
" if (i > 0) {\n" +
" document.documentElement.style.overflowY=\"hidden\";\n" +
" } else {\n" +
" document.documentElement.style.overflowY=\"auto\";\n" +
" }\n" +
"}";
public static final String FILE_JS_ETAG = md5(FILE_JS.getBytes(StandardCharsets.US_ASCII));
public static final String FILE_JS_ETAG_QUOTED = '"' + FILE_JS_ETAG + '"';
public static final String FILE_CSS =
"body {\n" +
" font-family: \"Lucida Grande\", \"Lucida Sans Unicode\", \"Trebuchet MS\", Helvetica, Arial, Verdana, sans-serif;\n" +
" margin: 5px;\n" +
"}\n" +
"\n" +
"th.loc {\n" +
" background-image: linear-gradient(bottom, rgb(153,151,153) 8%, rgb(199,199,199) 54%);\n" +
" background-image: -o-linear-gradient(bottom, rgb(153,151,153) 8%, rgb(199,199,199) 54%);\n" +
" background-image: -moz-linear-gradient(bottom, rgb(153,151,153) 8%, rgb(199,199,199) 54%);\n" +
" background-image: -webkit-linear-gradient(bottom, rgb(153,151,153) 8%, rgb(199,199,199) 54%);\n" +
" background-image: -ms-linear-gradient(bottom, rgb(153,151,153) 8%, rgb(199,199,199) 54%);\n" +
" \n" +
" background-image: -webkit-gradient(\n" +
" linear,\n" +
" left bottom,\n" +
" left top,\n" +
" color-stop(0.08, rgb(153,151,153)),\n" +
" color-stop(0.54, rgb(199,199,199))\n" +
" );\n" +
" color: black;\n" +
" padding: 2px;\n" +
" font-weight: normal;\n" +
" border: solid 1px;\n" +
" font-size: 150%;\n" +
" text-align: left;\n" +
"}\n" +
"\n" +
"th.label {\n" +
" border: solid 1px;\n" +
" text-align: left;\n" +
" padding: 4px;\n" +
" padding-left: 8px;\n" +
" font-weight: normal;\n" +
" font-size: small;\n" +
" background-color: #e8e8e8;\n" +
"}\n" +
"\n" +
"th.offset {\n" +
" padding-left: 32px;\n" +
"}\n" +
"\n" +
"th.footer {\n" +
" font-size: 75%;\n" +
" text-align: right;\n" +
"}\n" +
"\n" +
"a.icon {\n" +
" padding-left: 24px;\n" +
" text-decoration: none;\n" +
" color: black;\n" +
"}\n" +
"\n" +
"a.icon:hover {\n" +
" text-decoration: underline;\n" +
"}\n" +
"\n" +
"table {\n" +
" border: 1px solid;\n" +
" border-spacing: 0px;\n" +
" width: 100%;\n" +
" border-collapse: collapse;\n" +
"}\n" +
"\n" +
"tr.odd {\n" +
" background-color: #f3f6fa;\n" +
"}\n" +
"\n" +
"tr.odd td {\n" +
" padding: 2px;\n" +
" padding-left: 8px;\n" +
" font-size: smaller;\n" +
"}\n" +
"\n" +
"tr.even {\n" +
" background-color: #ffffff;\n" +
"}\n" +
"\n" +
"tr.even td {\n" +
" padding: 2px;\n" +
" padding-left: 8px;\n" +
" font-size: smaller;\n" +
"}\n" +
"\n" +
"tr.eveninvis td {\n" +
" color: #ffffff;\n" +
"}\n" +
"\n" +
"tr.oddinvis td {\n" +
" color: #f3f6fa\n" +
"}\n" +
"\n" +
"a.up {\n" +
" background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABI0lEQVQ4y2P4//8/Ay7sM4nhPwjjUwMm0ua//Y+M0+e//QrSGDAfgvEZAjdgydHXcAzTXLjWDoxhhqBbhGLA1N0vwBhdM7ohMHVwA8yrzn4zLj/936j8FE7N6IaA1IL0gPQy2DVc+rnp3FeCmtENAekB6WXw7Lz1tWD5x/+wEIdhdI3o8iA9IL0MYZMfvq9a9+V/w+avcIzLAGQ1ID0gvQxJc56/aNn29X/vnm9wjMsAZDWtQD0gvQwFy94+6N37/f/Moz/gGJcByGpAekB6GarXf7427ciP/0vP/YRjdP/CMLIakB6QXobKDd9PN+769b91P2kYpAekl2HJhb8r11/583/9ZRIxUM+8U783MQCBGBDXAHEbibgGrBdfTiMGU2wAAPz+nxp+TnhDAAAAAElFTkSuQmCC') left center no-repeat; background-size: 16px 16px;\n" +
"}\n" +
"\n" +
"a.dir {\n" +
" background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXZwQWcAAAAQAAAAEABcxq3DAAAA+UlEQVQ4jWP4//8/AyUYTKTNf/sfGafPf/s1be47G5IMWHL0NRxP2f3mbcaCtz/RDUbHKAZM3f2CJAw3wLzq7Dfj8tP/jcpPkYRBekB6GewaLv3cdO7r/y0XSMMgPSC9DJ6dt74WLP/4v3TVZ5IwSA9IL0PY5Ifvq9Z9+d+w+StJGKQHpJchac7zFy3bvv7v3fONJNwK1APSy5C/7O2D3r3f/888+oMkDNID0stQvf7ztWlHfvxfeu4nSRikB6SXoXLD99ONu379b91PGgbpAellWHLh38r1V/78X3+ZRAzUM/fUr00MQCAGxDVA3EYirgHrpUpupAQDAPs+7c1tGDnPAAAAAElFTkSuQmCC') left center no-repeat; background-size: 16px 16px;\n" +
"}\n" +
"\n" +
"a.file {\n" +
" background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXZwQWcAAAAQAAAAEABcxq3DAAABM0lEQVQ4y5WSTW6DMBCF3xvzc4wuOEIO0kVAuUB7vJ4g3KBdoHSRROomEpusUaoAcaYLfmKoqVRLIxnJ7/M3YwJVBcknACv8b+1U9SvoP1bXa/3WNDVIAQmQBLsNOEsGQYAwDNcARgDqusbl+wIRA2NkBEyqP0s+kCOAQhhjICJdkaDIJDwEvQAhH+G+SHagWTsi4jHoAWYIOxYDZDjnb8Fn4Akvz6AHcAbx3Tp5ETwI3RwckyVtv4Fr4VEe9qq6bDB5tlnYWou2bWGtRRRF6jdwAm5Za1FVFc7nM0QERVG8A9hPDRaGpapomgZlWSJJEuR5ftpsNq8ADr9amC+SuN/vuN1uIIntdnvKsuwZwKf2wxgBxpjpX+dA4jjW4/H4kabpixt2AbvAmDX+XnsAB509ww+A8mAar+XXgQAAAABJRU5ErkJggg==') left center no-repeat;\n" +
"}";
public static final String FILE_CSS_ETAG = md5(FILE_CSS.getBytes(StandardCharsets.US_ASCII));
public static final String FILE_CSS_ETAG_QUOTED = '"' + FILE_CSS_ETAG + '"';
public static final ByteBuffer FILE_CSS_BUFFER;
public static final ByteBuffer FILE_JS_BUFFER;
static {
try {
byte[] bytes = FILE_CSS.getBytes(StandardCharsets.US_ASCII);
FILE_CSS_BUFFER = ByteBuffer.allocateDirect(bytes.length);
FILE_CSS_BUFFER.put(bytes);
FILE_CSS_BUFFER.flip();
bytes = FILE_JS.getBytes(StandardCharsets.US_ASCII);
FILE_JS_BUFFER = ByteBuffer.allocateDirect(bytes.length);
FILE_JS_BUFFER.put(bytes);
FILE_JS_BUFFER.flip();
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
/**
* Generate the MD5 hash out of the given {@link ByteBuffer}
*/
private static String md5(byte[] buffer) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buffer);
byte[] digest = md.digest();
return new String(FlexBase64.encodeBytes(digest, 0, digest.length, false), StandardCharsets.US_ASCII);
} catch (NoSuchAlgorithmException e) {
// Should never happen
throw new InternalError("MD5 not supported on this platform");
}
}
}