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  package org.apache.hc.client5.http.psl;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.Reader;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import org.apache.hc.core5.annotation.Contract;
36  import org.apache.hc.core5.annotation.ThreadingBehavior;
37  
38  /**
39   * Parses the list from <a href="http://publicsuffix.org/">publicsuffix.org</a>
40   * and configures a PublicSuffixFilter.
41   *
42   * @since 4.4
43   */
44  @Contract(threading = ThreadingBehavior.STATELESS)
45  public final class PublicSuffixListParser {
46  
47      public PublicSuffixListParser() {
48      }
49  
50      /**
51       * Parses the public suffix list format.
52       * <p>
53       * When creating the reader from the file, make sure to use the correct encoding
54       * (the original list is in UTF-8).
55       *
56       * @param reader the data reader. The caller is responsible for closing the reader.
57       * @throws java.io.IOException on error while reading from list
58       */
59      public PublicSuffixList parse(final Reader reader) throws IOException {
60          final List<String> rules = new ArrayList<>();
61          final List<String> exceptions = new ArrayList<>();
62          final BufferedReader r = new BufferedReader(reader);
63  
64          String line;
65          while ((line = r.readLine()) != null) {
66              if (line.isEmpty()) {
67                  continue;
68              }
69              if (line.startsWith("//")) {
70                  continue; //entire lines can also be commented using //
71              }
72              if (line.startsWith(".")) {
73                  line = line.substring(1); // A leading dot is optional
74              }
75              // An exclamation mark (!) at the start of a rule marks an exception to a previous wildcard rule
76              final boolean isException = line.startsWith("!");
77              if (isException) {
78                  line = line.substring(1);
79              }
80  
81              if (isException) {
82                  exceptions.add(line);
83              } else {
84                  rules.add(line);
85              }
86          }
87          return new PublicSuffixList(DomainType.UNKNOWN, rules, exceptions);
88      }
89  
90      /**
91       * Parses the public suffix list format by domain type (currently supported ICANN and PRIVATE).
92       * <p>
93       * When creating the reader from the file, make sure to use the correct encoding
94       * (the original list is in UTF-8).
95       *
96       * @param reader the data reader. The caller is responsible for closing the reader.
97       * @throws java.io.IOException on error while reading from list
98       *
99       * @since 4.5
100      */
101     public List<PublicSuffixList> parseByType(final Reader reader) throws IOException {
102         final List<PublicSuffixList> result = new ArrayList<>(2);
103 
104         final BufferedReader r = new BufferedReader(reader);
105 
106         DomainType domainType = null;
107         List<String> rules = null;
108         List<String> exceptions = null;
109         String line;
110         while ((line = r.readLine()) != null) {
111             if (line.isEmpty()) {
112                 continue;
113             }
114             if (line.startsWith("//")) {
115 
116                 if (domainType == null) {
117                     if (line.contains("===BEGIN ICANN DOMAINS===")) {
118                         domainType = DomainType.ICANN;
119                     } else if (line.contains("===BEGIN PRIVATE DOMAINS===")) {
120                         domainType = DomainType.PRIVATE;
121                     }
122                 } else {
123                     if (line.contains("===END ICANN DOMAINS===") || line.contains("===END PRIVATE DOMAINS===")) {
124                         if (rules != null) {
125                             result.add(new PublicSuffixList(domainType, rules, exceptions));
126                         }
127                         domainType = null;
128                         rules = null;
129                         exceptions = null;
130                     }
131                 }
132 
133                 continue; //entire lines can also be commented using //
134             }
135             if (domainType == null) {
136                 continue;
137             }
138 
139             if (line.startsWith(".")) {
140                 line = line.substring(1); // A leading dot is optional
141             }
142             // An exclamation mark (!) at the start of a rule marks an exception to a previous wildcard rule
143             final boolean isException = line.startsWith("!");
144             if (isException) {
145                 line = line.substring(1);
146             }
147 
148             if (isException) {
149                 if (exceptions == null) {
150                     exceptions = new ArrayList<>();
151                 }
152                 exceptions.add(line);
153             } else {
154                 if (rules == null) {
155                     rules = new ArrayList<>();
156                 }
157                 rules.add(line);
158             }
159         }
160         return result;
161     }
162 
163 }