3030import javax .inject .Provider ;
3131import javax .net .ssl .SSLContext ;
3232
33+ import org .eclipse .jetty .alpn .server .ALPNServerConnectionFactory ;
34+ import org .eclipse .jetty .http2 .server .HTTP2ServerConnectionFactory ;
3335import org .eclipse .jetty .server .HttpConfiguration ;
3436import org .eclipse .jetty .server .HttpConnectionFactory ;
3537import org .eclipse .jetty .server .SecureRequestCustomizer ;
5456
5557public class JettyServer implements org .jooby .spi .Server {
5658
59+ private static final String H2 = "h2" ;
60+ private static final String H2_17 = "h2-17" ;
61+ private static final String HTTP_1_1 = "http/1.1" ;
62+
5763 private static final String JETTY_HTTP = "jetty.http" ;
5864 private static final String CONNECTOR = "connector" ;
65+
5966 /** The logging system. */
6067 private final Logger log = LoggerFactory .getLogger (org .jooby .spi .Server .class );
6168
6269 private Server server ;
6370
6471 @ Inject
65- public JettyServer (final HttpHandler handler , final Config config ,
72+ public JettyServer (final HttpHandler handler , final Config conf ,
6673 final Provider <SSLContext > sslCtx ) {
67- this .server = server (handler , config , sslCtx );
74+ this .server = server (handler , conf , sslCtx );
6875 }
6976
70- private Server server (final HttpHandler handler , final Config config ,
71- final Provider <SSLContext > sslCtx ) {
77+ private Server server (final HttpHandler handler , final Config conf ,
78+ final Provider <SSLContext > sslCtx ) {
7279 System .setProperty ("org.eclipse.jetty.util.UrlEncoded.charset" ,
73- config .getString ("jetty.url.charset" ));
80+ conf .getString ("jetty.url.charset" ));
7481
7582 System .setProperty ("org.eclipse.jetty.server.Request.maxFormContentSize" ,
76- config .getBytes ("server.http.MaxRequestSize" ).toString ());
83+ conf .getBytes ("server.http.MaxRequestSize" ).toString ());
7784
78- QueuedThreadPool pool = conf (new QueuedThreadPool (), config .getConfig ("jetty.threads" ),
85+ QueuedThreadPool pool = conf (new QueuedThreadPool (), conf .getConfig ("jetty.threads" ),
7986 "jetty.threads" );
8087
8188 Server server = new Server (pool );
8289 server .setStopAtShutdown (false );
8390
8491 // HTTP connector
85- ServerConnector http = http (server , config .getConfig (JETTY_HTTP ), JETTY_HTTP );
86- http .setPort (config .getInt ("application.port" ));
87- http .setHost (config .getString ("application.host" ));
92+ ServerConnector http = http (server , conf .getConfig (JETTY_HTTP ), JETTY_HTTP );
93+ http .setPort (conf .getInt ("application.port" ));
94+ http .setHost (conf .getString ("application.host" ));
8895
89- if ( config . hasPath ( "application.securePort" )) {
96+ boolean http2 = conf . getBoolean ( "server.http2.enabled" );
9097
91- ServerConnector https = https (server , config .getConfig (JETTY_HTTP ), JETTY_HTTP ,
92- sslCtx .get ());
93- https .setPort (config .getInt ("application.securePort" ));
98+ if (conf .hasPath ("application.securePort" )) {
99+
100+ ServerConnector https = https (server , conf .getConfig (JETTY_HTTP ), JETTY_HTTP ,
101+ sslCtx .get (), http2 );
102+ https .setPort (conf .getInt ("application.securePort" ));
94103
95104 server .addConnector (https );
96105 }
97106
98107 server .addConnector (http );
99108
100109 WebSocketPolicy wsConfig = conf (new WebSocketPolicy (WebSocketBehavior .SERVER ),
101- config .getConfig ("jetty.ws" ), "jetty.ws" );
110+ conf .getConfig ("jetty.ws" ), "jetty.ws" );
102111 WebSocketServerFactory webSocketServerFactory = new WebSocketServerFactory (wsConfig );
103112 webSocketServerFactory .setCreator ((req , rsp ) -> {
104113 JettyWebSocket ws = new JettyWebSocket ();
105114 req .getHttpServletRequest ().setAttribute (JettyWebSocket .class .getName (), ws );
106115 return ws ;
107116 });
108117
109- server .setHandler (new JettyHandler (handler , webSocketServerFactory , config
110- .getString ("application.tmpdir" ),
111- config .getBytes ("jetty.http.FileSizeThreshold" ).intValue ()));
118+ server .setHandler (new JettyHandler (handler , webSocketServerFactory , conf
119+ .getString ("application.tmpdir" ), conf .getBytes ("jetty.FileSizeThreshold" ).intValue ()));
112120
113121 return server ;
114122 }
@@ -121,11 +129,12 @@ private ServerConnector http(final Server server, final Config conf, final Strin
121129
122130 ServerConnector connector = new ServerConnector (server , httpFactory );
123131
124- return conf (connector , conf .getConfig (CONNECTOR ), path + ".connector" );
132+ return conf (connector , conf .getConfig (CONNECTOR ), path + "." + CONNECTOR );
125133 }
126134
127135 private ServerConnector https (final Server server , final Config conf , final String path ,
128- final SSLContext sslContext ) {
136+ final SSLContext sslContext , final boolean http2 ) {
137+
129138 HttpConfiguration httpConf = conf (new HttpConfiguration (), conf .withoutPath (CONNECTOR ),
130139 path );
131140
@@ -135,12 +144,24 @@ private ServerConnector https(final Server server, final Config conf, final Stri
135144 HttpConfiguration httpsConf = new HttpConfiguration (httpConf );
136145 httpsConf .addCustomizer (new SecureRequestCustomizer ());
137146
138- HttpConnectionFactory httpsFactory = new HttpConnectionFactory (httpsConf );
147+ if (http2 ) {
148+ ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory (H2 , H2_17 );
149+ alpn .setDefaultProtocol (HTTP_1_1 );
139150
140- ServerConnector connector = new ServerConnector (server ,
141- new SslConnectionFactory (sslContextFactory , "HTTP/1.1" ), httpsFactory );
151+ HTTP2ServerConnectionFactory http2Factory = new HTTP2ServerConnectionFactory (httpsConf );
142152
143- return conf (connector , conf .getConfig (CONNECTOR ), path + ".connector" );
153+ ServerConnector connector = new ServerConnector (server ,
154+ new SslConnectionFactory (sslContextFactory , "alpn" ), alpn , http2Factory );
155+
156+ return conf (connector , conf .getConfig (CONNECTOR ), path + ".connector" );
157+ } else {
158+ HttpConnectionFactory httpsFactory = new HttpConnectionFactory (httpsConf );
159+
160+ ServerConnector connector = new ServerConnector (server ,
161+ new SslConnectionFactory (sslContextFactory , HTTP_1_1 ), httpsFactory );
162+
163+ return conf (connector , conf .getConfig (CONNECTOR ), path + ".connector" );
164+ }
144165 }
145166
146167 @ Override
0 commit comments