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