]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAtomStream.pm
www*stream: gzip ->getline responses
[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
19 # called by PSGI server after getline:
20 sub close {}
21
22 sub new {
23         my ($class, $ctx, $cb) = @_;
24         $ctx->{feed_base_url} = $ctx->{-inbox}->base_url($ctx->{env});
25         bless { cb => $cb || \&close, ctx => $ctx, emit_header => 1 }, $class;
26 }
27
28 sub response {
29         my ($class, $ctx, $code, $cb) = @_;
30         my $h = [ 'Content-Type' => 'application/atom+xml' ];
31         my $self = $class->new($ctx, $cb);
32         $self->{gzf} = gzf_maybe($h, $ctx->{env});
33         [ $code, $h, $self ]
34 }
35
36 # called once for each message by PSGI server
37 sub getline {
38         my ($self) = @_;
39         my $buf = do {
40                 if (my $middle = $self->{cb}) {
41                         my $smsg = $middle->($self->{ctx});
42                         feed_entry($self, $smsg) if $smsg;
43                 }
44         } // (delete($self->{cb}) ? '</feed>' : undef);
45
46         # gzf may be GzipFilter, `undef' or `0'
47         my $gzf = $self->{gzf} or return $buf;
48
49         return $gzf->translate($buf) if defined $buf;
50         $self->{gzf} = 0; # next call to ->getline returns $buf (== undef)
51         $gzf->translate(undef);
52 }
53
54 # private
55
56 sub title_tag {
57         my ($title) = @_;
58         $title =~ tr/\t\n / /s; # squeeze spaces
59         # try to avoid the type attribute in title:
60         $title = ascii_html($title);
61         my $type = index($title, '&') >= 0 ? "\ntype=\"html\"" : '';
62         "<title$type>$title</title>";
63 }
64
65 sub to_uuid ($) {
66         my ($any) = @_;
67         utf8::encode($any); # really screwed up In-Reply-To fields exist
68         $any = sha1_hex($any);
69         my $h = '[a-f0-9]';
70         my (@uuid5) = ($any =~ m!\A($h{8})($h{4})($h{4})($h{4})($h{12})!o);
71         'urn:uuid:' . join('-', @uuid5);
72 }
73
74 sub atom_header {
75         my ($ctx, $title) = @_;
76         my $ibx = $ctx->{-inbox};
77         my $base_url = $ctx->{feed_base_url};
78         my $search_q = $ctx->{search_query};
79         my $self_url = $base_url;
80         my $mid = $ctx->{mid};
81         my $page_id;
82         if (defined $mid) { # per-thread
83                 $self_url .= mid_href($mid).'/t.atom';
84                 $page_id = to_uuid("t\n".$mid)
85         } elsif (defined $search_q) {
86                 my $query = $search_q->{'q'};
87                 $title = title_tag("$query - search results");
88                 $base_url .= '?' . $search_q->qs_html(x => undef);
89                 $self_url .= '?' . $search_q->qs_html;
90                 $page_id = to_uuid("q\n".$query);
91         } else {
92                 $title = title_tag($ibx->description);
93                 $self_url .= 'new.atom';
94                 $page_id = "mailto:$ibx->{-primary_address}";
95         }
96         my $mtime = (stat($ibx->{inboxdir}))[9] || time;
97
98         qq(<?xml version="1.0" encoding="us-ascii"?>\n) .
99         qq(<feed\nxmlns="http://www.w3.org/2005/Atom"\n) .
100         qq(xmlns:thr="http://purl.org/syndication/thread/1.0">) .
101         qq{$title} .
102         qq(<link\nrel="alternate"\ntype="text/html") .
103                 qq(\nhref="$base_url"/>) .
104         qq(<link\nrel="self"\nhref="$self_url"/>) .
105         qq(<id>$page_id</id>) .
106         feed_updated(gmtime($mtime));
107 }
108
109 # returns undef or string
110 sub feed_entry {
111         my ($self, $smsg) = @_;
112         my $ctx = $self->{ctx};
113         my $eml = $ctx->{-inbox}->smsg_eml($smsg) or return '';
114         my $hdr = $eml->header_obj;
115         my $mid = $smsg->{mid};
116         my $irt = PublicInbox::View::in_reply_to($hdr);
117         my $uuid = to_uuid($mid);
118         my $base = $ctx->{feed_base_url};
119         if (defined $irt) {
120                 my $irt_uuid = to_uuid($irt);
121                 $irt = mid_href($irt);
122                 $irt = qq(<thr:in-reply-to\nref="$irt_uuid"\n).
123                         qq(href="$base$irt/"/>);
124         } else {
125                 $irt = '';
126         }
127         my $href = $base . mid_href($mid) . '/';
128         my $t = msg_timestamp($hdr);
129         my @t = gmtime(defined $t ? $t : time);
130         my $updated = feed_updated(@t);
131
132         my $title = $hdr->header('Subject');
133         $title = '(no subject)' unless defined $title && $title ne '';
134         $title = title_tag($title);
135
136         my $from = $hdr->header('From') or return;
137         my ($email) = PublicInbox::Address::emails($from);
138         my $name = join(', ',PublicInbox::Address::names($from));
139         $name = ascii_html($name);
140         $email = ascii_html($email);
141
142         my $s = '';
143         if (delete $self->{emit_header}) {
144                 $s .= atom_header($ctx, $title);
145         }
146         $s .= "<entry><author><name>$name</name><email>$email</email>" .
147                 "</author>$title$updated" .
148                 qq(<link\nhref="$href"/>).
149                 "<id>$uuid</id>$irt" .
150                 qq{<content\ntype="xhtml">} .
151                 qq{<div\nxmlns="http://www.w3.org/1999/xhtml">} .
152                 qq(<pre\nstyle="white-space:pre-wrap">);
153         $ctx->{obuf} = \$s;
154         $ctx->{mhref} = $href;
155         PublicInbox::View::multipart_text_as_html($eml, $ctx);
156         delete $ctx->{obuf};
157         $s .= '</pre></div></content></entry>';
158 }
159
160 sub feed_updated {
161         '<updated>' . strftime('%Y-%m-%dT%H:%M:%SZ', @_) . '</updated>';
162 }
163
164 1;