Skip to content

Commit 5ab09ea

Browse files
committed
Do not add Upgrade header if Connection header already present (the caller manually manages connection state)
1 parent 7ee083a commit 5ab09ea

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

httpclient5/src/main/java/org/apache/hc/client5/http/protocol/RequestUpgrade.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public void process(
6767
final RequestConfig requestConfig = clientContext.getRequestConfigOrDefault();
6868
if (requestConfig.isProtocolUpgradeEnabled()) {
6969
final ProtocolVersion version = request.getVersion() != null ? request.getVersion() : clientContext.getProtocolVersion();
70-
if (!request.containsHeader(HttpHeaders.UPGRADE) && version.getMajor() == 1 && version.getMinor() >= 1) {
70+
if (!request.containsHeader(HttpHeaders.UPGRADE) &&
71+
!request.containsHeader(HttpHeaders.CONNECTION) &&
72+
version.getMajor() == 1 && version.getMinor() >= 1) {
7173
if (LOG.isDebugEnabled()) {
7274
LOG.debug("Connection is upgradable: protocol version = {}", version);
7375
}

httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestUpgrade.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ void testDoUpgradeIfAlreadyTLS() throws Exception {
109109
Assertions.assertFalse(get.containsHeader(HttpHeaders.UPGRADE));
110110
}
111111

112+
@Test
113+
void testDoUpgradeIfConnectionHeaderPresent() throws Exception {
114+
final HttpRequest get = new BasicHttpRequest("GET", "/");
115+
get.addHeader(HttpHeaders.CONNECTION, "keep-alive");
116+
interceptor.process(get, null, context);
117+
Assertions.assertFalse(get.containsHeader(HttpHeaders.UPGRADE));
118+
}
119+
112120
@Test
113121
void testDoUpgradeNonSafeMethodsOrTrace() throws Exception {
114122
final HttpRequest post = new BasicHttpRequest("POST", "/");

0 commit comments

Comments
 (0)