]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WwwListing.pm
wwwlisting: avoid lazy loading JSON module
[public-inbox.git] / lib / PublicInbox / WwwListing.pm
index a52dba110c359fa988e4781657d2be81d7fe6d0c..33cb0ace075016638227cbcce55e659d033afe3f 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2019 all contributors <meta@public-inbox.org>
+# Copyright (C) 2019-2020 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # Provide an HTTP-accessible listing of inboxes.
@@ -10,11 +10,19 @@ use PublicInbox::Hval qw(ascii_html prurl);
 use PublicInbox::Linkify;
 use PublicInbox::View;
 use PublicInbox::Inbox;
-use bytes ();
+use bytes (); # bytes::length
 use HTTP::Date qw(time2str);
-require Digest::SHA;
-require File::Spec;
+use Digest::SHA ();
+use File::Spec ();
 *try_cat = \&PublicInbox::Inbox::try_cat;
+our $json;
+if (eval { require IO::Compress::Gzip }) {
+       for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
+               eval "require $mod" or next;
+               # ->ascii encodes non-ASCII to "\uXXXX"
+               $json = $mod->new->ascii(1);
+       }
+}
 
 sub list_all_i {
        my ($ibx, $arg) = @_;
@@ -111,8 +119,7 @@ sub html ($$) {
 
                my $tmp = join("\n", map { ibx_entry(@$_, $env) } @$list);
                my $l = PublicInbox::Linkify->new;
-               $l->linkify_1($tmp);
-               $out = '<pre>'.$l->linkify_2(ascii_html($tmp)).'</pre><hr>';
+               $out = '<pre>'.$l->to_html($tmp).'</pre><hr>';
        }
        $out = "<html><head><title>$title</title></head><body>" . $out;
        $out .= '<pre>'. PublicInbox::WwwStream::code_footer($env) .
@@ -122,23 +129,11 @@ sub html ($$) {
        [ $code, $h, [ $out ] ];
 }
 
-my $json;
-sub _json () {
-       for my $mod (qw(JSON::MaybeXS JSON JSON::PP)) {
-               eval "require $mod" or next;
-               # ->ascii encodes non-ASCII to "\uXXXX"
-               return $mod->new->ascii(1);
-       }
-       die;
-}
-
 sub fingerprint ($) {
        my ($git) = @_;
        # TODO: convert to qspawn for fairness when there's
        # thousands of repos
-       my ($fh, $pid) = $git->popen('show-ref') or
-               die "popen($git->{git_dir} show-ref) failed: $!";
-
+       my ($fh, $pid) = $git->popen('show-ref');
        my $dig = Digest::SHA->new(1);
        while (read($fh, my $buf, 65536)) {
                $dig->add($buf);
@@ -204,7 +199,8 @@ sub manifest_add ($$;$$) {
 # manifest.js.gz
 sub js ($$) {
        my ($env, $list) = @_;
-       eval { require IO::Compress::Gzip } or return [ 404, [], [] ];
+       # $json won't be defined if IO::Compress::Gzip is missing
+       $json or return [ 404, [], [] ];
 
        my $manifest = { -abs2urlpath => {}, -mtime => 0 };
        for my $ibx (@$list) {
@@ -224,8 +220,7 @@ sub js ($$) {
                $repo->{reference} = $abs2urlpath->{$abs};
        }
        my $out;
-       IO::Compress::Gzip::gzip(\(($json ||= _json())->encode($manifest)) =>
-                                \$out);
+       IO::Compress::Gzip::gzip(\($json->encode($manifest)) => \$out);
        $manifest = undef;
        [ 200, [ qw(Content-Type application/gzip),
                 'Last-Modified', time2str($mtime),