1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl;
28
29 import java.util.Iterator;
30
31 import org.apache.hc.core5.annotation.Internal;
32 import org.apache.hc.core5.http.HttpHeaders;
33 import org.apache.hc.core5.http.HttpMessage;
34 import org.apache.hc.core5.http.ParseException;
35 import org.apache.hc.core5.http.ProtocolException;
36 import org.apache.hc.core5.http.ProtocolVersion;
37 import org.apache.hc.core5.http.message.MessageSupport;
38 import org.apache.hc.core5.http.ssl.TLS;
39
40
41
42
43
44
45 @Internal
46 public final class ProtocolSwitchStrategy {
47
48 enum ProtocolSwitch { FAILURE, TLS }
49
50 public ProtocolVersion switchProtocol(final HttpMessage response) throws ProtocolException {
51 final Iterator<String> it = MessageSupport.iterateTokens(response, HttpHeaders.UPGRADE);
52
53 ProtocolVersion tlsUpgrade = null;
54 while (it.hasNext()) {
55 final String token = it.next();
56 if (token.startsWith("TLS")) {
57
58 try {
59 tlsUpgrade = token.length() == 3 ? TLS.V_1_2.getVersion() : TLS.parse(token.replace("TLS/", "TLSv"));
60 } catch (final ParseException ex) {
61 throw new ProtocolException("Invalid protocol: " + token);
62 }
63 } else if (token.equals("HTTP/1.1")) {
64
65 } else {
66 throw new ProtocolException("Unsupported protocol: " + token);
67 }
68 }
69 if (tlsUpgrade == null) {
70 throw new ProtocolException("Invalid protocol switch response");
71 }
72 return tlsUpgrade;
73 }
74
75 }