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.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