Skip to content

Commit a77f980

Browse files
committed
添加 solon-server-tomcat-jakarta http2 支持
1 parent 7001e2b commit a77f980

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

solon-jakarta-projects/solon-server/solon-server-tomcat-jakarta/src/main/java/org/noear/solon/server/tomcat/TomcatServer.java

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import org.apache.catalina.Wrapper;
2020
import org.apache.catalina.connector.Connector;
2121
import org.apache.catalina.startup.Tomcat;
22-
import org.apache.coyote.http11.Http11NioProtocol;
22+
import org.apache.coyote.ProtocolHandler;
23+
import org.apache.coyote.http11.Http11Nio2Protocol;
24+
import org.apache.coyote.http2.Http2Protocol;
2325
import org.apache.tomcat.util.net.SSLHostConfig;
2426
import org.apache.tomcat.util.net.SSLHostConfigCertificate;
2527
import org.noear.solon.core.util.IoUtil;
@@ -32,6 +34,7 @@
3234
import org.noear.solon.server.tomcat.ssl.TomcatSslContext;
3335

3436
import javax.net.ssl.SSLContext;
37+
import java.io.IOException;
3538

3639
/**
3740
* @author Yukai
@@ -88,28 +91,12 @@ protected Context initContext() {
8891
}
8992

9093
@Override
91-
protected void addConnector(int port, boolean isMain) throws Throwable {
94+
protected void addConnector(int port, boolean isMain) throws IOException {
9295
//::protocol
93-
final Http11NioProtocol protocol = new Http11NioProtocol();
96+
ProtocolHandler protocol = createHttp11Protocol(isMain);
9497

95-
if (ServerProps.request_maxHeaderSize > 0) {
96-
protocol.setMaxHttpHeaderSize(ServerProps.request_maxHeaderSize);
97-
}
98-
99-
if (ServerProps.request_maxBodySize > 0) {
100-
protocol.setMaxSwallowSize(ServerProps.request_maxBodySizeAsInt());
101-
}
102-
103-
protocol.setRelaxedQueryChars("[]|{}");
104-
105-
if (isMain) {
106-
//for protocol ssl
107-
if (sslConfig.isSslEnable()) {
108-
protocol.setSSLEnabled(true);
109-
protocol.setSecure(true);
110-
protocol.addSslHostConfig(createSSLHostConfig(sslConfig.getSslContext()));
111-
isSecure = true;
112-
}
98+
if (isMain && enableHttp2) {
99+
protocol.addUpgradeProtocol(new Http2Protocol());
113100
}
114101

115102

@@ -134,6 +121,33 @@ protected void addConnector(int port, boolean isMain) throws Throwable {
134121
_server.getService().addConnector(connector);
135122
}
136123

124+
private ProtocolHandler createHttp11Protocol(boolean isMain) throws IOException {
125+
final Http11Nio2Protocol protocol = new Http11Nio2Protocol();
126+
127+
if (ServerProps.request_maxHeaderSize > 0) {
128+
protocol.setMaxHttpHeaderSize(ServerProps.request_maxHeaderSize);
129+
}
130+
131+
if (ServerProps.request_maxBodySize > 0) {
132+
protocol.setMaxSwallowSize(ServerProps.request_maxBodySizeAsInt());
133+
}
134+
135+
protocol.setRelaxedQueryChars("[]|{}");
136+
137+
if (isMain) {
138+
//for protocol ssl
139+
if (sslConfig.isSslEnable()) {
140+
protocol.setSSLEnabled(true);
141+
protocol.setSecure(true);
142+
protocol.addSslHostConfig(createSSLHostConfig(sslConfig.getSslContext()));
143+
isSecure = true;
144+
}
145+
}
146+
147+
return protocol;
148+
}
149+
150+
137151
private static SSLHostConfig createSSLHostConfig(final SSLContext sslContext) {
138152
final SSLHostConfig sslHostConfig = new SSLHostConfig();
139153

solon-jakarta-projects/solon-server/solon-server-tomcat-jakarta/src/main/java/org/noear/solon/server/tomcat/TomcatServerBase.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.slf4j.LoggerFactory;
2929

3030
import javax.net.ssl.SSLContext;
31+
import java.io.IOException;
3132
import java.util.LinkedHashSet;
3233
import java.util.Set;
3334
import java.util.concurrent.Executor;
@@ -72,7 +73,7 @@ public void enableHttp2(boolean enable) {
7273
this.enableHttp2 = enable;
7374
}
7475

75-
public boolean isEnableHttp2(){
76+
public boolean isEnableHttp2() {
7677
return enableHttp2;
7778
}
7879

@@ -94,7 +95,6 @@ public HttpServerProps getProps() {
9495
}
9596

9697

97-
9898
@Override
9999
public void start(String host, int port) throws Throwable {
100100
_server = new Tomcat();
@@ -126,7 +126,7 @@ public void stop() throws Throwable {
126126
}
127127
}
128128

129-
protected abstract Context initContext() throws Throwable;
129+
protected abstract Context initContext() throws IOException;
130130

131-
protected abstract void addConnector(int port, boolean isMain) throws Throwable;
132-
}
131+
protected abstract void addConnector(int port, boolean isMain) throws IOException;
132+
}

0 commit comments

Comments
 (0)