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