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