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
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! A native Rust library for Mozilla's Public Suffix List

#![cfg_attr(not(any(feature = "punycode", feature = "std")), no_std)]
#![forbid(unsafe_code)]

extern crate alloc;

mod error;
mod fxhash;

use crate::alloc::borrow::ToOwned;
#[cfg(feature = "anycase")]
use alloc::string::String;
#[cfg(not(feature = "anycase"))]
use alloc::vec::Vec;
#[cfg(feature = "anycase")]
use core::str;
use core::str::{from_utf8, FromStr};
use fxhash::FxBuildHasher;
use hashbrown::HashMap;
#[cfg(feature = "anycase")]
use unicase::UniCase;

pub use error::Error;
pub use psl_types::{Domain, Info, List as Psl, Suffix, Type};

/// The official URL of the list
pub const LIST_URL: &str = "https://publicsuffix.org/list/public_suffix_list.dat";

#[cfg(not(feature = "anycase"))]
type Children = HashMap<Vec<u8>, Node, FxBuildHasher>;

#[cfg(feature = "anycase")]
type Children = HashMap<UniCase<String>, Node, FxBuildHasher>;

const WILDCARD: &str = "*";

#[derive(Debug, Clone, Default, Eq, PartialEq)]
struct Node {
    children: Children,
    leaf: Option<Leaf>,
}

#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
struct Leaf {
    is_exception: bool,
    typ: Option<Type>,
}

/// A dynamic public suffix list
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct List(Node);

impl List {
    /// Creates a new list with default wildcard rule support
    pub fn new() -> Self {
        let mut children = Children::default();
        children.insert(
            #[cfg(not(feature = "anycase"))]
            WILDCARD.as_bytes().to_vec(),
            #[cfg(feature = "anycase")]
            UniCase::new(WILDCARD.to_owned()),
            Node {
                leaf: Some(Default::default()),
                ..Default::default()
            },
        );
        Self(Node {
            children,
            ..Default::default()
        })
    }

    /// Creates a new list from a byte slice
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
        from_utf8(bytes)
            .map_err(|_| Error::ListNotUtf8Encoded)?
            .parse()
    }

    /// Checks to see if the list is empty, ignoring the wildcard rule
    pub fn is_empty(&self) -> bool {
        self.0.children.len() < 2
    }

    fn append(&mut self, mut rule: &str, typ: Type) -> Result<(), Error> {
        let mut is_exception = false;
        if rule.starts_with('!') {
            if !rule.contains('.') {
                return Err(Error::ExceptionAtFirstLabel(rule.to_owned()));
            }
            is_exception = true;
            rule = &rule[1..];
        }

        let mut current = &mut self.0;
        for label in rule.rsplit('.') {
            if label.is_empty() {
                return Err(Error::EmptyLabel(rule.to_owned()));
            }

            let children = &mut current.children;

            #[cfg(not(feature = "anycase"))]
            let entry = children.entry(label.as_bytes().to_vec());

            #[cfg(feature = "anycase")]
            let entry = children.entry(UniCase::new(label.to_owned()));

            current = entry.or_insert_with(Default::default);
        }

        current.leaf = Some(Leaf {
            is_exception,
            typ: Some(typ),
        });

        Ok(())
    }
}

impl Psl for List {
    #[inline]
    fn find<'a, T>(&self, labels: T) -> Info
    where
        T: Iterator<Item = &'a [u8]>,
    {
        let mut info = Info { len: 0, typ: None };
        let mut len = 0;

        let mut current = &self.0;
        for label in labels {
            let is_first_label = len == 0;
            #[cfg(not(feature = "anycase"))]
            let node_opt = current.children.get(label);
            #[cfg(feature = "anycase")]
            let node_opt = match str::from_utf8(label) {
                Ok(label) => current.children.get(&UniCase::new(label.to_owned())),
                Err(_) => return Info { len: 0, typ: None },
            };
            match node_opt {
                Some(node) => {
                    current = node;
                }
                None => {
                    #[cfg(not(feature = "anycase"))]
                    let node_opt = current.children.get(WILDCARD.as_bytes());
                    #[cfg(feature = "anycase")]
                    let node_opt = current.children.get(&UniCase::new(WILDCARD.to_owned()));
                    match node_opt {
                        Some(node) => {
                            current = node;
                        }
                        None => break,
                    }
                }
            }
            let label_len = label.len();
            len += if is_first_label {
                label_len
            } else {
                1 + label_len
            };
            if let Some(leaf) = current.leaf {
                if leaf.is_exception {
                    len -= 1 + label_len;
                }
                info.len = len;
                info.typ = leaf.typ;
            } else if is_first_label {
                info.len = len;
            }
        }

        info
    }
}

impl FromStr for List {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut typ = None;
        let mut list = List::new();
        for line in s.lines() {
            match line {
                line if line.contains("BEGIN ICANN DOMAINS") => {
                    typ = Some(Type::Icann);
                }
                line if line.contains("BEGIN PRIVATE DOMAINS") => {
                    typ = Some(Type::Private);
                }
                line if line.starts_with("//") => {
                    continue;
                }
                line => match typ {
                    Some(typ) => {
                        let rule = match line.split_whitespace().next() {
                            Some(rule) => rule,
                            None => continue,
                        };
                        list.append(rule, typ)?;
                        #[cfg(feature = "punycode")]
                        {
                            let ascii = idna::domain_to_ascii(rule)
                                .map_err(|_| Error::InvalidRule(rule.to_owned()))?;
                            list.append(&ascii, typ)?;
                        }
                    }
                    None => {
                        continue;
                    }
                },
            }
        }
        Ok(list)
    }
}

impl Default for List {
    fn default() -> Self {
        Self::new()
    }
}