ok2c commented on code in PR #627:
URL:
https://github.com/apache/httpcomponents-client/pull/627#discussion_r2033928882
##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/ProtocolSwitchStrategy.java:
##########
@@ -45,31 +52,106 @@
@Internal
public final class ProtocolSwitchStrategy {
- enum ProtocolSwitch { FAILURE, TLS }
+ private static final ProtocolVersionParser PROTOCOL_VERSION_PARSER =
ProtocolVersionParser.INSTANCE;
+
+ private static final Tokenizer TOKENIZER = Tokenizer.INSTANCE;
+
+ private static final Tokenizer.Delimiter UPGRADE_TOKEN_DELIMITER =
Tokenizer.delimiters(',');
+
+ @FunctionalInterface
+ private interface HeaderConsumer {
+ void accept(CharSequence buffer, Tokenizer.Cursor cursor) throws
ProtocolException;
+ }
public ProtocolVersion switchProtocol(final HttpMessage response) throws
ProtocolException {
- final Iterator<String> it = MessageSupport.iterateTokens(response,
HttpHeaders.UPGRADE);
+ final AtomicReference<ProtocolVersion> tlsUpgrade = new
AtomicReference<>();
- ProtocolVersion tlsUpgrade = null;
- while (it.hasNext()) {
- final String token = it.next();
- if (token.startsWith("TLS")) {
- // TODO: Improve handling of HTTP protocol token once
HttpVersion has a #parse method
- try {
- tlsUpgrade = token.length() == 3 ? TLS.V_1_2.getVersion()
: TLS.parse(token.replace("TLS/", "TLSv"));
- } catch (final ParseException ex) {
- throw new ProtocolException("Invalid protocol: " + token);
+ parseHeaders(response, HttpHeaders.UPGRADE, (buffer, cursor) -> {
+ while (!cursor.atEnd()) {
+ TOKENIZER.skipWhiteSpace(buffer, cursor);
+ if (cursor.atEnd()) {
+ break;
+ }
+ final int tokenStart = cursor.getPos();
+ TOKENIZER.parseToken(buffer, cursor, UPGRADE_TOKEN_DELIMITER);
+ final int tokenEnd = cursor.getPos();
+ if (tokenStart < tokenEnd) {
+ final ProtocolVersion version = parseProtocolToken(buffer,
tokenStart, tokenEnd);
+ if (version != null &&
"TLS".equalsIgnoreCase(version.getProtocol())) {
+ tlsUpgrade.set(version);
+ }
}
- } else if (token.equals("HTTP/1.1")) {
- // TODO: Improve handling of HTTP protocol token once
HttpVersion has a #parse method
+ if (!cursor.atEnd()) {
+ cursor.updatePos(cursor.getPos() + 1);
+ }
+ }
+ });
+
+ final ProtocolVersion result = tlsUpgrade.get();
+ if (result != null) {
+ return result;
+ } else {
+ throw new ProtocolException("Invalid protocol switch response: no
TLS version found");
+ }
+ }
+
+ private ProtocolVersion parseProtocolToken(final CharSequence buffer,
final int start, final int end)
+ throws ProtocolException {
+ if (start >= end) {
+ return null;
+ }
+
+ if (end - start == 3) {
Review Comment:
@arturobernalg I think there is a way to make it nicer, but it is good
enough for now
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]