Skip to content

Commit dfd2371

Browse files
committed
fix(core): Make HTTP version configurable
1 parent 7b68ae0 commit dfd2371

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

agentscope-core/src/main/java/io/agentscope/core/model/transport/HttpTransportConfig.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class HttpTransportConfig {
4141
private final Duration keepAliveDuration;
4242
private final boolean ignoreSsl;
4343
private final ProxyConfig proxyConfig;
44+
private final HttpVersion httpVersion;
4445

4546
private HttpTransportConfig(Builder builder) {
4647
this.connectTimeout = builder.connectTimeout;
@@ -50,6 +51,7 @@ private HttpTransportConfig(Builder builder) {
5051
this.keepAliveDuration = builder.keepAliveDuration;
5152
this.ignoreSsl = builder.ignoreSsl;
5253
this.proxyConfig = builder.proxyConfig;
54+
this.httpVersion = builder.httpVersion;
5355
}
5456

5557
/**
@@ -119,6 +121,15 @@ public ProxyConfig getProxyConfig() {
119121
return proxyConfig;
120122
}
121123

124+
/**
125+
* Get the HTTP version.
126+
*
127+
* @return the HTTP version
128+
*/
129+
public HttpVersion getHttpVersion() {
130+
return httpVersion;
131+
}
132+
122133
/**
123134
* Create a new builder for HttpTransportConfig.
124135
*
@@ -148,6 +159,7 @@ public static class Builder {
148159
private Duration keepAliveDuration = Duration.ofMinutes(5);
149160
private boolean ignoreSsl = false;
150161
private ProxyConfig proxyConfig = null;
162+
private HttpVersion httpVersion = HttpVersion.HTTP_2;
151163

152164
/**
153165
* Set the connect timeout.
@@ -232,6 +244,17 @@ public Builder proxy(ProxyConfig proxyConfig) {
232244
return this;
233245
}
234246

247+
/**
248+
* Set the HTTP version to use.
249+
*
250+
* @param httpVersion the HTTP version
251+
* @return this builder
252+
*/
253+
public Builder httpVersion(HttpVersion httpVersion) {
254+
this.httpVersion = httpVersion;
255+
return this;
256+
}
257+
235258
/**
236259
* Build the HttpTransportConfig.
237260
*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2024-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.core.model.transport;
17+
18+
import java.net.http.HttpClient;
19+
20+
/**
21+
* HTTP version.
22+
*/
23+
public enum HttpVersion {
24+
25+
/**
26+
* HTTP version 1.1
27+
*/
28+
HTTP_1_1,
29+
30+
/**
31+
* HTTP version 2
32+
*/
33+
HTTP_2;
34+
35+
public HttpClient.Version toJdkHttpVersion() {
36+
return switch (this) {
37+
case HTTP_1_1 -> HttpClient.Version.HTTP_1_1;
38+
case HTTP_2 -> HttpClient.Version.HTTP_2;
39+
};
40+
}
41+
}

agentscope-core/src/main/java/io/agentscope/core/model/transport/JdkHttpTransport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.net.URI;
2828
import java.net.http.HttpClient;
2929
import java.net.http.HttpClient.Redirect;
30-
import java.net.http.HttpClient.Version;
3130
import java.net.http.HttpResponse.BodyHandlers;
3231
import java.nio.charset.StandardCharsets;
3332
import java.security.KeyManagementException;
@@ -111,7 +110,7 @@ public JdkHttpTransport(HttpClient client, HttpTransportConfig config) {
111110
private static HttpClient buildClient(HttpTransportConfig config) {
112111
HttpClient.Builder builder =
113112
HttpClient.newBuilder()
114-
.version(Version.HTTP_2)
113+
.version(config.getHttpVersion().toJdkHttpVersion())
115114
.followRedirects(Redirect.NORMAL)
116115
.connectTimeout(config.getConnectTimeout());
117116

agentscope-core/src/test/java/io/agentscope/core/model/transport/JdkHttpTransportTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.junit.jupiter.api.Assertions.assertEquals;
1919
import static org.junit.jupiter.api.Assertions.assertFalse;
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
21+
import static org.junit.jupiter.api.Assertions.assertSame;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324

@@ -1047,4 +1048,19 @@ void testStreamNdJsonFormatWithoutHeaderDefaultsToSse() {
10471048
"{\"id\":\"2\",\"text\":\"World\"}") // This is sent before [DONE] marker
10481049
.verifyComplete();
10491050
}
1051+
1052+
@Test
1053+
void testHttpVersionConfig() {
1054+
HttpTransportConfig defaults = HttpTransportConfig.defaults();
1055+
HttpTransportConfig config =
1056+
HttpTransportConfig.builder().httpVersion(HttpVersion.HTTP_1_1).build();
1057+
JdkHttpTransport jdkHttpTransport = JdkHttpTransport.builder().config(defaults).build();
1058+
JdkHttpTransport jdkHttpTransport2 = JdkHttpTransport.builder().config(config).build();
1059+
assertSame(HttpVersion.HTTP_2, defaults.getHttpVersion());
1060+
assertEquals(HttpClient.Version.HTTP_2, defaults.getHttpVersion().toJdkHttpVersion());
1061+
assertSame(HttpVersion.HTTP_1_1, config.getHttpVersion());
1062+
assertEquals(HttpClient.Version.HTTP_1_1, config.getHttpVersion().toJdkHttpVersion());
1063+
assertNotNull(jdkHttpTransport);
1064+
assertNotNull(jdkHttpTransport2);
1065+
}
10501066
}

0 commit comments

Comments
 (0)