]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WWW.pm
www: remove old footer generation code and normalize new.html
[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;
17 use PublicInbox::Hval;
18 use URI::Escape qw(uri_escape_utf8 uri_unescape);
19 use constant SSOMA_URL => '//ssoma.public-inbox.org/';
20 use constant PI_URL => '//public-inbox.org/';
21 require PublicInbox::Git;
22 use PublicInbox::GitHTTPBackend;
23 our $INBOX_RE = qr!\A/([\w\.\-]+)!;
24 our $MID_RE = qr!([^/]+)!;
25 our $END_RE = qr!(T/|t/|t\.mbox(?:\.gz)?|t\.atom|raw|)!;
26 our $ATTACH_RE = qr!(\d[\.\d]*)-([[:alnum:]][\w\.-]+[[:alnum:]])!i;
27
28 sub new {
29         my ($class, $pi_config) = @_;
30         $pi_config ||= PublicInbox::Config->new;
31         bless { pi_config => $pi_config }, $class;
32 }
33
34 # backwards compatibility, do not use
35 sub run {
36         my ($req, $method) = @_;
37         PublicInbox::WWW->new->call($req->env);
38 }
39
40 sub call {
41         my ($self, $env) = @_;
42         my $ctx = { env => $env, www => $self, pi_config => $self->{pi_config} };
43
44         # we don't care about multi-value
45         my %qp = map {
46                 my ($k, $v) = split('=', $_, 2);
47                 $v = '' unless defined $v;
48                 $v =~ tr/+/ /;
49                 ($k, $v)
50         } split(/[&;]/, uri_unescape($env->{QUERY_STRING}));
51         $ctx->{qp} = \%qp;
52
53         my $path_info = $env->{PATH_INFO};
54         my $method = $env->{REQUEST_METHOD};
55
56         if ($method eq 'POST' &&
57                  $path_info =~ m!$INBOX_RE/(git-upload-pack)\z!) {
58                 my $path = $2;
59                 return (invalid_inbox($self, $ctx, $1) ||
60                         serve_git($env, $ctx->{git}, $path));
61         }
62         elsif ($method !~ /\AGET|HEAD\z/) {
63                 return r(405, 'Method Not Allowed');
64         }
65
66         # top-level indices and feeds
67         if ($path_info eq '/') {
68                 r404();
69         } elsif ($path_info =~ m!$INBOX_RE\z!o) {
70                 invalid_inbox($self, $ctx, $1) || r301($ctx, $1);
71         } elsif ($path_info =~ m!$INBOX_RE(?:/|/index\.html)?\z!o) {
72                 invalid_inbox($self, $ctx, $1) || get_index($ctx);
73         } elsif ($path_info =~ m!$INBOX_RE/(?:atom\.xml|new\.atom)\z!o) {
74                 invalid_inbox($self, $ctx, $1) || get_atom($ctx);
75         } elsif ($path_info =~ m!$INBOX_RE/new\.html\z!o) {
76                 invalid_inbox($self, $ctx, $1) || get_new($ctx);
77         } elsif ($path_info =~ m!$INBOX_RE/
78                                 ($PublicInbox::GitHTTPBackend::ANY)\z!ox) {
79                 my $path = $2;
80                 invalid_inbox($self, $ctx, $1) ||
81                         serve_git($env, $ctx->{git}, $path);
82         } elsif ($path_info =~ m!$INBOX_RE/([\w-]+).mbox\.gz\z!o) {
83                 serve_mbox_range($self, $ctx, $1, $2);
84         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$END_RE\z!o) {
85                 msg_page($self, $ctx, $1, $2, $3);
86
87         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/$ATTACH_RE\z!o) {
88                 my ($idx, $fn) = ($3, $4);
89                 invalid_inbox_mid($self, $ctx, $1, $2) ||
90                         get_attach($ctx, $idx, $fn);
91         # in case people leave off the trailing slash:
92         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/(T|t)\z!o) {
93                 my ($inbox, $mid, $suffix) = ($1, $2, $3);
94                 $suffix .= $suffix =~ /\A[tT]\z/ ? '/#u' : '/';
95                 r301($ctx, $inbox, $mid, $suffix);
96
97         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/R/?\z!o) {
98                 my ($inbox, $mid) = ($1, $2);
99                 r301($ctx, $inbox, $mid, '#R');
100
101         } elsif ($path_info =~ m!$INBOX_RE/$MID_RE/f/?\z!o) {
102                 r301($ctx, $1, $2);
103
104         # convenience redirects order matters
105         } elsif ($path_info =~ m!$INBOX_RE/([^/]{2,})\z!o) {
106                 r301($ctx, $1, $2);
107
108         } else {
109                 legacy_redirects($self, $ctx, $path_info);
110         }
111 }
112
113 # for CoW-friendliness, MOOOOO!
114 sub preload {
115         require PublicInbox::Feed;
116         require PublicInbox::View;
117         require PublicInbox::Thread;
118         require Email::MIME;
119         require Digest::SHA;
120         require POSIX;
121
122         foreach (qw(PublicInbox::Search PublicInbox::SearchView
123                         PublicInbox::Mbox IO::Compress::Gzip
124                         PublicInbox::NewsWWW)) {
125                 eval "require $_;";
126         }
127 }
128
129 # private functions below
130
131 sub r404 {
132         my ($ctx) = @_;
133         if ($ctx && $ctx->{mid}) {
134                 require PublicInbox::ExtMsg;
135                 searcher($ctx);
136                 return PublicInbox::ExtMsg::ext_msg($ctx);
137         }
138         r(404, 'Not Found');
139 }
140
141 # simple response for errors
142 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
143
144 # returns undef if valid, array ref response if invalid
145 sub invalid_inbox {
146         my ($self, $ctx, $inbox) = @_;
147         my $obj = $ctx->{pi_config}->lookup_name($inbox);
148         if (defined $obj) {
149                 $ctx->{git_dir} = $obj->{mainrepo};
150                 $ctx->{git} = $obj->git;
151                 $ctx->{-inbox} = $obj;
152                 $ctx->{inbox} = $inbox;
153                 return;
154         }
155
156         # sometimes linkifiers (not ours!) screw up automatic link
157         # generation and link things intended for nntp:// to https?://,
158         # so try to infer links and redirect them to the appropriate
159         # list URL.
160         $self->news_www->call($ctx->{env});
161 }
162
163 # returns undef if valid, array ref response if invalid
164 sub invalid_inbox_mid {
165         my ($self, $ctx, $inbox, $mid) = @_;
166         my $ret = invalid_inbox($self, $ctx, $inbox);
167         return $ret if $ret;
168
169         $ctx->{mid} = $mid = uri_unescape($mid);
170         if ($mid =~ /\A[a-f0-9]{40}\z/) {
171                 # this is horiffically wasteful for legacy URLs:
172                 if ($mid = mid2blob($ctx)) {
173                         require Email::Simple;
174                         use PublicInbox::MID qw/mid_clean/;
175                         my $s = Email::Simple->new($mid);
176                         $ctx->{mid} = mid_clean($s->header('Message-ID'));
177                 }
178         }
179         undef;
180 }
181
182 # /$INBOX/new.atom                     -> Atom feed, includes replies
183 sub get_atom {
184         my ($ctx) = @_;
185         require PublicInbox::Feed;
186         PublicInbox::Feed::generate($ctx);
187 }
188
189 # /$INBOX/new.html                      -> HTML only
190 sub get_new {
191         my ($ctx) = @_;
192         require PublicInbox::Feed;
193         PublicInbox::Feed::new_html($ctx);
194 }
195
196 # /$INBOX/?r=$GIT_COMMIT                 -> HTML only
197 sub get_index {
198         my ($ctx) = @_;
199         require PublicInbox::Feed;
200         my $srch = searcher($ctx);
201         if ($ctx->{env}->{QUERY_STRING} =~ /(?:\A|[&;])q=/) {
202                 require PublicInbox::SearchView;
203                 PublicInbox::SearchView::sres_top_html($ctx);
204         } else {
205                 PublicInbox::Feed::generate_html_index($ctx);
206         }
207 }
208
209 # just returns a string ref for the blob in the current ctx
210 sub mid2blob {
211         my ($ctx) = @_;
212         $ctx->{-inbox}->msg_by_mid($ctx->{mid});
213 }
214
215 # /$INBOX/$MESSAGE_ID/raw                    -> raw mbox
216 sub get_mid_txt {
217         my ($ctx) = @_;
218         my $x = mid2blob($ctx) or return r404($ctx);
219         require PublicInbox::Mbox;
220         PublicInbox::Mbox::emit1($ctx, $x);
221 }
222
223 # /$INBOX/$MESSAGE_ID/                   -> HTML content (short quotes)
224 sub get_mid_html {
225         my ($ctx) = @_;
226         my $x = mid2blob($ctx) or return r404($ctx);
227
228         require PublicInbox::View;
229         require Email::MIME;
230         my $mime = Email::MIME->new($x);
231         searcher($ctx);
232         PublicInbox::View::msg_html($ctx, $mime);
233 }
234
235 # /$INBOX/$MESSAGE_ID/t/
236 sub get_thread {
237         my ($ctx, $flat) = @_;
238         searcher($ctx) or return need_search($ctx);
239         $ctx->{flat} = $flat;
240         require PublicInbox::View;
241         PublicInbox::View::thread_html($ctx);
242 }
243
244 sub ctx_get {
245         my ($ctx, $key) = @_;
246         my $val = $ctx->{$key};
247         (defined $val && $val ne '') or die "BUG: bad ctx, $key unusable";
248         $val;
249 }
250
251 # search support is optional, returns undef if Xapian is not installed
252 # or not configured for the given GIT_DIR
253 sub searcher {
254         my ($ctx) = @_;
255         eval {
256                 require PublicInbox::Search;
257                 $ctx->{srch} = $ctx->{-inbox}->search;
258         };
259 }
260
261 sub need_search {
262         my ($ctx) = @_;
263         my $msg = <<EOF;
264 <html><head><title>Search not available for this
265 public-inbox</title><body><pre>Search is not available for this public-inbox
266 <a href="../">Return to index</a></pre></body></html>
267 EOF
268         [ 501, [ 'Content-Type' => 'text/html; charset=UTF-8' ], [ $msg ] ];
269 }
270
271 # /$INBOX/$MESSAGE_ID/t.mbox           -> thread as mbox
272 # /$INBOX/$MESSAGE_ID/t.mbox.gz        -> thread as gzipped mbox
273 # note: I'm not a big fan of other compression formats since they're
274 # significantly more expensive on CPU than gzip and less-widely available,
275 # especially on older systems.  Stick to zlib since that's what git uses.
276 sub get_thread_mbox {
277         my ($ctx, $sfx) = @_;
278         my $srch = searcher($ctx) or return need_search($ctx);
279         require PublicInbox::Mbox;
280         PublicInbox::Mbox::thread_mbox($ctx, $srch, $sfx);
281 }
282
283
284 # /$INBOX/$MESSAGE_ID/t.atom              -> thread as Atom feed
285 sub get_thread_atom {
286         my ($ctx) = @_;
287         searcher($ctx) or return need_search($ctx);
288         require PublicInbox::Feed;
289         PublicInbox::Feed::generate_thread_atom($ctx);
290 }
291
292 sub legacy_redirects {
293         my ($self, $ctx, $path_info) = @_;
294
295         # single-message pages
296         if ($path_info =~ m!$INBOX_RE/m/(\S+)/\z!o) {
297                 r301($ctx, $1, $2);
298         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)/raw\z!o) {
299                 r301($ctx, $1, $2, 'raw');
300
301         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)/\z!o) {
302                 r301($ctx, $1, $2);
303
304         # thread display
305         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/\z!o) {
306                 r301($ctx, $1, $2, 't/#u');
307
308         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)/mbox(\.gz)?\z!o) {
309                 r301($ctx, $1, $2, "t.mbox$3");
310
311         # even older legacy redirects
312         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\.html\z!o) {
313                 r301($ctx, $1, $2);
314
315         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\.html\z!o) {
316                 r301($ctx, $1, $2, 't/#u');
317
318         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\.html\z!o) {
319                 r301($ctx, $1, $2);
320
321         } elsif ($path_info =~ m!$INBOX_RE/(?:m|f)/(\S+)\.txt\z!o) {
322                 r301($ctx, $1, $2, 'raw');
323
324         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)(\.mbox(?:\.gz)?)\z!o) {
325                 r301($ctx, $1, $2, "t$3");
326
327         # legacy convenience redirects, order still matters
328         } elsif ($path_info =~ m!$INBOX_RE/m/(\S+)\z!o) {
329                 r301($ctx, $1, $2);
330         } elsif ($path_info =~ m!$INBOX_RE/t/(\S+)\z!o) {
331                 r301($ctx, $1, $2, 't/#u');
332         } elsif ($path_info =~ m!$INBOX_RE/f/(\S+)\z!o) {
333                 r301($ctx, $1, $2);
334
335         # some Message-IDs have slashes in them and the HTTP server
336         # may try to be clever and unescape them :<
337         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/$END_RE\z!o) {
338                 msg_page($self, $ctx, $1, $2, $3);
339
340         # in case people leave off the trailing slash:
341         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/(T|t)\z!o) {
342                 r301($ctx, $1, $2, $3 eq 't' ? 't/#u' : $3);
343         } elsif ($path_info =~ m!$INBOX_RE/(\S+/\S+)/f\z!o) {
344                 r301($ctx, $1, $2);
345         } else {
346                 $self->news_www->call($ctx->{env});
347         }
348 }
349
350 sub r301 {
351         my ($ctx, $inbox, $mid, $suffix) = @_;
352         my $obj = $ctx->{-inbox};
353         unless ($obj) {
354                 my $r404 = invalid_inbox($ctx->{www}, $ctx, $inbox);
355                 return $r404 if $r404;
356                 $obj = $ctx->{-inbox};
357         }
358         my $url = $obj->base_url($ctx->{env});
359         my $qs = $ctx->{env}->{QUERY_STRING};
360         $url .= (uri_escape_utf8($mid) . '/') if (defined $mid);
361         $url .= $suffix if (defined $suffix);
362         $url .= "?$qs" if $qs ne '';
363
364         [ 301,
365           [ Location => $url, 'Content-Type' => 'text/plain' ],
366           [ "Redirecting to $url\n" ] ]
367 }
368
369 sub msg_page {
370         my ($self, $ctx, $inbox, $mid, $e) = @_;
371         my $ret;
372         $ret = invalid_inbox_mid($self, $ctx, $inbox, $mid) and return $ret;
373         '' eq $e and return get_mid_html($ctx);
374         'T/' eq $e and return get_thread($ctx, 1);
375         't/' eq $e and return get_thread($ctx);
376         't.atom' eq $e and return get_thread_atom($ctx);
377         't.mbox' eq $e and return get_thread_mbox($ctx);
378         't.mbox.gz' eq $e and return get_thread_mbox($ctx, '.gz');
379         'raw' eq $e and return get_mid_txt($ctx);
380
381         # legacy, but no redirect for compatibility:
382         'f/' eq $e and return get_mid_html($ctx);
383         r404($ctx);
384 }
385
386 sub serve_git {
387         my ($env, $git, $path) = @_;
388         PublicInbox::GitHTTPBackend::serve($env, $git, $path);
389 }
390
391 sub serve_mbox_range {
392         my ($self, $ctx, $inbox, $range) = @_;
393         invalid_inbox($self, $ctx, $inbox) || eval {
394                 require PublicInbox::Mbox;
395                 searcher($ctx);
396                 PublicInbox::Mbox::emit_range($ctx, $range);
397         }
398 }
399
400 sub news_www {
401         my ($self) = @_;
402         my $nw = $self->{news_www};
403         return $nw if $nw;
404         require PublicInbox::NewsWWW;
405         $self->{news_www} = PublicInbox::NewsWWW->new($self->{pi_config});
406 }
407
408 sub get_attach {
409         my ($ctx, $idx, $fn) = @_;
410         require PublicInbox::WwwAttach;
411         PublicInbox::WwwAttach::get_attach($ctx, $idx, $fn);
412 }
413
414 1;