Proxy: Precompress error page CSS

This commit is contained in:
zzz
2021-04-11 09:06:23 -04:00
parent 39bebaff7c
commit 926fdc097f
4 changed files with 34 additions and 11 deletions

View File

@@ -88,9 +88,17 @@
<property name="workspace.changes.j.tr" value="" />
<mkdir dir="./build/obj/net/i2p/i2ptunnel/resources"/>
<copy todir="./build/obj/net/i2p/i2ptunnel/resources">
<fileset dir="../../routerconsole/jsp/" includes="themes/console/*/*.css themes/console/images/i2plogo.png themes/console/images/favicon.ico themes/console/images/itoopie_sm.png themes/console/images/buttons/go.png themes/console/images/buttons/yes.png" />
<fileset dir="../../routerconsole/jsp/" includes="themes/console/images/i2plogo.png themes/console/images/favicon.ico themes/console/images/itoopie_sm.png themes/console/images/buttons/go.png themes/console/images/buttons/yes.png" />
<fileset dir="../resources" includes="**/*.ht" />
</copy>
<mkdir dir="./build/obj/net/i2p/i2ptunnel/resources/themes/console/light"/>
<mkdir dir="./build/obj/net/i2p/i2ptunnel/resources/themes/console/dark"/>
<!--
- Pre-gzip the css files.
- LocalHTTPServer will convert the filenames, and gunzip if necessary.
-->
<gzip src="../../routerconsole/jsp/themes/console/light/console.css" destfile="./build/obj/net/i2p/i2ptunnel/resources/themes/console/light/console.css.gz" />
<gzip src="../../routerconsole/jsp/themes/console/dark/console.css" destfile="./build/obj/net/i2p/i2ptunnel/resources/themes/console/dark/console.css.gz" />
<jar destfile="./build/i2ptunnel.jar" basedir="./build/obj" excludes="**/ui/*.class **/web/*.class" >
<manifest>
<attribute name="Main-Class" value="net.i2p.i2ptunnel.I2PTunnel" />

View File

@@ -17,9 +17,9 @@ import net.i2p.data.DataHelper;
* so we don't have a Reusable version of this.
*
* Modified from net.i2p.util.ResettableGZIPInputStream to use Java 6 InflaterOutputstream
* @since 0.9.21
* @since 0.9.21, public since 0.9.50 for LocalHTTPServer
*/
class GunzipOutputStream extends InflaterOutputStream {
public class GunzipOutputStream extends InflaterOutputStream {
private static final int FOOTER_SIZE = 8; // CRC32 + ISIZE
private final CRC32 _crc32;
private final byte _buf1[] = new byte[1];

View File

@@ -417,6 +417,7 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
String referer = null;
URI origRequestURI = null;
boolean preserveConnectionHeader = false;
boolean allowGzip = false;
while((line = reader.readLine(method)) != null) {
line = line.trim();
if(_log.shouldLog(Log.DEBUG)) {
@@ -985,8 +986,9 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
// strip the accept-blah headers, as they vary dramatically from
// browser to browser
// But allow Accept-Encoding: gzip, deflate
if(!lowercaseLine.startsWith("accept-encoding: ") &&
!Boolean.parseBoolean(getTunnel().getClientOptions().getProperty(PROP_ACCEPT))) {
if (lowercaseLine.startsWith("accept-encoding: ")) {
allowGzip = lowercaseLine.contains("gzip");
} else if (!Boolean.parseBoolean(getTunnel().getClientOptions().getProperty(PROP_ACCEPT))) {
line = null;
continue;
}
@@ -1163,7 +1165,7 @@ public class I2PTunnelHTTPClient extends I2PTunnelHTTPClientBase implements Runn
Boolean.parseBoolean(getTunnel().getClientOptions().getProperty(PROP_DISABLE_HELPER))) {
out.write(ERR_HELPER_DISABLED.getBytes("UTF-8"));
} else {
LocalHTTPServer.serveLocalFile(_context, sockMgr, out, method, internalPath, internalRawQuery, _proxyNonce);
LocalHTTPServer.serveLocalFile(_context, sockMgr, out, method, internalPath, internalRawQuery, _proxyNonce, allowGzip);
}
} catch (IOException ioe) {
// ignore

View File

@@ -28,6 +28,7 @@ import net.i2p.data.Destination;
import net.i2p.data.PrivateKey;
import net.i2p.data.PublicKey;
import net.i2p.data.SigningPublicKey;
import net.i2p.i2ptunnel.GunzipOutputStream;
import net.i2p.i2ptunnel.I2PTunnelHTTPClientBase;
import net.i2p.util.FileUtil;
import net.i2p.util.PortMapper;
@@ -111,10 +112,11 @@ public abstract class LocalHTTPServer {
* @param sockMgr only for /b32, otherwise ignored
* @param targetRequest decoded path only, non-null
* @param query raw (encoded), may be null
* @param allowGzip may we send a gzipped response?
*/
public static void serveLocalFile(I2PAppContext context, I2PSocketManager sockMgr,
OutputStream out, String method, String targetRequest,
String query, String proxyNonce) throws IOException {
String query, String proxyNonce, boolean allowGzip) throws IOException {
//System.err.println("targetRequest: \"" + targetRequest + "\"");
// a home page message for the curious...
if (targetRequest.equals("/")) {
@@ -129,23 +131,34 @@ public abstract class LocalHTTPServer {
// theme hack
if (filename.startsWith("themes/console/default/"))
filename = filename.replaceFirst("default", context.getProperty("routerconsole.theme", "light"));
if (filename.endsWith(".css"))
filename = filename + ".gz";
InputStream in = getResource(filename);
if (in != null) {
try {
String type;
if (filename.endsWith(".css"))
type = "text/css";
if (filename.endsWith(".css.gz"))
type = "text/css; charset=UTF-8";
else if (filename.endsWith(".ico"))
type = "image/x-icon";
else if (filename.endsWith(".png"))
type = "image/png";
else if (filename.endsWith(".jpg"))
type = "image/jpeg";
else type = "text/html";
else type = "text/html; charset=UTF-8";
out.write("HTTP/1.1 200 OK\r\nContent-Type: ".getBytes("UTF-8"));
out.write(type.getBytes("UTF-8"));
if (allowGzip && filename.endsWith(".gz"))
out.write("\r\nContent-Encoding: gzip".getBytes("UTF-8"));
out.write("\r\nCache-Control: max-age=86400\r\nConnection: close\r\nProxy-Connection: close\r\n\r\n".getBytes("UTF-8"));
DataHelper.copy(in, out);
if (!allowGzip && filename.endsWith(".gz")) {
// gunzip on the fly. should be very rare, all browsers should support gzip
OutputStream out2 = new GunzipOutputStream(out);
DataHelper.copy(in, out2);
out2.flush();
} else {
DataHelper.copy(in, out);
}
} finally {
try { in.close(); } catch (IOException ioe) {}
}