]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-cgi
4e727da8a61fd20df7fd3921388314e15234be86
[public-inbox.git] / public-inbox-cgi
1 #!/usr/bin/perl -w
2 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
3 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 #
5 # We focus on the lowest common denominators here:
6 # - targeted at text-only console browsers (lynx, w3m, etc..)
7 # - Only basic HTML, CSS only for line-wrapping <pre> text content for GUIs
8 # - No JavaScript, graphics or icons allowed.
9 # - Must not rely on static content
10 # - UTF-8 is only for user-content, 7-bit US-ASCII for us
11
12 use 5.008;
13 use strict;
14 use warnings;
15 use CGI qw(:cgi :escapeHTML -nosticky); # PSGI/FastCGI/mod_perl compat
16 use Encode qw(decode_utf8);
17 use PublicInbox::Config;
18 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
19 our $pi_config;
20 BEGIN {
21         $pi_config = PublicInbox::Config->new;
22         # TODO: detect and reload config as needed
23         if ($ENV{MOD_PERL}) {
24                 CGI->compile;
25         }
26 }
27
28 binmode STDOUT, ':utf8';
29
30 my $ret = main();
31
32 my ($status, $headers, $body) = @$ret;
33 if (@ARGV && $ARGV[0] eq 'static') {
34         print $body;
35 } else { # CGI
36         print "Status: $status\r\n";
37         while (my ($k, $v) = each %$headers) {
38                 print "$k: $v\r\n";
39         }
40         print "\r\n", $body;
41 }
42
43 # TODO: plack support
44
45 # private functions below
46
47 sub main {
48         my $cgi = CGI->new;
49         my %ctx;
50         if ($cgi->request_method !~ /\AGET|HEAD\z/) {
51                 return r("405 Method Not Allowed");
52         }
53         my $path_info = decode_utf8($ENV{PATH_INFO});
54
55         # top-level indices and feeds
56         if ($path_info eq "/") {
57                 r404();
58         } elsif ($path_info =~ m!$LISTNAME_RE/?\z!o) {
59                 invalid_list(\%ctx, $1) || get_list_log(\%ctx, $cgi);
60         } elsif ($path_info =~ m!$LISTNAME_RE/all\z!o) {
61                 invalid_list(\%ctx, $1) || get_list_all(\%ctx, $cgi);
62         } elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
63                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
64         } elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
65                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
66
67         # single-message pages
68         } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.txt\z!o) {
69                 get_mid_txt($cgi, $1, $2);
70         } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\.html\z!o) {
71                 get_mid_html($cgi, $1, $2);
72         } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\z!o) {
73                 redirect_mid_html($cgi, $1, $2);
74         } else {
75                 r404();
76         }
77 }
78
79 sub r404 { r("404 Not Found") }
80
81 # simple response for errors
82 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
83
84 # returns undef if valid, array ref response if invalid
85 sub invalid_list {
86         my ($ctx, $listname) = @_;
87         my $git_dir = $pi_config->get($listname, "mainrepo");
88         if (defined $git_dir) {
89                 $ctx->{git_dir} = $git_dir;
90                 $ctx->{listname} = $listname;
91                 return undef;
92         }
93         r404();
94 }
95
96 # returns undef if valid, array ref response if invalid
97 sub invalid_list_mid {
98         my ($ctx, $listname, $mid) = @_;
99         my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
100         $ctx->{mid} = $mid;
101         undef;
102 }
103
104 sub get_atom {
105         my ($ctx, $cgi, $top) = @_;
106         require PublicInbox::Feed;
107         [ '200 OK', { 'Content-Type' => 'application/xml' },
108           PublicInbox::Feed->generate({
109                         git_dir => $ctx->{git_dir},
110                         listname => $ctx->{listname},
111                         pi_config => $pi_config,
112                         cgi => $cgi,
113                         top => $top,
114                 })
115         ];
116 }