Skip to content

Commit 225c915

Browse files
committed
Simplify containsConnectionUpgrade
1 parent 6073c21 commit 225c915

2 files changed

Lines changed: 8 additions & 31 deletions

File tree

httpclient5/src/main/java/org/apache/hc/client5/http/impl/ProtocolSwitchStrategy.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
package org.apache.hc.client5.http.impl;
2828

29+
import java.util.concurrent.atomic.AtomicBoolean;
2930
import java.util.concurrent.atomic.AtomicReference;
3031

3132
import org.apache.hc.core5.annotation.Internal;
@@ -95,29 +96,14 @@ public ProtocolVersion switchProtocol(final HttpMessage response) throws Protoco
9596
}
9697
}
9798

98-
private boolean containsConnectionUpgrade(final HttpMessage message) throws ProtocolException {
99-
final Iterator<Header> it = message.headerIterator(HttpHeaders.CONNECTION);
100-
while (it.hasNext()) {
101-
final Header header = it.next();
102-
if (header instanceof FormattedHeader) {
103-
final CharArrayBuffer buf = ((FormattedHeader) header).getBuffer();
104-
final ParserCursor cursor = new ParserCursor(0, buf.length());
105-
cursor.updatePos(((FormattedHeader) header).getValuePos());
106-
if (containsUpgradeToken(buf, cursor)) {
107-
return true;
108-
}
109-
} else {
110-
final String value = header.getValue();
111-
if (value == null) {
112-
continue;
113-
}
114-
final ParserCursor cursor = new ParserCursor(0, value.length());
115-
if (containsUpgradeToken(value, cursor)) {
116-
return true;
117-
}
99+
private boolean containsConnectionUpgrade(final HttpMessage message) {
100+
final AtomicBoolean found = new AtomicBoolean(false);
101+
MessageSupport.parseTokens(message, HttpHeaders.CONNECTION, token -> {
102+
if ("upgrade".equalsIgnoreCase(token)) {
103+
found.set(true);
118104
}
119-
}
120-
return false;
105+
});
106+
return found.get();
121107
}
122108

123109
private boolean containsUpgradeToken(final CharSequence buffer, final ParserCursor cursor) throws ProtocolException {

httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestProtocolSwitchStrategy.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,6 @@ void testSwitchInvalid() {
105105
Assertions.assertThrows(ProtocolException.class, () -> switchStrategy.switchProtocol(response3));
106106
}
107107

108-
@Test
109-
void testNullToken() throws ProtocolException {
110-
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);
111-
response.addHeader(HttpHeaders.CONNECTION, "Upgrade");
112-
response.addHeader(HttpHeaders.UPGRADE, "TLS,");
113-
response.addHeader(HttpHeaders.UPGRADE, null);
114-
Assertions.assertEquals(TLS.V_1_2.getVersion(), switchStrategy.switchProtocol(response));
115-
}
116-
117108
@Test
118109
void testWhitespaceOnlyToken() throws ProtocolException {
119110
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_SWITCHING_PROTOCOLS);

0 commit comments

Comments
 (0)