source: apps/routerconsole/jsp/viewtheme.jsp @ 33ea4cf

Last change on this file since 33ea4cf was 33ea4cf, checked in by zzz <zzz@…>, 3 years ago

Console: Change sendError() to setStatus() for 3xx responses
to avoid unnecessary output

  • Property mode set to 100644
File size: 2.7 KB
Line 
1<%
2/*
3 * USE CAUTION WHEN EDITING
4 * Trailing whitespace OR NEWLINE on the last line will cause
5 * IllegalStateExceptions !!!
6 *
7 * Do not tag this file for translation.
8 */
9
10String uri = request.getRequestURI();
11if (uri.endsWith(".css")) {
12  response.setContentType("text/css");
13} else if (uri.endsWith(".png")) {
14  response.setContentType("image/png");
15} else if (uri.endsWith(".gif")) {
16  response.setContentType("image/gif");
17} else if (uri.endsWith(".jpg")) {
18  response.setContentType("image/jpeg");
19} else if (uri.endsWith(".ico")) {
20  response.setContentType("image/x-icon");
21} else if (uri.endsWith(".svg")) {
22  response.setContentType("image/svg+xml");
23}
24response.setHeader("X-Content-Type-Options", "nosniff");
25/*
26 * User or plugin themes
27 * If the request is for /themes/console/foo/bar/baz,
28 * and the property routerconsole.theme.foo=/path/to/foo,
29 * get the file from /path/to/foo/bar/baz
30 */
31String themePath = null;
32final String PFX = "/themes/console/";
33if (uri.startsWith(PFX) && uri.length() > PFX.length() + 1) {
34    String theme = uri.substring(PFX.length());
35    int slash = theme.indexOf('/');
36    if (slash > 0) {
37        theme = theme.substring(0, slash);
38        themePath = net.i2p.I2PAppContext.getGlobalContext().getProperty("routerconsole.theme." + theme);
39        if (themePath != null)
40            uri = uri.substring(PFX.length() + theme.length()); // /bar/baz
41    }
42}
43String base;
44if (themePath != null)
45    base = themePath;
46else
47    base = net.i2p.I2PAppContext.getGlobalContext().getBaseDir().getAbsolutePath() +
48              java.io.File.separatorChar + "docs";
49java.io.File file = new java.io.File(base, uri);
50long lastmod = file.lastModified();
51if (lastmod > 0) {
52    long iflast = request.getDateHeader("If-Modified-Since");
53    // iflast is -1 if not present; round down file time
54    if (iflast >= ((lastmod / 1000) * 1000)) {
55        response.setStatus(304);
56        return;
57    }
58    response.setDateHeader("Last-Modified", lastmod);
59    // cache for a day
60    response.setDateHeader("Expires", net.i2p.I2PAppContext.getGlobalContext().clock().now() + 86400000l);
61    response.setHeader("Cache-Control", "public, max-age=86400");
62}
63long length = file.length();
64if (length > 0)
65    response.setHeader("Content-Length", Long.toString(length));
66try {
67    net.i2p.util.FileUtil.readFile(uri, base, response.getOutputStream());
68} catch (java.io.IOException ioe) {
69    // prevent 'Committed' IllegalStateException from Jetty
70    if (!response.isCommitted()) {
71        response.sendError(403, ioe.toString());
72    }  else {
73        // not an error, happens when the browser closes the stream
74        net.i2p.I2PAppContext.getGlobalContext().logManager().getLog(getClass()).warn("Error serving " + uri, ioe);
75        // Jetty doesn't log this
76        throw ioe;
77    }
78}
79%>
Note: See TracBrowser for help on using the repository browser.