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
28 package org.apache.hc.core5.http.ssl;
29
30 import java.util.BitSet;
31
32 import org.apache.hc.core5.http.ParseException;
33 import org.apache.hc.core5.http.ProtocolVersion;
34 import org.apache.hc.core5.util.Tokenizer;
35
36 final class TlsVersionParser {
37
38 public final static TlsVersionParserrsionParser.html#TlsVersionParser">TlsVersionParser INSTANCE = new TlsVersionParser();
39
40 private final Tokenizer tokenizer;
41
42 TlsVersionParser() {
43 this.tokenizer = Tokenizer.INSTANCE;
44 }
45
46 ProtocolVersion parse(
47 final CharSequence buffer,
48 final Tokenizer.Cursor cursor,
49 final BitSet delimiters) throws ParseException {
50 final int lowerBound = cursor.getLowerBound();
51 final int upperBound = cursor.getUpperBound();
52
53 int pos = cursor.getPos();
54 if (pos + 4 > cursor.getUpperBound()) {
55 throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
56 }
57 if (buffer.charAt(pos) != 'T' || buffer.charAt(pos + 1) != 'L' || buffer.charAt(pos + 2) != 'S'
58 || buffer.charAt(pos + 3) != 'v') {
59 throw new ParseException("Invalid TLS protocol version", buffer, lowerBound, upperBound, pos);
60 }
61 pos = pos + 4;
62 cursor.updatePos(pos);
63 if (cursor.atEnd()) {
64 throw new ParseException("Invalid TLS version", buffer, lowerBound, upperBound, pos);
65 }
66 final String s = this.tokenizer.parseToken(buffer, cursor, delimiters);
67 final int idx = s.indexOf('.');
68 if (idx == -1) {
69 final int major;
70 try {
71 major = Integer.parseInt(s);
72 } catch (final NumberFormatException e) {
73 throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
74 }
75 return new ProtocolVersion("TLS", major, 0);
76 } else {
77 final String s1 = s.substring(0, idx);
78 final int major;
79 try {
80 major = Integer.parseInt(s1);
81 } catch (final NumberFormatException e) {
82 throw new ParseException("Invalid TLS major version", buffer, lowerBound, upperBound, pos);
83 }
84 final String s2 = s.substring(idx + 1);
85 final int minor;
86 try {
87 minor = Integer.parseInt(s2);
88 } catch (final NumberFormatException e) {
89 throw new ParseException("Invalid TLS minor version", buffer, lowerBound, upperBound, pos);
90 }
91 return new ProtocolVersion("TLS", major, minor);
92 }
93 }
94
95 ProtocolVersion parse(final String s) throws ParseException {
96 if (s == null) {
97 return null;
98 }
99 final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
100 return parse(s, cursor, null);
101 }
102
103 }
104