source: apps/routerconsole/jsp/flags.jsp @ a845d4f

Last change on this file since a845d4f was a845d4f, checked in by zzz <zzz@…>, 4 years ago

Servlets: Add Accept-Ranges headers

  • 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
10/**
11 *  flags.jsp?c=de => icons/flags/de.png
12 *  flags.jsp?c=de&s=48 => icons/flags48x48/de.png
13 *  with headers set so the browser caches.
14 */
15String c = request.getParameter("c");
16if (c != null &&
17    (c.length() == 2 || c.length() == 7) &&
18    c.replaceAll("[a-z0-9_]", "").length() == 0) {
19    String flagSet = "flags";
20    String s = request.getParameter("s");
21    if ("48".equals(s)) {
22        flagSet = "flags48x48";
23    }
24    java.io.OutputStream cout = response.getOutputStream();
25    String base = net.i2p.I2PAppContext.getGlobalContext().getBaseDir().getAbsolutePath();
26    String file = "docs" + java.io.File.separatorChar + "icons" + java.io.File.separatorChar +
27                  flagSet + java.io.File.separatorChar + c + ".png";
28    java.io.File ffile = new java.io.File(base, file);
29    long lastmod = ffile.lastModified();
30    if (lastmod > 0) {
31        long iflast = request.getDateHeader("If-Modified-Since");
32        // iflast is -1 if not present; round down file time
33        if (iflast >= ((lastmod / 1000) * 1000)) {
34            response.setStatus(304);
35            return;
36        }
37        response.setDateHeader("Last-Modified", lastmod);
38        // cache for a day
39        response.setDateHeader("Expires", net.i2p.I2PAppContext.getGlobalContext().clock().now() + 86400000l);
40        response.setHeader("Cache-Control", "public, max-age=604800");
41        response.setHeader("X-Content-Type-Options", "nosniff");
42    }
43    long length = ffile.length();
44    if (length > 0)
45        response.setHeader("Content-Length", Long.toString(length));
46    response.setContentType("image/png");
47    response.setHeader("Accept-Ranges", "none");
48    try {
49        net.i2p.util.FileUtil.readFile(file, base, cout);
50    } catch (java.io.IOException ioe) {
51        // prevent 'Committed' IllegalStateException from Jetty
52        if (!response.isCommitted()) {
53            response.sendError(403, ioe.toString());
54        }  else {
55            // not an error, happens when the browser closes the stream
56            net.i2p.I2PAppContext.getGlobalContext().logManager().getLog(getClass()).warn("Error serving flags/" + c + ".png", ioe);
57            // Jetty doesn't log this
58            throw ioe;
59        }
60    }
61} else {
62    /*
63     *  Send a 403 instead of a 404, because the server sends error.jsp
64     *  for 404 errors, complete with the summary bar, which would be
65     *  a huge load for a page full of flags if the user didn't have the
66     *  flags directory for some reason.
67     */
68    response.sendError(403, "No flag specified");
69}
70%>
Note: See TracBrowser for help on using the repository browser.