Skip to content

Commit d90d908

Browse files
committed
Add push functions for PSL loading from memory
1 parent 79f92c6 commit d90d908

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/upa_url_bind.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ inline nb::str to_str(std::string_view sv) {
3333
return nb::str{ sv.data(), sv.length() };
3434
}
3535

36+
class public_suffix_list_py : public upa::public_suffix_list {
37+
public:
38+
inline void push_line(std::string_view line) {
39+
upa::public_suffix_list::push_line(ctx_, line);
40+
}
41+
inline void push(std::string_view buff) {
42+
upa::public_suffix_list::push(ctx_, buff);
43+
}
44+
inline bool finalize() {
45+
return upa::public_suffix_list::finalize(ctx_);
46+
}
47+
private:
48+
push_context ctx_;
49+
};
50+
3651
} // namespace
3752

3853
NB_MODULE(upa_url, m) {
@@ -181,29 +196,36 @@ NB_MODULE(upa_url, m) {
181196
;
182197

183198
// PSL class
184-
nb::class_<upa::public_suffix_list>(m, "PSL")
199+
nb::class_<public_suffix_list_py>(m, "PSL")
200+
// Load Public Suffix List using push interface
201+
.def(nb::init<>())
202+
.def("push_line", &public_suffix_list_py::push_line, nb::arg("line"))
203+
.def("push", &public_suffix_list_py::push, nb::arg("buff"))
204+
.def("finalize", &public_suffix_list_py::finalize)
205+
185206
// Load Public Suffix List from file
186207
.def_static("load", [](std::string_view filename)
187-
-> std::optional<upa::public_suffix_list> {
188-
upa::public_suffix_list psl;
208+
-> std::optional<public_suffix_list_py> {
209+
public_suffix_list_py psl;
189210
if (psl.load(filename))
190211
return psl;
191212
return std::nullopt;
192213
}, nb::arg("filename"))
214+
193215
// Get public suffix
194-
.def("public_suffix", [](const upa::public_suffix_list& self,
216+
.def("public_suffix", [](const public_suffix_list_py& self,
195217
std::string_view str_host, bool ascii) {
196218
if (ascii) {
197219
return to_str(self.get_suffix(str_host));
198220
}
199221
return to_str(self.get_suffix_view(str_host));
200222
}, nb::arg("host"), nb::arg("ascii") = true)
201-
.def("public_suffix", [](const upa::public_suffix_list& self,
223+
.def("public_suffix", [](const public_suffix_list_py& self,
202224
const upa::url& url) {
203225
return self.get_suffix_view(url);
204226
}, nb::arg("url"))
205227
// Get registrable domain
206-
.def("registrable_domain", [](const upa::public_suffix_list& self,
228+
.def("registrable_domain", [](const public_suffix_list_py& self,
207229
std::string_view str_host, bool ascii) {
208230
if (ascii) {
209231
return to_str(self.get_suffix(str_host,
@@ -212,7 +234,7 @@ NB_MODULE(upa_url, m) {
212234
return to_str(self.get_suffix_view(str_host,
213235
upa::public_suffix_list::option::registrable_domain));
214236
}, nb::arg("host"), nb::arg("ascii") = true)
215-
.def("registrable_domain", [](const upa::public_suffix_list& self,
237+
.def("registrable_domain", [](const public_suffix_list_py& self,
216238
const upa::url& url) {
217239
return self.get_suffix_view(url,
218240
upa::public_suffix_list::option::registrable_domain);

tests/test_psl.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@
44

55
class TestPSL(unittest.TestCase):
66

7+
def test_psl_push(self):
8+
# push_line
9+
psl = PSL()
10+
psl.push_line('github.io')
11+
self.assertTrue(psl.finalize())
12+
self.assertEqual(psl.public_suffix('upa-url.github.io'), 'github.io')
13+
self.assertEqual(psl.public_suffix('abc.io'), 'io')
14+
15+
# push
16+
psl = PSL()
17+
psl.push('githu')
18+
psl.push('b.io')
19+
self.assertTrue(psl.finalize())
20+
self.assertEqual(psl.public_suffix('upa-url.github.io.'), 'github.io.')
21+
self.assertEqual(psl.public_suffix('abc.io.'), 'io.')
22+
23+
# push error
24+
psl = PSL()
25+
psl.push('^.com')
26+
self.assertFalse(psl.finalize())
27+
728
def test_psl(self):
829
# Load list
930
dir = os.path.dirname(os.path.realpath(__file__))

0 commit comments

Comments
 (0)