]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: comments for denoting Plack::Request vs CGI
[public-inbox.git] / lib / PublicInbox / WWW.pm
1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # Main web interface for mailing list archives
5 #
6 # We focus on the lowest common denominators here:
7 # - targeted at text-only console browsers (w3m, links, etc..)
8 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
9 # - No JavaScript, graphics or icons allowed.
10 # - Must not rely on static content
11 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
12 package PublicInbox::WWW;
13 use 5.008;
14 use strict;
15 use warnings;
16 use PublicInbox::Config qw(try_cat);
17 use URI::Escape qw(uri_escape_utf8 uri_unescape);
18 use constant SSOMA_URL => 'http://ssoma.public-inbox.org/';
19 use constant PI_URL => 'http://public-inbox.org/';
20 require PublicInbox::Git;
21 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
22 our $MID_RE = qr!([^/]+)!;
23 our $END_RE = qr!(f/|T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
24 our $pi_config;
25
26 sub run {
27         my ($cgi, $method) = @_;
28         $pi_config ||= PublicInbox::Config->new;
29         my $ctx = { cgi => $cgi, pi_config => $pi_config };
30         if ($method !~ /\AGET|HEAD\z/) {
31                 return r(405, 'Method Not Allowed');
32         }
33         my $path_info = $cgi->path_info;
34
35         # top-level indices and feeds
36         if ($path_info eq '/') {
37                 r404();
38         } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
39                 invalid_list($ctx, $1) || r301($ctx, $1);
40         } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
41                 invalid_list($ctx, $1) || get_index($ctx);
42         } elsif ($path_info =~ m!$LISTNAME_RE/(?:atom\.xml|new\.atom)\z!o) {
43                 invalid_list($ctx, $1) || get_atom($ctx);
44
45         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/$END_RE\z!o) {
46                 msg_page($ctx, $1, $2, $3);
47
48         # in case people leave off the trailing slash:
49         } elsif ($path_info =~ m!$LISTNAME_RE/$MID_RE/(f|T|t)\z!o) {
50                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
51
52         # convenience redirects order matters
53         } elsif ($path_info =~ m!$LISTNAME_RE/([^/]{2,})\z!o) {
54                 r301($ctx, $1, $2);
55
56         } else {
57                 legacy_redirects($ctx, $path_info);
58         }
59 }
60
61 # for CoW-friendliness, MOOOOO!
62 sub preload {
63         require PublicInbox::Feed;
64         require PublicInbox::View;
65         require PublicInbox::Thread;
66         require Email::MIME;
67         require Digest::SHA;
68         require POSIX;
69
70         eval {
71                 require PublicInbox::Search;
72                 require PublicInbox::SearchView;
73                 require PublicInbox::Mbox;
74                 require IO::Compress::Gzip;
75         };
76 }
77
78 # private functions below
79
80 sub r404 {
81         my ($ctx) = @_;
82         if ($ctx && $ctx->{mid}) {
83                 require PublicInbox::ExtMsg;
84                 searcher($ctx);
85                 return PublicInbox::ExtMsg::ext_msg($ctx);
86         }
87         r(404, 'Not Found');
88 }
89
90 # simple response for errors
91 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
92
93 # returns undef if valid, array ref response if invalid
94 sub invalid_list {
95         my ($ctx, $listname) = @_;
96         my $git_dir = $pi_config->get($listname, "mainrepo");
97         if (defined $git_dir) {
98                 $ctx->{git_dir} = $git_dir;
99                 $ctx->{git} = PublicInbox::Git->new($git_dir);
100                 $ctx->{listname} = $listname;
101                 return;
102         }
103         r404();
104 }
105
106 # returns undef if valid, array ref response if invalid
107 sub invalid_list_mid {
108         my ($ctx, $listname, $mid) = @_;
109         my $ret = invalid_list($ctx, $listname, $mid);
110         return $ret if $ret;
111
112         $ctx->{mid} = $mid = uri_unescape($mid);
113         if ($mid =~ /\A[a-f0-9]{40}\z/) {
114                 if ($mid = mid2blob($ctx)) {
115                         require Email::Simple;
116                         use PublicInbox::MID qw/mid_clean/;
117                         $mid = Email::Simple->new($mid);
118                         $ctx->{mid} = mid_clean($mid->header('Message-ID'));
119                 }
120         }
121         undef;
122 }
123
124 # /$LISTNAME/new.atom                     -> Atom feed, includes replies
125 sub get_atom {
126         my ($ctx) = @_;
127         require PublicInbox::Feed;
128         PublicInbox::Feed::generate($ctx);
129 }
130
131 # /$LISTNAME/?r=$GIT_COMMIT                 -> HTML only
132 sub get_index {
133         my ($ctx) = @_;
134         require PublicInbox::Feed;
135         my $srch = searcher($ctx);
136         footer($ctx);
137         if (defined $ctx->{cgi}->param('q')) {
138                 require PublicInbox::SearchView;
139                 PublicInbox::SearchView::sres_top_html($ctx);
140         } else {
141                 PublicInbox::Feed::generate_html_index($ctx);
142         }
143 }
144
145 # just returns a string ref for the blob in the current ctx
146 sub mid2blob {
147         my ($ctx) = @_;
148         require PublicInbox::MID;
149         my $path = PublicInbox::MID::mid2path($ctx->{mid});
150         $ctx->{git}->cat_file("HEAD:$path");
151 }
152
153 # /$LISTNAME/$MESSAGE_ID/raw                    -> raw mbox
154 sub get_mid_txt {
155         my ($ctx) = @_;
156         my $x = mid2blob($ctx) or return r404($ctx);
157         require PublicInbox::Mbox;
158         PublicInbox::Mbox::emit1($ctx, $x);
159 }
160
161 # /$LISTNAME/$MESSAGE_ID/                   -> HTML content (short quotes)
162 sub get_mid_html {
163         my ($ctx) = @_;
164         my $x = mid2blob($ctx) or return r404($ctx);
165
166         require PublicInbox::View;
167         my $foot = footer($ctx);
168         require Email::MIME;
169         my $mime = Email::MIME->new($x);
170         searcher($ctx);
171         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
172           [ PublicInbox::View::msg_html($ctx, $mime, 'f/', $foot) ] ];
173 }
174
175 # /$LISTNAME/$MESSAGE_ID/f/                   -> HTML content (fullquotes)
176 sub get_full_html {
177         my ($ctx) = @_;
178         my $x = mid2blob($ctx) or return r404($ctx);
179
180         require PublicInbox::View;
181         my $foot = footer($ctx);
182         require Email::MIME;
183         my $mime = Email::MIME->new($x);
184         searcher($ctx);
185         [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
186           [ PublicInbox::View::msg_html($ctx, $mime, undef, $foot)] ];
187 }
188
189 # /$LISTNAME/$MESSAGE_ID/t/
190 sub get_thread {
191         my ($ctx, $flat) = @_;
192         my $srch = searcher($ctx) or return need_search($ctx);
193         require PublicInbox::View;
194         my $foot = footer($ctx);
195         $ctx->{flat} = $flat;
196         PublicInbox::View::thread_html($ctx, $foot, $srch);
197 }
198
199 sub self_url {
200         my ($cgi) = @_;
201                                                 # Plack::Request
202         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
203 }
204
205 sub ctx_get {
206         my ($ctx, $key) = @_;
207         my $val = $ctx->{$key};
208         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
209         $val;
210 }
211
212 sub footer {
213         my ($ctx) = @_;
214         return '' unless $ctx;
215         my $git_dir = ctx_get($ctx, 'git_dir');
216
217         # favor user-supplied footer
218         my $footer = try_cat("$git_dir/public-inbox/footer.html");
219         if (defined $footer) {
220                 chomp $footer;
221                 $ctx->{footer} = $footer;
222                 return $footer;
223         }
224
225         # auto-generate a footer
226         my $listname = ctx_get($ctx, 'listname');
227         my $desc = try_cat("$git_dir/description");
228         $desc = '$GIT_DIR/description missing' unless defined $desc;
229         chomp $desc;
230
231         my $urls = try_cat("$git_dir/cloneurl");
232         my @urls = split(/\r?\n/, $urls || '');
233         my $nurls = scalar @urls;
234         if ($nurls == 0) {
235                 $urls = '($GIT_DIR/cloneurl missing)';
236         } elsif ($nurls == 1) {
237                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
238                         '">ssoma</a>: ' . $urls[0];
239         } else {
240                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
241                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
242         }
243
244         my $addr = $pi_config->get($listname, 'address');
245         if (ref($addr) eq 'ARRAY') {
246                 $addr = $addr->[0]; # first address is primary
247         }
248
249         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
250
251         $ctx->{footer} = join("\n",
252                 '- ' . $desc,
253                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
254                         'anybody may post in plain-text (not HTML):',
255                 $addr,
256                 $urls
257         );
258 }
259
260 # search support is optional, returns undef if Xapian is not installed
261 # or not configured for the given GIT_DIR
262 sub searcher {
263         my ($ctx) = @_;
264         eval {
265                 require PublicInbox::Search;
266                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
267         };
268 }
269
270 sub need_search {
271         my ($ctx) = @_;
272         my $msg = <<EOF;
273 <html><head><title>Search not available for this
274 public-inbox</title><body><pre>Search is not available for this public-inbox
275 <a href="../">Return to index</a></pre></body></html>
276 EOF
277         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
278 }
279
280 # /$LISTNAME/$MESSAGE_ID/t.mbox           -> thread as mbox
281 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
282 # note: I'm not a big fan of other compression formats since they're
283 # significantly more expensive on CPU than gzip and less-widely available,
284 # especially on older systems.  Stick to zlib since that's what git uses.
285 sub get_thread_mbox {
286         my ($ctx, $sfx) = @_;
287         my $srch = searcher($ctx) or return need_search($ctx);
288         require PublicInbox::Mbox;
289         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
290 }
291
292
293 # /$LISTNAME/$MESSAGE_ID/t.atom           -> thread as Atom feed
294 sub get_thread_atom {
295         my ($ctx) = @_;
296         searcher($ctx) or return need_search($ctx);
297         $ctx->{self_url} = self_url($ctx->{cgi});
298         require PublicInbox::Feed;
299         PublicInbox::Feed::generate_thread_atom($ctx);
300 }
301
302 sub legacy_redirects {
303         my ($ctx, $path_info) = @_;
304
305         # single-message pages
306         if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
307                 r301($ctx, $1, $2);
308         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
309                 r301($ctx, $1, $2, 'raw');
310
311         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
312                 r301($ctx, $1, $2, 'f/');
313
314         # thread display
315         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
316                 r301($ctx, $1, $2, 't/#u');
317
318         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
319                 r301($ctx, $1, $2, "t.mbox$3");
320
321         # even older legacy redirects
322         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
323                 r301($ctx, $1, $2);
324
325         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
326                 r301($ctx, $1, $2, 't/#u');
327
328         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
329                 r301($ctx, $1, $2, 'f/');
330
331         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
332                 r301($ctx, $1, $2, 'raw');
333
334         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
335                 r301($ctx, $1, $2, "t$3");
336
337         # legacy convenience redirects, order still matters
338         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
339                 r301($ctx, $1, $2);
340         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
341                 r301($ctx, $1, $2, 't/#u');
342         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
343                 r301($ctx, $1, $2, 'f/');
344
345         # some Message-IDs have slashes in them and the HTTP server
346         # may try to be clever and unescape them :<
347         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
348                 msg_page($ctx, $1, $2, $3);
349
350         # in case people leave off the trailing slash:
351         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
352                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
353         } else {
354                 r404();
355         }
356 }
357
358 sub r301 {
359         my ($ctx, $listname, $mid, $suffix) = @_;
360         my $cgi = $ctx->{cgi};
361         my $url;
362         my $qs;
363         if (ref($cgi) eq 'CGI') {
364                 $url = $cgi->url(-base) . '/';
365                 $qs = $cgi->query_string;
366         } else { # Plack::Request
367                 $url = $cgi->base->as_string;
368                 $qs = $cgi->env->{QUERY_STRING};
369         }
370
371         $url .= $listname . '/';
372         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
373         $url .= $suffix if (defined $suffix);
374         $url .= "?$qs" if $qs ne '';
375
376         [ 301,
377           [ Location => $url, 'Content-Type' => 'text/plain' ],
378           [ "Redirecting to $url\n" ] ]
379 }
380
381 sub msg_page {
382         my ($ctx, $list, $mid, $e) = @_;
383         unless (invalid_list_mid($ctx, $list, $mid)) {
384                 '' eq $e and return get_mid_html($ctx);
385                 't/' eq $e and return get_thread($ctx);
386                 't.atom' eq $e and return get_thread_atom($ctx);
387                 't.mbox' eq $e and return get_thread_mbox($ctx);
388                 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
389                 'T/' eq $e and return get_thread($ctx, 1);
390                 'raw' eq $e and return get_mid_txt($ctx);
391                 'f/' eq $e and return get_full_html($ctx);
392         }
393         r404($ctx);
394 }
395
396 1;