]> Sergey Matveev's repositories - public-inbox.git/blob - public-inbox-cgi
cgi: add one-line descriptions for subroutines
[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 use Digest::SHA qw(sha1_hex);
19 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
20 our $pi_config;
21 BEGIN {
22         $pi_config = PublicInbox::Config->new;
23         # TODO: detect and reload config as needed
24         if ($ENV{MOD_PERL}) {
25                 CGI->compile;
26         }
27 }
28
29 my $ret = main();
30
31 my ($status, $headers, $body) = @$ret;
32 set_binmode($headers);
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(?:/|/index\.html)?\z!o) {
59                 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
60         } elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
61                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
62         } elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
63                 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
64
65         # single-message pages
66         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
67                 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
68         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
69                 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
70         } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
71                 redirect_mid_html($cgi, $1, $2);
72         } else {
73                 r404();
74         }
75 }
76
77 sub r404 { r("404 Not Found") }
78
79 # simple response for errors
80 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
81
82 # returns undef if valid, array ref response if invalid
83 sub invalid_list {
84         my ($ctx, $listname) = @_;
85         my $git_dir = $pi_config->get($listname, "mainrepo");
86         if (defined $git_dir) {
87                 $ctx->{git_dir} = $git_dir;
88                 $ctx->{listname} = $listname;
89                 return undef;
90         }
91         r404();
92 }
93
94 # returns undef if valid, array ref response if invalid
95 sub invalid_list_mid {
96         my ($ctx, $listname, $mid) = @_;
97         my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
98         $ctx->{mid} = $mid;
99         undef;
100 }
101
102 # /$LISTNAME/index.atom.xml                     -> Atom feed
103 # /$LISTNAME/all.atom.xml                       -> Atom feed, includes replies
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 }
117
118 # /$LISTNAME/?before=$GIT_COMMIT                 -> HTML only
119 sub get_index {
120         my ($ctx, $cgi, $top) = @_;
121         require PublicInbox::Feed;
122         [ '200 OK', { 'Content-Type' => 'text/html' },
123           PublicInbox::Feed->generate_html_index({
124                         git_dir => $ctx->{git_dir},
125                         listname => $ctx->{listname},
126                         pi_config => $pi_config,
127                         cgi => $cgi,
128                         top => $top,
129                 })
130         ];
131 }
132
133 # just returns a string ref for the blob in the current ctx
134 sub mid2blob {
135         my ($ctx) = @_;
136         local $ENV{GIT_DIR} = $ctx->{git_dir};
137         my $hex = sha1_hex($ctx->{mid});
138         $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
139                         die "BUG: not a SHA-1 hex: $hex";
140         my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
141         $? == 0 ? \$blob : undef;
142 }
143
144 # /$LISTNAME/m/$MESSAGE_ID.txt                    -> raw original
145 sub get_mid_txt {
146         my ($ctx, $cgi) = @_;
147         my $x = mid2blob($ctx);
148         $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
149 }
150
151 # only used for CGI and static file generation modes
152 sub set_binmode {
153         my ($headers) = @_;
154         if ($headers->{'Content-Type'} eq 'text/plain') {
155                 # no way to validate raw messages, mixed encoding is possible.
156                 binmode STDOUT;
157         } else { # strict encoding for HTML and XML
158                 binmode STDOUT, ':encoding(UTF-8)';
159         }
160 }