]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ManifestJsGz.pm
imap+nntp: share COMPRESS implementation
[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" (or per-inbox)
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         warn "E: $@" if $@;
57 }
58
59 sub eidx_manifest_add ($$$) {
60         my ($ctx, $ALL, $ibx) = @_;
61         if (my $data = $ALL->misc->inbox_data($ibx)) {
62                 $data = $json->decode($data);
63                 delete $data->{''}; # private
64                 while (my ($url_path, $ent) = each %$data) {
65                         inject_entry($ctx, $url_path, $ent);
66                 }
67         } else {
68                 warn "E: `${\$ibx->eidx_key}' not indexed by $ALL->{topdir}\n";
69                 # do not use slow path for global manifest since
70                 # it can become catastrophically slow.  per-inbox manifest
71                 # is not too bad with dozens of epochs, so never fail that:
72                 slow_manifest_add($ctx, $ibx) if $ibx == $ctx->{ibx};
73         }
74 }
75
76 sub response {
77         my ($class, $ctx) = @_;
78         bless $ctx, $class;
79         my ($re, undef) = $ctx->url_filter;
80         $re // return psgi_triple($ctx);
81         my $iter = PublicInbox::ConfigIter->new($ctx->{www}->{pi_cfg},
82                                         $ctx->can('list_match_i'), $re, $ctx);
83         sub {
84                 $ctx->{-wcb} = $_[0]; # HTTP server callback
85                 $ctx->{env}->{'pi-httpd.async'} ?
86                                 $iter->event_step : $iter->each_section;
87         }
88 }
89
90 sub ibx_entry {
91         my ($ctx, $ibx) = @_;
92         my $ALL = $ctx->{www}->{pi_cfg}->ALL;
93         $ALL ? eidx_manifest_add($ctx, $ALL, $ibx) :
94                 slow_manifest_add($ctx, $ibx);
95 }
96
97 sub hide_key { 'manifest' } # for WwwListing->list_match_i
98
99 sub psgi_triple {
100         my ($ctx) = @_;
101         my $abs2urlpath = delete($ctx->{-abs2urlpath}) // {};
102         my $manifest = delete($ctx->{manifest}) // {};
103         while (my ($url_path, $repo) = each %$manifest) {
104                 defined(my $abs = $repo->{reference}) or next;
105                 $repo->{reference} = $abs2urlpath->{$abs};
106         }
107         $manifest = $json->encode($manifest);
108         gzip(\$manifest => \(my $out));
109         [ 200, [ qw(Content-Type application/gzip),
110                  'Last-Modified', time2str($ctx->{-mtime}),
111                  'Content-Length', length($out) ], [ $out ] ]
112 }
113
114 sub per_inbox {
115         my ($ctx) = @_;
116         ibx_entry($ctx, $ctx->{ibx});
117         psgi_triple($ctx);
118 }
119
120 1;