Jetty: Add GzipHandler for eepsites on Jetty 9.3/9.4 (ticket #2599)

by adding jetty-gzip.xml. JettyStart will copy the file
and add it to the configuration list automatically.
This does not affect standard installs which are still on 9.2;
it's only for Debian installs.
See also ticket #2098.
Partial backport from branch i2p.i2p.zzz.jetty93
This commit is contained in:
zzz
2020-04-10 18:10:48 +00:00
parent 80ae2ccea6
commit 5976d4952f
3 changed files with 124 additions and 1 deletions

View File

@@ -31,8 +31,10 @@ import java.util.Properties;
import net.i2p.I2PAppContext;
import net.i2p.app.*;
import static net.i2p.app.ClientAppState.*;
import net.i2p.util.FileUtil;
import net.i2p.util.I2PAppThread;
import net.i2p.util.PortMapper;
import net.i2p.util.VersionComparator;
import org.eclipse.jetty.server.AbstractNetworkConnector;
import org.eclipse.jetty.server.Connector;
@@ -59,6 +61,9 @@ public class JettyStart implements ClientApp {
private final I2PAppContext _context;
private volatile ClientAppState _state;
private volatile int _port, _sslPort;
private static final String GZIP_DIR = "eepsite-jetty9.3";
private static final String GZIP_CONFIG = "jetty-gzip.xml";
private static final String MIN_GZIP_HANDLER_VER = "9.3";
/**
* All args must be XML file names.
@@ -81,7 +86,46 @@ public class JettyStart implements ClientApp {
* Modified from XmlConfiguration.main()
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public void parseArgs(String[] args) throws Exception {
private void parseArgs(String[] args) throws Exception {
if (VersionComparator.comp(Server.getVersion(), MIN_GZIP_HANDLER_VER) >= 0) {
// If 9.3 or higher,
// look for jetty-gzip.xml in args.
// If not found, copy over from $I2P if necessary and add to args
// so content will be gzipped.
boolean found = false;
File path = new File(".");
for (int i = 0; i < args.length; i++) {
if (args[i].toLowerCase().endsWith(".properties"))
continue;
if (args[i].toLowerCase().endsWith(GZIP_CONFIG)) {
found = true;
break;
}
// save path to dir of other xml file for use below
File f = new File(args[i]);
File p = f.getParentFile();
if (p != null)
path = p;
}
if (!found) {
File f = new File(path, GZIP_CONFIG);
boolean exists = f.exists();
if (!exists && !_context.getBaseDir().equals(_context.getConfigDir())) {
// copy jetty-gzip.xml over
File from = new File(_context.getBaseDir(), GZIP_DIR);
from = new File(from, GZIP_CONFIG);
exists = FileUtil.copy(from, f, false, true);
}
if (exists) {
// add to args
String[] nargs = new String[args.length + 1];
System.arraycopy(args, 0, nargs, 0, args.length);
nargs[args.length] = f.getPath();
args = nargs;
}
}
}
Properties properties=new Properties();
XmlConfiguration last=null;
for (int i = 0; i < args.length; i++) {

View File

@@ -1349,6 +1349,9 @@
<copy todir="pkg-temp/eepsite/" >
<fileset dir="installer/resources/eepsite/" excludes="**/.placeholder" />
</copy>
<copy todir="pkg-temp/eepsite-jetty9.3" >
<fileset dir="installer/resources/eepsite-jetty9.3" />
</copy>
<copy file="installer/resources/themes/console/images/favicon.ico" tofile="pkg-temp/eepsite/docroot/favicon.ico" />
</target>
@@ -1758,6 +1761,9 @@
<copy todir="pkg-temp/eepsite-jetty9" >
<fileset dir="installer/resources/eepsite" includes="*.xml contexts/* etc/*" />
</copy>
<copy todir="pkg-temp/eepsite-jetty9.3" >
<fileset dir="installer/resources/eepsite-jetty9.3" />
</copy>
</target>
<target name="delete-j6-update">

View File

@@ -0,0 +1,73 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<!-- =============================================================== -->
<!-- Mixin the GZIP Handler -->
<!-- This applies the GZIP Handler to the entire server -->
<!-- If a GZIP handler is required for an individual context, then -->
<!-- use a context XML (see test.xml example in distribution) -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="insertHandler">
<Arg>
<New id="GzipHandler" class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
<Set name="minGzipSize"><Property name="jetty.gzip.minGzipSize" deprecated="gzip.minGzipSize" default="512"/></Set>
<Set name="checkGzExists"><Property name="jetty.gzip.checkGzExists" deprecated="gzip.checkGzExists" default="false"/></Set>
<Set name="compressionLevel"><Property name="jetty.gzip.compressionLevel" deprecated="gzip.compressionLevel" default="-1"/></Set>
<Set name="syncFlush"><Property name="jetty.gzip.syncFlush" default="false" /></Set>
<Set name="excludedAgentPatterns">
<Array type="String">
<Item><Property name="jetty.gzip.excludedUserAgent" deprecated="gzip.excludedUserAgent" default=".*MSIE.6\.0.*"/></Item>
</Array>
</Set>
<Set name="includedMethods">
<Array type="String">
<Item>GET</Item>
<Item>POST</Item>
</Array>
</Set>
<!--
<Set name="includedPaths">
<Array type="String">
<Item>/*</Item>
</Array>
</Set>
-->
<Set name="excludedPaths">
<Array type="String">
<Item>*.su3</Item>
</Array>
</Set>
<Call name="addIncludedMimeTypes">
<Arg><Array type="String">
<Item>application/javascript</Item>
<Item>application/x-javascript</Item>
<Item>application/pdf</Item>
<Item>application/xhtml+xml</Item>
<Item>application/xml</Item>
<Item>application/x-bittorrent</Item>
<Item>image/svg+xml</Item>
<Item>text/css</Item>
<Item>text/html</Item>
<Item>text/plain</Item>
</Array></Arg>
</Call>
<!--
<Call name="addExcludedMimeTypes">
<Arg><Array type="String">
<Item>some/type</Item>
</Array></Arg>
</Call>
-->
</New>
</Arg>
</Call>
</Configure>