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>
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;
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;
20 # called by generic PSGI server after getline,
21 # and also by PublicInbox::HTTP::close
22 sub close { !!delete($_[0]->{http_out}) }
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;
32 # called by PublicInbox::DS::write
34 my ($http) = @_; # PublicInbox::HTTP
35 atom_async_step($http->{forward});
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';
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);
48 $smsg->{blob} eq $oid or die "BUG: $smsg->{blob} != $oid";
50 my $buf = feed_entry($ctx, $smsg, PublicInbox::Eml->new($bref));
51 if (my $gzf = $ctx->{gzf}) {
52 $buf = $gzf->translate($buf);
54 # PublicInbox::HTTP::{Chunked,Identity}::write
55 $ctx->{http_out}->write($buf);
57 $http->next_step(\&atom_async_next);
60 sub atom_async_step { # this is public-inbox-httpd-specific
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);
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'}) {
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
86 [ $code, $res_hdr, $ctx ];
90 # called once for each message by PSGI server
94 if (my $middle = $self->{cb}) {
95 if (my $smsg = $middle->($self)) {
96 my $eml = $self->{-inbox}->smsg_eml($smsg) or
98 feed_entry($self, $smsg, $eml);
103 } // (delete($self->{cb}) ? '</feed>' : undef);
105 # gzf may be GzipFilter, `undef' or `0'
106 my $gzf = $self->{gzf} or return $buf;
108 return $gzf->translate($buf) if defined $buf;
109 $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
110 $gzf->translate(undef);
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>";
126 utf8::encode($any); # really screwed up In-Reply-To fields exist
127 $any = sha1_hex($any);
129 my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
130 'urn:uuid:' . join('-', @uuid5);
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};
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);
151 $title = title_tag($ibx->description);
152 $self_url .= 'new.atom';
153 $page_id = "mailto:$ibx->{-primary_address}";
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">) .
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);
166 # returns undef or string
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};
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/"/>);
182 my $href = $base . mid_href($mid) . '/';
183 my $updated = feed_updated(msg_timestamp($hdr));
185 my $title = $hdr->header('Subject');
186 $title = '(no subject)' unless defined $title && $title ne '';
187 $title = title_tag($title);
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);
196 if (delete $ctx->{emit_header}) {
197 $s .= atom_header($ctx, $title);
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">);
207 $ctx->{mhref} = $href;
208 PublicInbox::View::multipart_text_as_html($eml, $ctx);
210 $s .= '</pre></div></content></entry>';
214 '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', gmtime(@_)) . '</updated>';