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