View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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