arturobernalg commented on code in PR #627: URL: https://github.com/apache/httpcomponents-client/pull/627#discussion_r2015749929
########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/ProtocolSwitchStrategy.java: ########## @@ -45,31 +49,96 @@ @Internal public final class ProtocolSwitchStrategy { - enum ProtocolSwitch { FAILURE, TLS } + private static final ProtocolVersionParser PARSER = ProtocolVersionParser.INSTANCE; + private static final Tokenizer TOKENIZER = Tokenizer.INSTANCE; public ProtocolVersion switchProtocol(final HttpMessage response) throws ProtocolException { final Iterator<String> it = MessageSupport.iterateTokens(response, HttpHeaders.UPGRADE); - ProtocolVersion tlsUpgrade = null; + ProtocolVersion httpUpgrade = null; + while (it.hasNext()) { - final String token = it.next(); + final String token = it.next().trim(); 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")); + tlsUpgrade = parseTlsToken(token); + } catch (final ParseException ex) { + throw new ProtocolException("Invalid TLS protocol: " + token, ex); + } + } else if (token.startsWith("HTTP/")) { + try { + httpUpgrade = parseHttpToken(token); } catch (final ParseException ex) { - throw new ProtocolException("Invalid protocol: " + token); + throw new ProtocolException("Invalid HTTP protocol: " + token, ex); } - } else if (token.equals("HTTP/1.1")) { - // TODO: Improve handling of HTTP protocol token once HttpVersion has a #parse method } else { throw new ProtocolException("Unsupported protocol: " + token); } } - if (tlsUpgrade == null) { + + if (tlsUpgrade != null) { + return tlsUpgrade; + } else if (httpUpgrade != null) { + return httpUpgrade; + } else { throw new ProtocolException("Invalid protocol switch response"); } - return tlsUpgrade; } -} + private ProtocolVersion parseTlsToken(final String token) throws ParseException { Review Comment: @ok2c The reason I didn’t go straight for it was to avoid cases like this: if we get something weird like `response.addHeader(HttpHeaders.UPGRADE, "HTTP/11.22")` without batting an eye, and I wanted to catch that and only allow our supported versions -- 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: dev-unsubscr...@hc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org