]> Sergey Matveev's repositories - public-inbox.git/commitdiff
add PublicInbox::URInntps package
authorEric Wong <e@80x24.org>
Wed, 24 Feb 2021 11:31:51 +0000 (17:31 +0600)
committerEric Wong <e@80x24.org>
Wed, 24 Feb 2021 23:26:12 +0000 (23:26 +0000)
We prefer the IANA-registered form of URIs to avoid confusing
users, but the URI package has yet to support it.

cf. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=983419

MANIFEST
lib/PublicInbox/URInntps.pm [new file with mode: 0644]
t/uri_nntps.t [new file with mode: 0644]

index 21e37678292496dfd9a481848ba05878c99a5b5b..9cf97563d0f119d592563f662a1808cc1192324d 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -243,6 +243,7 @@ lib/PublicInbox/TLS.pm
 lib/PublicInbox/TestCommon.pm
 lib/PublicInbox/Tmpfile.pm
 lib/PublicInbox/URIimap.pm
+lib/PublicInbox/URInntps.pm
 lib/PublicInbox/Unsubscribe.pm
 lib/PublicInbox/UserContent.pm
 lib/PublicInbox/V2Writable.pm
@@ -437,6 +438,7 @@ t/thread-cycle.t
 t/thread-index-gap.t
 t/time.t
 t/uri_imap.t
+t/uri_nntps.t
 t/utf8.eml
 t/v1-add-remove-add.t
 t/v1reindex.t
diff --git a/lib/PublicInbox/URInntps.pm b/lib/PublicInbox/URInntps.pm
new file mode 100644 (file)
index 0000000..69fe716
--- /dev/null
@@ -0,0 +1,17 @@
+# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# deal with the lack of URI::nntps in upstream URI.
+# nntps is IANA registered, snews is deprecated
+# cf. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=983419
+package PublicInbox::URInntps;
+use strict;
+use parent qw(URI::snews);
+use URI;
+
+sub new {
+       my ($class, $url) = @_;
+       $url =~ m!\Anntps://!i ? bless(\$url, $class) : URI->new($url);
+}
+
+1;
diff --git a/t/uri_nntps.t b/t/uri_nntps.t
new file mode 100644 (file)
index 0000000..babd808
--- /dev/null
@@ -0,0 +1,40 @@
+#!perl -w
+# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict; use v5.10.1; use PublicInbox::TestCommon;
+require_mods 'URI';
+use_ok 'PublicInbox::URInntps';
+my $uri = PublicInbox::URInntps->new('nntp://EXAMPLE.com/inbox.test');
+isnt(ref($uri), 'PublicInbox::URInntps', 'URI fallback');
+is($uri->scheme, 'nntp', 'NNTP fallback ->scheme');
+
+$uri = PublicInbox::URInntps->new('nntps://EXAMPLE.com/inbox.test');
+is($uri->host, 'EXAMPLE.com', 'host matches');
+is($uri->canonical->host, 'example.com', 'host canonicalized');
+is($uri->canonical->as_string, 'nntps://example.com/inbox.test',
+       'URI canonicalized');
+is($uri->port, 563, 'nntps port');
+is($uri->userinfo, undef, 'no userinfo');
+is($uri->scheme, 'nntps', '->scheme works');
+is($uri->group, 'inbox.test', '->group works');
+
+$uri = PublicInbox::URInntps->new('nntps://foo@0/');
+is("$uri", $uri->as_string, '"" overload works');
+is($uri->host, '0', 'numeric host');
+is($uri->userinfo, 'foo', 'user extracted');
+
+$uri = PublicInbox::URInntps->new('nntps://ipv6@[::1]');
+is($uri->host, '::1', 'IPv6 host');
+is($uri->group, '', '->group is empty');
+
+$uri = PublicInbox::URInntps->new('nntps://0:666/INBOX.test');
+is($uri->port, 666, 'port read');
+is($uri->group, 'INBOX.test', 'group read after port');
+
+is(PublicInbox::URInntps->new('nntps://0:563/')->canonical->as_string,
+       'nntps://0/', 'default port stripped');
+
+$uri = PublicInbox::URInntps->new('nntps://NSA:Hunter2@0/inbox');
+is($uri->userinfo, 'NSA:Hunter2', 'userinfo accepted w/ pass');
+
+done_testing;