]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAtomStream.pm
wwwatomstream: support async blob fetch
[public-inbox.git] / lib / PublicInbox / WwwAtomStream.pm
1 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Atom body stream for which yields getline+close methods
5 # public-inbox-httpd favors "getline" response bodies to take a
6 # "pull"-based approach to feeding slow clients (as opposed to a
7 # more common "push" model)
8 package PublicInbox::WwwAtomStream;
9 use strict;
10 use warnings;
11
12 use POSIX qw(strftime);
13 use Digest::SHA qw(sha1_hex);
14 use PublicInbox::Address;
15 use PublicInbox::Hval qw(ascii_html mid_href);
16 use PublicInbox::MsgTime qw(msg_timestamp);
17 use PublicInbox::GzipFilter qw(gzf_maybe);
18 use PublicInbox::GitAsyncCat;
19
20 # called by generic PSGI server after getline,
21 # and also by PublicInbox::HTTP::close
22 sub close { !!delete($_[0]->{http_out}) }
23
24 sub new {
25         my ($class, $ctx, $cb) = @_;
26         $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
27         $ctx->{cb} = $cb || \&close;
28         $ctx->{emit_header} = 1;
29         bless $ctx, $class;
30 }
31
32 # called by PublicInbox::DS::write
33 sub atom_async_next {
34         my ($http) = @_; # PublicInbox::HTTP
35         atom_async_step($http->{forward});
36 }
37
38 # this is public-inbox-httpd-specific
39 sub atom_blob_cb { # git->cat_async callback
40         my ($bref, $oid, $type, $size, $ctx) = @_;
41         my $http = $ctx->{env}->{'psgix.io'} or return; # client abort
42         my $smsg = delete $ctx->{smsg} or die 'BUG: no smsg';
43         if (!defined($oid)) {
44                 # it's possible to have TOCTOU if an admin runs
45                 # public-inbox-(edit|purge), just move onto the next message
46                 return $http->next_step(\&atom_async_next);
47         } else {
48                 $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
49         }
50         my $buf = feed_entry($ctx, $smsg, PublicInbox::Eml->new($bref));
51         if (my $gzf = $ctx->{gzf}) {
52                 $buf = $gzf->translate($buf);
53         }
54         # PublicInbox::HTTP::{Chunked,Identity}::write
55         $ctx->{http_out}->write($buf);
56
57         $http->next_step(\&atom_async_next);
58 }
59
60 sub atom_async_step { # this is public-inbox-httpd-specific
61         my ($ctx) = @_;
62         if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) {
63                 git_async_cat($ctx->{-inbox}->git, $smsg->{blob},
64                                 \&atom_blob_cb, $ctx);
65         } elsif (my $out = delete $ctx->{http_out}) {
66                 if (my $gzf = delete $ctx->{gzf}) {
67                         $out->write($gzf->zflush);
68                 }
69                 $out->close;
70         }
71 }
72
73 sub response {
74         my ($class, $ctx, $code, $cb) = @_;
75         my $res_hdr = [ 'Content-Type' => 'application/atom+xml' ];
76         $class->new($ctx, $cb);
77         $ctx->{gzf} = gzf_maybe($res_hdr, $ctx->{env});
78         if ($ctx->{env}->{'pi-httpd.async'}) {
79                 sub {
80                         my ($wcb) = @_; # -httpd provided write callback
81                         $ctx->{http_out} = $wcb->([200, $res_hdr]);
82                         $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
83                         atom_async_step($ctx); # start stepping
84                 };
85         } else {
86                 [ $code, $res_hdr, $ctx ];
87         }
88 }
89
90 # called once for each message by PSGI server
91 sub getline {
92         my ($self) = @_;
93         my $buf = do {
94                 if (my $middle = $self->{cb}) {
95                         if (my $smsg = $middle->($self)) {
96                                 my $eml = $self->{-inbox}->smsg_eml($smsg) or
97                                                 return '';
98                                 feed_entry($self, $smsg, $eml);
99                         } else {
100                                 undef;
101                         }
102                 }
103         } // (delete($self->{cb}) ? '</feed>' : undef);
104
105         # gzf may be GzipFilter, `undef' or `0'
106         my $gzf = $self->{gzf} or return $buf;
107
108         return $gzf->translate($buf) if defined $buf;
109         $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
110         $gzf->translate(undef);
111 }
112
113 # private
114
115 sub title_tag {
116         my ($title) = @_;
117         $title =~ tr/\t\n / /s; # squeeze spaces
118         # try to avoid the type attribute in title:
119         $title = ascii_html($title);
120         my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
121         "<title$type>$title</title>";
122 }
123
124 sub to_uuid ($) {
125         my ($any) = @_;
126         utf8::encode($any); # really screwed up In-Reply-To fields exist
127         $any = sha1_hex($any);
128         my $h = '[a-f0-9]';
129         my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
130         'urn:uuid:' . join('-', @uuid5);
131 }
132
133 sub atom_header {
134         my ($ctx, $title) = @_;
135         my $ibx = $ctx->{-inbox};
136         my $base_url = $ctx->{feed_base_url};
137         my $search_q = $ctx->{search_query};
138         my $self_url = $base_url;
139         my $mid = $ctx->{mid};
140         my $page_id;
141         if (defined $mid) { # per-thread
142                 $self_url .= mid_href($mid).'/t.atom';
143                 $page_id = to_uuid("t\n".$mid)
144         } elsif (defined $search_q) {
145                 my $query = $search_q->{'q'};
146                 $title = title_tag("$query - search results");
147                 $base_url .= '?' . $search_q->qs_html(x => undef);
148                 $self_url .= '?' . $search_q->qs_html;
149                 $page_id = to_uuid("q\n".$query);
150         } else {
151                 $title = title_tag($ibx->description);
152                 $self_url .= 'new.atom';
153                 $page_id = "mailto:$ibx->{-primary_address}";
154         }
155         qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
156         qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
157         qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
158         qq{$title} .
159         qq(<link\nrel="alternate"\ntype="text/html") .
160                 qq(\nhref="$base_url"/>) .
161         qq(<link\nrel="self"\nhref="$self_url"/>) .
162         qq(<id>$page_id</id>) .
163         feed_updated($ibx->modified);
164 }
165
166 # returns undef or string
167 sub feed_entry {
168         my ($ctx, $smsg, $eml) = @_;
169         my $hdr = $eml->header_obj;
170         my $mid = $smsg->{mid};
171         my $irt = PublicInbox::View::in_reply_to($hdr);
172         my $uuid = to_uuid($mid);
173         my $base = $ctx->{feed_base_url};
174         if (defined $irt) {
175                 my $irt_uuid = to_uuid($irt);
176                 $irt = mid_href($irt);
177                 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
178                         qq(href="$base$irt/"/>);
179         } else {
180                 $irt = '';
181         }
182         my $href = $base . mid_href($mid) . '/';
183         my $updated = feed_updated(msg_timestamp($hdr));
184
185         my $title = $hdr->header('Subject');
186         $title = '(no subject)' unless defined $title && $title ne '';
187         $title = title_tag($title);
188
189         my $from = $hdr->header('From') or return;
190         my ($email) = PublicInbox::Address::emails($from);
191         my $name = join(', ',PublicInbox::Address::names($from));
192         $name = ascii_html($name);
193         $email = ascii_html($email);
194
195         my $s = '';
196         if (delete $ctx->{emit_header}) {
197                 $s .= atom_header($ctx, $title);
198         }
199         $s .= "<entry><author><name>$name</name><email>$email</email>" .
200                 "</author>$title$updated" .
201                 qq(<link\nhref="$href"/>).
202                 "<id>$uuid</id>$irt" .
203                 qq{<content\ntype="xhtml">} .
204                 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
205                 qq(<pre\nstyle="white-space:pre-wrap">);
206         $ctx->{obuf} = \$s;
207         $ctx->{mhref} = $href;
208         PublicInbox::View::multipart_text_as_html($eml, $ctx);
209         delete $ctx->{obuf};
210         $s .= '</pre></div></content></entry>';
211 }
212
213 sub feed_updated {
214         '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(@_)) . '</updated>';
215 }
216
217 1;