]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: redirect with query string
[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         ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
202 }
203
204 sub ctx_get {
205         my ($ctx, $key) = @_;
206         my $val = $ctx->{$key};
207         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable\n";
208         $val;
209 }
210
211 sub footer {
212         my ($ctx) = @_;
213         return '' unless $ctx;
214         my $git_dir = ctx_get($ctx, 'git_dir');
215
216         # favor user-supplied footer
217         my $footer = try_cat("$git_dir/public-inbox/footer.html");
218         if (defined $footer) {
219                 chomp $footer;
220                 $ctx->{footer} = $footer;
221                 return $footer;
222         }
223
224         # auto-generate a footer
225         my $listname = ctx_get($ctx, 'listname');
226         my $desc = try_cat("$git_dir/description");
227         $desc = '$GIT_DIR/description missing' unless defined $desc;
228         chomp $desc;
229
230         my $urls = try_cat("$git_dir/cloneurl");
231         my @urls = split(/\r?\n/, $urls || '');
232         my $nurls = scalar @urls;
233         if ($nurls == 0) {
234                 $urls = '($GIT_DIR/cloneurl missing)';
235         } elsif ($nurls == 1) {
236                 $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
237                         '">ssoma</a>: ' . $urls[0];
238         } else {
239                 $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
240                         "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
241         }
242
243         my $addr = $pi_config->get($listname, 'address');
244         if (ref($addr) eq 'ARRAY') {
245                 $addr = $addr->[0]; # first address is primary
246         }
247
248         $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
249
250         $ctx->{footer} = join("\n",
251                 '- ' . $desc,
252                 "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
253                         'anybody may post in plain-text (not HTML):',
254                 $addr,
255                 $urls
256         );
257 }
258
259 # search support is optional, returns undef if Xapian is not installed
260 # or not configured for the given GIT_DIR
261 sub searcher {
262         my ($ctx) = @_;
263         eval {
264                 require PublicInbox::Search;
265                 $ctx->{srch} = PublicInbox::Search->new($ctx->{git_dir});
266         };
267 }
268
269 sub need_search {
270         my ($ctx) = @_;
271         my $msg = <<EOF;
272 <html><head><title>Search not available for this
273 public-inbox</title><body><pre>Search is not available for this public-inbox
274 <a href="../">Return to index</a></pre></body></html>
275 EOF
276         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
277 }
278
279 # /$LISTNAME/$MESSAGE_ID/t.mbox           -> thread as mbox
280 # /$LISTNAME/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
281 # note: I'm not a big fan of other compression formats since they're
282 # significantly more expensive on CPU than gzip and less-widely available,
283 # especially on older systems.  Stick to zlib since that's what git uses.
284 sub get_thread_mbox {
285         my ($ctx, $sfx) = @_;
286         my $srch = searcher($ctx) or return need_search($ctx);
287         require PublicInbox::Mbox;
288         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
289 }
290
291
292 # /$LISTNAME/$MESSAGE_ID/t.atom           -> thread as Atom feed
293 sub get_thread_atom {
294         my ($ctx) = @_;
295         searcher($ctx) or return need_search($ctx);
296         $ctx->{self_url} = self_url($ctx->{cgi});
297         require PublicInbox::Feed;
298         PublicInbox::Feed::generate_thread_atom($ctx);
299 }
300
301 sub legacy_redirects {
302         my ($ctx, $path_info) = @_;
303
304         # single-message pages
305         if ($path_info =~ m!$LISTNAME_RE/m/(\S+)/\z!o) {
306                 r301($ctx, $1, $2);
307         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)/raw\z!o) {
308                 r301($ctx, $1, $2, 'raw');
309
310         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)/\z!o) {
311                 r301($ctx, $1, $2, 'f/');
312
313         # thread display
314         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/\z!o) {
315                 r301($ctx, $1, $2, 't/#u');
316
317         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)/mbox(\.gz)?\z!o) {
318                 r301($ctx, $1, $2, "t.mbox$3");
319
320         # even older legacy redirects
321         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
322                 r301($ctx, $1, $2);
323
324         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\.html\z!o) {
325                 r301($ctx, $1, $2, 't/#u');
326
327         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
328                 r301($ctx, $1, $2, 'f/');
329
330         } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\.txt\z!o) {
331                 r301($ctx, $1, $2, 'raw');
332
333         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
334                 r301($ctx, $1, $2, "t$3");
335
336         # legacy convenience redirects, order still matters
337         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
338                 r301($ctx, $1, $2);
339         } elsif ($path_info =~ m!$LISTNAME_RE/t/(\S+)\z!o) {
340                 r301($ctx, $1, $2, 't/#u');
341         } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
342                 r301($ctx, $1, $2, 'f/');
343
344         # some Message-IDs have slashes in them and the HTTP server
345         # may try to be clever and unescape them :<
346         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/$END_RE\z!o) {
347                 msg_page($ctx, $1, $2, $3);
348
349         # in case people leave off the trailing slash:
350         } elsif ($path_info =~ m!$LISTNAME_RE/(\S+/\S+)/(f|T|t)\z!o) {
351                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
352         } else {
353                 r404();
354         }
355 }
356
357 sub r301 {
358         my ($ctx, $listname, $mid, $suffix) = @_;
359         my $cgi = $ctx->{cgi};
360         my $url;
361         my $qs;
362         if (ref($cgi) eq 'CGI') {
363                 $url = $cgi->url(-base) . '/';
364                 $qs = $cgi->query_string;
365         } else {
366                 $url = $cgi->base->as_string;
367                 $qs = $cgi->env->{QUERY_STRING};
368         }
369
370         $url .= $listname . '/';
371         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
372         $url .= $suffix if (defined $suffix);
373         $url .= "?$qs" if $qs ne '';
374
375         [ 301,
376           [ Location => $url, 'Content-Type' => 'text/plain' ],
377           [ "Redirecting to $url\n" ] ]
378 }
379
380 sub msg_page {
381         my ($ctx, $list, $mid, $e) = @_;
382         unless (invalid_list_mid($ctx, $list, $mid)) {
383                 '' eq $e and return get_mid_html($ctx);
384                 't/' eq $e and return get_thread($ctx);
385                 't.atom' eq $e and return get_thread_atom($ctx);
386                 't.mbox' eq $e and return get_thread_mbox($ctx);
387                 't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
388                 'T/' eq $e and return get_thread($ctx, 1);
389                 'raw' eq $e and return get_mid_txt($ctx);
390                 'f/' eq $e and return get_full_html($ctx);
391         }
392         r404($ctx);
393 }
394
395 1;