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)
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
15 use CGI qw(:cgi -nosticky); # PSGI/FastCGI/mod_perl compat
16 use Encode qw(find_encoding);
17 use PublicInbox::Config;
18 use URI::Escape qw(uri_escape_utf8 uri_unescape);
19 our $enc_utf8 = find_encoding('UTF-8');
20 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
23 $pi_config = PublicInbox::Config->new;
24 # TODO: detect and reload config as needed
30 if ($ENV{PI_PLACKUP}) {
35 if (@ARGV && $ARGV[0] eq 'static') {
42 # private functions below
45 # some servers (Ruby webrick) include scheme://host[:port] here,
46 # which confuses CGI.pm when generating self_url.
47 # RFC 3875 does not mention REQUEST_URI at all,
48 # so nuke it since CGI.pm functions without it.
49 delete $ENV{REQUEST_URI};
53 if ($cgi->request_method !~ /\AGET|HEAD\z/) {
54 return r(405, 'Method Not Allowed');
56 my $path_info = $enc_utf8->decode($cgi->path_info);
58 # top-level indices and feeds
59 if ($path_info eq "/") {
61 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
62 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
63 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
64 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
65 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
66 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
68 # single-message pages
69 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
70 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
71 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
72 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
75 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
76 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
78 # convenience redirects, order matters
79 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
80 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
87 sub r404 { r(404, 'Not Found') }
89 # simple response for errors
90 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
92 # returns undef if valid, array ref response if invalid
94 my ($ctx, $listname) = @_;
95 my $git_dir = $pi_config->get($listname, "mainrepo");
96 if (defined $git_dir) {
97 $ctx->{git_dir} = $git_dir;
98 $ctx->{listname} = $listname;
104 # returns undef if valid, array ref response if invalid
105 sub invalid_list_mid {
106 my ($ctx, $listname, $mid) = @_;
107 my $ret = invalid_list($ctx, $listname, $mid);
108 $ctx->{mid} = uri_unescape($mid) unless $ret;
112 # /$LISTNAME/atom.xml -> Atom feed, includes replies
114 my ($ctx, $cgi, $top) = @_;
115 require PublicInbox::Feed;
116 [ 200, [ 'Content-Type' => 'application/xml' ],
117 [ PublicInbox::Feed->generate({
118 git_dir => $ctx->{git_dir},
119 listname => $ctx->{listname},
120 pi_config => $pi_config,
127 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
129 my ($ctx, $cgi, $top) = @_;
130 require PublicInbox::Feed;
131 [ 200, [ 'Content-Type' => 'text/html' ],
132 [ PublicInbox::Feed->generate_html_index({
133 git_dir => $ctx->{git_dir},
134 listname => $ctx->{listname},
135 pi_config => $pi_config,
142 # just returns a string ref for the blob in the current ctx
145 local $ENV{GIT_DIR} = $ctx->{git_dir};
147 my $hex = Digest::SHA::sha1_hex($ctx->{mid});
148 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
149 die "BUG: not a SHA-1 hex: $hex";
150 my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
151 $? == 0 ? \$blob : undef;
154 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
156 my ($ctx, $cgi) = @_;
157 my $x = mid2blob($ctx);
158 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
161 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
163 my ($ctx, $cgi) = @_;
164 my $x = mid2blob($ctx);
165 return r404() unless $x;
167 require PublicInbox::View;
168 my $mid_href = PublicInbox::Hval::ascii_html(
169 uri_escape_utf8($ctx->{mid}));
170 my $pfx = "../f/$mid_href.html";
172 [ 200, [ 'Content-Type' => 'text/html' ],
173 [ PublicInbox::View->as_html(Email::MIME->new($$x), $pfx) ] ];
176 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
178 my ($ctx, $cgi) = @_;
179 my $x = mid2blob($ctx);
180 return r404() unless $x;
181 require PublicInbox::View;
183 [ 200, [ 'Content-Type' => 'text/html' ],
184 [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
187 sub redirect_list_index {
188 my ($ctx, $cgi) = @_;
189 do_redirect($cgi->self_url . "/");
193 my ($ctx, $cgi) = @_;
194 my $url = $cgi->self_url;
196 do_redirect($url . '.html');
202 [ Location => $url, 'Content-Type' => 'text/plain' ],
203 [ "Redirecting to $url\n" ]
208 require CGI::Emulate::PSGI;
210 # preload so we are CoW friendly
211 require PublicInbox::Feed;
212 require PublicInbox::View;
213 require Mail::Thread;
216 require XML::Atom::SimpleFeed;
217 eval { require Git };
220 local %ENV = (%ENV, CGI::Emulate::PSGI->emulate_environment($e));
227 my ($status, $headers, $body) = @$ret;
230 301 => 'Moved Permanently',
232 405 => 'Method Not Allowed',
235 print "Status: $status $codes{$status}\r\n";
237 while (my ($k, $v) = splice(@tmp, 0, 2)) {
240 print "\r\n", $body->[0];