This is an automated email from the ASF dual-hosted git repository. dkulp pushed a commit to branch 3.6.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/3.6.x-fixes by this push: new d75a54c236 Make the default for http servers to enable http/2 when possible. Provide way for client to force to http/2 ahead of time. d75a54c236 is described below commit d75a54c236ad37c4a97c1fb5b0b97f9d28305a91 Author: Daniel Kulp <d...@kulp.com> AuthorDate: Fri Mar 17 12:50:19 2023 -0400 Make the default for http servers to enable http/2 when possible. Provide way for client to force to http/2 ahead of time. (cherry picked from commit a4bdbdd4ddf9c1a202c7085ebc59fb73ef96db07) --- parent/pom.xml | 10 ++++ rt/transports/http-jetty/pom.xml | 5 ++ .../http_jetty/JettyHTTPServerEngine.java | 58 +++++++++++++++++----- .../http/netty/server/NettyHttpServerEngine.java | 4 +- .../server/NettyHttpServletPipelineFactory.java | 5 +- .../transport/http/HttpServerEngineSupport.java | 27 ++++++++++ systests/jaxws/pom.xml | 15 ++++++ 7 files changed, 108 insertions(+), 16 deletions(-) diff --git a/parent/pom.xml b/parent/pom.xml index 57414b9d9c..afded8ea1f 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -1210,6 +1210,16 @@ <artifactId>jetty-alpn-server</artifactId> <version>${cxf.jetty.version}</version> </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-alpn-java-server</artifactId> + <version>${cxf.jetty.version}</version> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-annotations</artifactId> + <version>${cxf.jetty.version}</version> + </dependency> <dependency> <groupId>io.undertow</groupId> <artifactId>undertow-core</artifactId> diff --git a/rt/transports/http-jetty/pom.xml b/rt/transports/http-jetty/pom.xml index 2039a5bebf..93b15ee1e3 100644 --- a/rt/transports/http-jetty/pom.xml +++ b/rt/transports/http-jetty/pom.xml @@ -133,6 +133,11 @@ <artifactId>jetty-alpn-server</artifactId> <optional>true</optional> </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-alpn-java-server</artifactId> + <optional>true</optional> + </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> diff --git a/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java b/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java index 16bb945667..2cfe49fdba 100644 --- a/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java +++ b/rt/transports/http-jetty/src/main/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngine.java @@ -56,9 +56,13 @@ import org.apache.cxf.interceptor.Fault; import org.apache.cxf.transport.HttpUriMapper; import org.apache.cxf.transport.http.HttpServerEngineSupport; import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory; +import org.eclipse.jetty.http.BadMessageException; +import org.eclipse.jetty.http.HttpFields.Mutable; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http2.server.HTTP2CServerConnectionFactory; import org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory; +import org.eclipse.jetty.io.Connection; +import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.security.SecurityHandler; import org.eclipse.jetty.server.AbstractConnector; import org.eclipse.jetty.server.ConnectionFactory; @@ -676,26 +680,33 @@ public class JettyHTTPServerEngine implements ServerEngine, HttpServerEngineSupp HttpConnectionFactory httpFactory = new HttpConnectionFactory(httpConfig); Collection<ConnectionFactory> connectionFactories = new ArrayList<>(); - connectionFactories.add(httpFactory); result = new org.eclipse.jetty.server.ServerConnector(server); if (tlsServerParameters != null) { + connectionFactories.add(httpFactory); httpConfig.addCustomizer(new SecureRequestCustomizer(tlsServerParameters.isSniHostCheck())); - if (!isHttp2Enabled(bus)) { + if (isHttp2Enabled(bus)) { + try { + // The ALPN processors are application specific (as per Jetty docs) and are pluggable as + // additional dependency. + final ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory(); + alpn.setDefaultProtocol(httpFactory.getProtocol()); + + final SslConnectionFactory scf = new SslConnectionFactory(sslcf, alpn.getProtocol()); + connectionFactories.add(scf); + connectionFactories.add(alpn); + connectionFactories.add(new HTTP2ServerConnectionFactory(httpConfig)); + } catch (Throwable ex) { + if (isHttp2Required(bus)) { + throw ex; + } + } + } + if (connectionFactories.size() == 1) { final SslConnectionFactory scf = new SslConnectionFactory(sslcf, httpFactory.getProtocol()); connectionFactories.add(scf); - } else { - // The ALPN processors are application specific (as per Jetty docs) and are pluggable as - // additional dependency. - final ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory(); - alpn.setDefaultProtocol(httpFactory.getProtocol()); - - final SslConnectionFactory scf = new SslConnectionFactory(sslcf, alpn.getProtocol()); - connectionFactories.add(scf); - connectionFactories.add(alpn); - connectionFactories.add(new HTTP2ServerConnectionFactory(httpConfig)); } // Has to be set before the default protocol change @@ -704,7 +715,28 @@ public class JettyHTTPServerEngine implements ServerEngine, HttpServerEngineSupp String proto = (major > 9 || (major == 9 && minor >= 3)) ? "SSL" : "SSL-HTTP/1.1"; result.setDefaultProtocol(proto); } else if (isHttp2Enabled(bus)) { - connectionFactories.add(new HTTP2CServerConnectionFactory(httpConfig)); + connectionFactories.add(httpFactory); + try { + connectionFactories.add(new HTTP2CServerConnectionFactory(httpConfig) { + + @Override + public Connection upgradeConnection(Connector c, EndPoint endPoint, + org.eclipse.jetty.http.MetaData.Request request, + Mutable response101) + throws BadMessageException { + if (request.getContentLength() > 0 + || request.getFields().contains("Transfer-Encoding")) { + // if there is a body, we cannot upgrade + return null; + } + return super.upgradeConnection(c, endPoint, request, response101); + } + }); + } catch (Throwable ex) { + if (isHttp2Required(bus)) { + throw ex; + } + } result.setConnectionFactories(connectionFactories); } diff --git a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java index c198fb27fc..0cabd99505 100644 --- a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java +++ b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServerEngine.java @@ -176,11 +176,13 @@ public class NettyHttpServerEngine implements ServerEngine, HttpServerEngineSupp .option(ChannelOption.SO_REUSEADDR, true); // Set up the event pipeline factory. + // Netty has issues with "UPGRADE" requests with payloads (POST/PUT) + // when not using SSL so we'll only start Http2 if the user specifically configures it servletPipeline = new NettyHttpServletPipelineFactory( tlsServerParameters, sessionSupport, maxChunkContentSize, handlerMap, - this, applicationExecutor, isHttp2Enabled(bus)); + this, applicationExecutor, isHttp2Required(bus)); // Start the servletPipeline's timer servletPipeline.start(); bootstrap.childHandler(servletPipeline); diff --git a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServletPipelineFactory.java b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServletPipelineFactory.java index cdc93500ca..fab4ef55f8 100644 --- a/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServletPipelineFactory.java +++ b/rt/transports/http-netty/netty-server/src/main/java/org/apache/cxf/transport/http/netty/server/NettyHttpServletPipelineFactory.java @@ -374,10 +374,11 @@ public class NettyHttpServletPipelineFactory extends ChannelInitializer<Channel> return null; } } - }; + }; final HttpServerCodec sourceCodec = new HttpServerCodec(); - final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory); + final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory, + 32 * 1024 * 1024); final CleartextHttp2ServerUpgradeHandler cleartextUpgradeHandler = new CleartextHttp2ServerUpgradeHandler( sourceCodec, upgradeHandler, createHttp2ChannelInitializerPriorKnowledge()); diff --git a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServerEngineSupport.java b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServerEngineSupport.java index fd4762e0b2..18644ce294 100644 --- a/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServerEngineSupport.java +++ b/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HttpServerEngineSupport.java @@ -31,6 +31,13 @@ import org.apache.cxf.common.util.SystemPropertyAction; public interface HttpServerEngineSupport { String ENABLE_HTTP2 = "org.apache.cxf.transports.http2.enabled"; + + /** + * Check if Http2 is enabled on the Bus or system property + * Default if not configured otherwise is true + * @param bus + * @return + */ default boolean isHttp2Enabled(Bus bus) { Object value = null; @@ -42,6 +49,26 @@ public interface HttpServerEngineSupport { value = SystemPropertyAction.getPropertyOrNull(ENABLE_HTTP2); } + return !PropertyUtils.isFalse(value); + } + + /** + * Check if Http2 is enabled on the Bus or system property + * Default if not configured otherwise is false + * @param bus + * @return + */ + default boolean isHttp2Required(Bus bus) { + Object value = null; + + if (bus != null) { + value = bus.getProperty(ENABLE_HTTP2); + } + + if (value == null) { + value = SystemPropertyAction.getPropertyOrNull(ENABLE_HTTP2); + } + return PropertyUtils.isTrue(value); } } diff --git a/systests/jaxws/pom.xml b/systests/jaxws/pom.xml index 7131615b2c..73b2e4393e 100644 --- a/systests/jaxws/pom.xml +++ b/systests/jaxws/pom.xml @@ -178,6 +178,21 @@ </exclusion> </exclusions> </dependency> + <dependency> + <groupId>org.eclipse.jetty.http2</groupId> + <artifactId>http2-server</artifactId> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-alpn-server</artifactId> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-alpn-java-server</artifactId> + <optional>true</optional> + </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId>