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

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

Console: Move modified flags to new directory,
in prep for symlinking original flags dir to package
for package installs (ticket #2270)

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