Skip to content

Commit

Permalink
Jetty http/2 support #418
Browse files Browse the repository at this point in the history
  • Loading branch information
jknack committed Sep 4, 2016
1 parent 8779643 commit 6d3e6cb
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 42 deletions.
61 changes: 59 additions & 2 deletions jooby-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<groupId>org.eclipse.jetty.http2</groupId>
<artifactId>http2-server</artifactId>
<version>${jetty.version}</version>
</dependency>

Expand All @@ -55,6 +55,12 @@
<artifactId>websocket-server</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-alpn-server</artifactId>
<version>${jetty.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.jooby</groupId>
Expand Down Expand Up @@ -110,4 +116,55 @@

</dependencies>

<profiles>
<profile>
<id>linux</id>
<activation>
<os>
<family>linux</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>1.1.33.Fork19</version>
<classifier>linux-x86_64</classifier>
</dependency>
</dependencies>
</profile>
<profile>
<id>windows_x86</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>1.1.33.Fork19</version>
<classifier>windows-x86_64</classifier>
</dependency>
</dependencies>
</profile>
<profile>
<id>mac_x86_64</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
<version>1.1.33.Fork19</version>
<classifier>osx-x86_64</classifier>
</dependency>
</dependencies>
</profile>
</profiles>

</project>
69 changes: 45 additions & 24 deletions jooby-jetty/src/main/java/org/jooby/internal/jetty/JettyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import javax.inject.Provider;
import javax.net.ssl.SSLContext;

import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
Expand All @@ -54,61 +56,67 @@

public class JettyServer implements org.jooby.spi.Server {

private static final String H2 = "h2";
private static final String H2_17 = "h2-17";
private static final String HTTP_1_1 = "http/1.1";

private static final String JETTY_HTTP = "jetty.http";
private static final String CONNECTOR = "connector";

/** The logging system. */
private final Logger log = LoggerFactory.getLogger(org.jooby.spi.Server.class);

private Server server;

@Inject
public JettyServer(final HttpHandler handler, final Config config,
public JettyServer(final HttpHandler handler, final Config conf,
final Provider<SSLContext> sslCtx) {
this.server = server(handler, config, sslCtx);
this.server = server(handler, conf, sslCtx);
}

private Server server(final HttpHandler handler, final Config config,
final Provider<SSLContext> sslCtx) {
private Server server(final HttpHandler handler, final Config conf,
final Provider<SSLContext> sslCtx) {
System.setProperty("org.eclipse.jetty.util.UrlEncoded.charset",
config.getString("jetty.url.charset"));
conf.getString("jetty.url.charset"));

System.setProperty("org.eclipse.jetty.server.Request.maxFormContentSize",
config.getBytes("server.http.MaxRequestSize").toString());
conf.getBytes("server.http.MaxRequestSize").toString());

QueuedThreadPool pool = conf(new QueuedThreadPool(), config.getConfig("jetty.threads"),
QueuedThreadPool pool = conf(new QueuedThreadPool(), conf.getConfig("jetty.threads"),
"jetty.threads");

Server server = new Server(pool);
server.setStopAtShutdown(false);

// HTTP connector
ServerConnector http = http(server, config.getConfig(JETTY_HTTP), JETTY_HTTP);
http.setPort(config.getInt("application.port"));
http.setHost(config.getString("application.host"));
ServerConnector http = http(server, conf.getConfig(JETTY_HTTP), JETTY_HTTP);
http.setPort(conf.getInt("application.port"));
http.setHost(conf.getString("application.host"));

if (config.hasPath("application.securePort")) {
boolean http2 = conf.getBoolean("server.http2.enabled");

ServerConnector https = https(server, config.getConfig(JETTY_HTTP), JETTY_HTTP,
sslCtx.get());
https.setPort(config.getInt("application.securePort"));
if (conf.hasPath("application.securePort")) {

ServerConnector https = https(server, conf.getConfig(JETTY_HTTP), JETTY_HTTP,
sslCtx.get(), http2);
https.setPort(conf.getInt("application.securePort"));

server.addConnector(https);
}

server.addConnector(http);

WebSocketPolicy wsConfig = conf(new WebSocketPolicy(WebSocketBehavior.SERVER),
config.getConfig("jetty.ws"), "jetty.ws");
conf.getConfig("jetty.ws"), "jetty.ws");
WebSocketServerFactory webSocketServerFactory = new WebSocketServerFactory(wsConfig);
webSocketServerFactory.setCreator((req, rsp) -> {
JettyWebSocket ws = new JettyWebSocket();
req.getHttpServletRequest().setAttribute(JettyWebSocket.class.getName(), ws);
return ws;
});

server.setHandler(new JettyHandler(handler, webSocketServerFactory, config
.getString("application.tmpdir"),
config.getBytes("jetty.http.FileSizeThreshold").intValue()));
server.setHandler(new JettyHandler(handler, webSocketServerFactory, conf
.getString("application.tmpdir"), conf.getBytes("jetty.FileSizeThreshold").intValue()));

return server;
}
Expand All @@ -121,11 +129,12 @@ private ServerConnector http(final Server server, final Config conf, final Strin

ServerConnector connector = new ServerConnector(server, httpFactory);

return conf(connector, conf.getConfig(CONNECTOR), path + ".connector");
return conf(connector, conf.getConfig(CONNECTOR), path + "." + CONNECTOR);
}

private ServerConnector https(final Server server, final Config conf, final String path,
final SSLContext sslContext) {
final SSLContext sslContext, final boolean http2) {

HttpConfiguration httpConf = conf(new HttpConfiguration(), conf.withoutPath(CONNECTOR),
path);

Expand All @@ -135,12 +144,24 @@ private ServerConnector https(final Server server, final Config conf, final Stri
HttpConfiguration httpsConf = new HttpConfiguration(httpConf);
httpsConf.addCustomizer(new SecureRequestCustomizer());

HttpConnectionFactory httpsFactory = new HttpConnectionFactory(httpsConf);
if (http2) {
ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory(H2, H2_17);
alpn.setDefaultProtocol(HTTP_1_1);

ServerConnector connector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "HTTP/1.1"), httpsFactory);
HTTP2ServerConnectionFactory http2Factory = new HTTP2ServerConnectionFactory(httpsConf);

return conf(connector, conf.getConfig(CONNECTOR), path + ".connector");
ServerConnector connector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, "alpn"), alpn, http2Factory);

return conf(connector, conf.getConfig(CONNECTOR), path + ".connector");
} else {
HttpConnectionFactory httpsFactory = new HttpConnectionFactory(httpsConf);

ServerConnector connector = new ServerConnector(server,
new SslConnectionFactory(sslContextFactory, HTTP_1_1), httpsFactory);

return conf(connector, conf.getConfig(CONNECTOR), path + ".connector");
}
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions jooby-jetty/src/main/resources/org/jooby/spi/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jetty {
Name = jetty task
}

FileSizeThreshold = 16k

http {
HeaderCacheSize = ${server.http.HeaderSize}

Expand All @@ -22,8 +24,6 @@ jetty {

OutputBufferSize = ${server.http.ResponseBufferSize}

FileSizeThreshold = 16k

SendServerVersion = false

SendXPoweredBy = false
Expand Down
18 changes: 18 additions & 0 deletions jooby-jetty/src/test/java/jetty/H2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package jetty;

import org.jooby.Jooby;

public class H2 extends Jooby {

{
http2();
securePort(8443);

assets("/assets/**");
assets("/", "index.html");
}

public static void main(final String[] args) throws Throwable {
run(H2::new, args);
}
}
5 changes: 5 additions & 0 deletions jooby-jetty/src/test/resources/assets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function () {

console.log('loaded');

})();
7 changes: 7 additions & 0 deletions jooby-jetty/src/test/resources/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<script type="text/javascript" src="/assets/index.js"></script>
<body>
<h1>H2</h1>
</body>
</html>
2 changes: 0 additions & 2 deletions jooby-jetty/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
</encoder>
</appender>

<logger name="org.jooby.spi.Server" level="DEBUG" />

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
Expand Down
12 changes: 0 additions & 12 deletions jooby-netty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@
<version>${netty.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.npn</groupId>
<artifactId>npn-api</artifactId>
<version>8.1.2.v20120308</version>
</dependency>

<dependency>
<groupId>org.mortbay.jetty.alpn</groupId>
<artifactId>alpn-boot</artifactId>
<version>8.1.9.v20160720</version>
</dependency>

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions jooby/src/main/java/org/jooby/internal/AppPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class AppPrinter {

private String[] urls;

private boolean http2;

public AppPrinter(final Set<Route.Definition> routes,
final Set<WebSocket.Definition> sockets,
final Config conf) {
Expand All @@ -52,6 +54,7 @@ public AppPrinter(final Set<Route.Definition> routes,
if (conf.hasPath("application.securePort")) {
this.urls[1] = "https://" + host + ":" + conf.getString("application.securePort") + path;
}
http2 = conf.getBoolean("server.http2.enabled");
}

@Override
Expand All @@ -66,6 +69,9 @@ public String toString() {
buffer.append("\n ").append(url);
}
}
if (http2) {
buffer.append(" +h2");
}
return buffer.toString();
}

Expand Down

0 comments on commit 6d3e6cb

Please sign in to comment.