]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
get rid of unnecessary bytes::length usage
[public-inbox.git] / lib / PublicInbox / ManifestJsGz.pm
1 # Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # generates manifest.js.gz for grokmirror(1)
5 package PublicInbox::ManifestJsGz;
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::WwwListing);
9 use PublicInbox::Config;
10 use IO::Compress::Gzip qw(gzip);
11 use HTTP::Date qw(time2str);
12
13 my $json = PublicInbox::Config::json();
14
15 sub url_filter {
16         my ($ctx) = @_;
17         # grokmirror uses relative paths, so it's domain-dependent
18         # SUPER calls PublicInbox::WwwListing::url_filter
19         $ctx->SUPER::url_filter('publicInbox.grokManifest', 'match=domain');
20 }
21
22 sub inject_entry ($$$;$) {
23         my ($ctx, $url_path, $ent, $git_dir) = @_;
24         $ctx->{-abs2urlpath}->{$git_dir // delete $ent->{git_dir}} = $url_path;
25         my $modified = $ent->{modified};
26         $ctx->{-mtime} = $modified if $modified > ($ctx->{-mtime} // 0);
27         $ctx->{manifest}->{$url_path} = $ent;
28 }
29
30 sub manifest_add ($$;$$) { # slow path w/o extindex "all"
31         my ($ctx, $ibx, $epoch, $default_desc) = @_;
32         my $url_path = "/$ibx->{name}";
33         my $git;
34         if (defined $epoch) {
35                 $url_path .= "/git/$epoch.git";
36                 $git = $ibx->git_epoch($epoch) or return;
37         } else {
38                 $git = $ibx->git;
39         }
40         my $ent = $git->manifest_entry($epoch, $default_desc) or return;
41         inject_entry($ctx, $url_path, $ent, $git->{git_dir});
42 }
43
44 sub slow_manifest_add ($$) {
45         my ($ctx, $ibx) = @_;
46         eval {
47                 if (defined(my $max = $ibx->max_git_epoch)) {
48                         my $desc = $ibx->description;
49                         for my $epoch (0..$max) {
50                                 manifest_add($ctx, $ibx, $epoch, $desc);
51                         }
52                 } else {
53                         manifest_add($ctx, $ibx);
54                 }
55         };
56 }
57
58 sub eidx_manifest_add ($$$) {
59         my ($ctx, $ALL, $ibx) = @_;
60         if (my $data = $ALL->misc->inbox_data($ibx)) {
61                 $data = $json->decode($data);
62                 delete $data->{''}; # private
63                 while (my ($url_path, $ent) = each %$data) {
64                         inject_entry($ctx, $url_path, $ent);
65                 }
66         } else {
67                 warn "E: `${\$ibx->eidx_key}' not indexed by $ALL->{topdir}\n";
68         }
69 }
70
71 sub response {
72         my ($class, $ctx) = @_;
73         bless $ctx, $class;
74         my ($re, undef) = $ctx->url_filter;
75         $re // return psgi_triple($ctx);
76         my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
77                                         $ctx->can('list_match_i'), $re, $ctx);
78         sub {
79                 $ctx->{-wcb} = $_[0]; # HTTP server callback
80                 $ctx->{env}->{'pi-httpd.async'} ?
81                                 $iter->event_step : $iter->each_section;
82         }
83 }
84
85 sub ibx_entry {
86         my ($ctx, $ibx) = @_;
87         my $ALL = $ctx->{www}->{pi_cfg}->ALL;
88         if ($ALL) { # FIXME: test this in t/
89                 eidx_manifest_add($ctx, $ALL, $ibx);
90         } else {
91                 slow_manifest_add($ctx, $ibx);
92                 warn "E: $@" if $@;
93         }
94 }
95
96 sub hide_key { 'manifest' } # for WwwListing->list_match_i
97
98 sub psgi_triple {
99         my ($ctx) = @_;
100         my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
101         my $manifest = delete($ctx->{manifest}) // {};
102         while (my ($url_path, $repo) = each %$manifest) {
103                 defined(my $abs = $repo->{reference}) or next;
104                 $repo->{reference} = $abs2urlpath->{$abs};
105         }
106         $manifest = $json->encode($manifest);
107         gzip(\$manifest => \(my $out));
108         [ 200, [ qw(Content-Type application/gzip),
109                  'Last-Modified', time2str($ctx->{-mtime}),
110                  'Content-Length', length($out) ], [ $out ] ]
111 }
112
113 sub per_inbox {
114         my ($ctx) = @_;
115         # only one inbox, slow is probably OK
116         slow_manifest_add($ctx, $ctx->{ibx});
117         psgi_triple($ctx);
118 }
119
120 1;