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 :escapeHTML -nosticky); # PSGI/FastCGI/mod_perl compat
16 use Encode qw(decode_utf8);
17 use PublicInbox::Config;
18 use URI::Escape qw(uri_escape uri_unescape);
19 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
22 $pi_config = PublicInbox::Config->new;
23 # TODO: detect and reload config as needed
31 my ($status, $headers, $body) = @$ret;
32 set_binmode($headers);
33 if (@ARGV && $ARGV[0] eq 'static') {
36 print "Status: $status\r\n";
37 while (my ($k, $v) = each %$headers) {
45 # private functions below
48 # some servers (Ruby webrick) include scheme://host[:port] here,
49 # which confuses CGI.pm when generating self_url.
50 # RFC 3875 does not mention REQUEST_URI at all,
51 # so nuke it since CGI.pm functions without it.
52 delete $ENV{REQUEST_URI};
56 if ($cgi->request_method !~ /\AGET|HEAD\z/) {
57 return r("405 Method Not Allowed");
59 my $path_info = decode_utf8($cgi->path_info);
61 # top-level indices and feeds
62 if ($path_info eq "/") {
64 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
65 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
66 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
67 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
68 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
69 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
71 # single-message pages
72 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
73 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
74 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
75 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
78 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
79 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
81 # convenience redirect
82 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
83 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
90 sub r404 { r("404 Not Found") }
92 # simple response for errors
93 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, $_[0]."\n" ] }
95 # returns undef if valid, array ref response if invalid
97 my ($ctx, $listname) = @_;
98 my $git_dir = $pi_config->get($listname, "mainrepo");
99 if (defined $git_dir) {
100 $ctx->{git_dir} = $git_dir;
101 $ctx->{listname} = $listname;
107 # returns undef if valid, array ref response if invalid
108 sub invalid_list_mid {
109 my ($ctx, $listname, $mid) = @_;
110 my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
111 $ctx->{mid} = uri_unescape($mid);
115 # /$LISTNAME/atom.xml -> Atom feed, includes replies
117 my ($ctx, $cgi, $top) = @_;
118 require PublicInbox::Feed;
119 [ '200 OK', { 'Content-Type' => 'application/xml' },
120 PublicInbox::Feed->generate({
121 git_dir => $ctx->{git_dir},
122 listname => $ctx->{listname},
123 pi_config => $pi_config,
130 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
132 my ($ctx, $cgi, $top) = @_;
133 require PublicInbox::Feed;
134 [ '200 OK', { 'Content-Type' => 'text/html' },
135 PublicInbox::Feed->generate_html_index({
136 git_dir => $ctx->{git_dir},
137 listname => $ctx->{listname},
138 pi_config => $pi_config,
145 # just returns a string ref for the blob in the current ctx
148 local $ENV{GIT_DIR} = $ctx->{git_dir};
150 my $hex = Digest::SHA::sha1_hex($ctx->{mid});
151 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
152 die "BUG: not a SHA-1 hex: $hex";
153 my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
154 $? == 0 ? \$blob : undef;
157 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
159 my ($ctx, $cgi) = @_;
160 my $x = mid2blob($ctx);
161 $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
164 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
166 my ($ctx, $cgi) = @_;
167 my $x = mid2blob($ctx);
168 return r404() unless $x;
170 my $pfx = "../f/" . uri_escape($ctx->{mid}) . ".html";
171 require PublicInbox::View;
173 [ "200 OK", {'Content-Type' => 'text/html'},
174 PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
177 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
179 my ($ctx, $cgi) = @_;
180 my $x = mid2blob($ctx);
181 return r404() unless $x;
182 require PublicInbox::View;
184 [ "200 OK", {'Content-Type' => 'text/html'},
185 PublicInbox::View->as_html(Email::MIME->new($$x))];
188 sub redirect_list_index {
189 my ($ctx, $cgi) = @_;
190 do_redirect($cgi->self_url . "/");
194 my ($ctx, $cgi) = @_;
195 my $url = $cgi->self_url;
197 do_redirect($url . '.html');
202 [ '301 Moved Permanently',
203 { Location => $url, 'Content-Type' => 'text/plain' },
204 "Redirecting to $url\n"
208 # only used for CGI and static file generation modes
211 if ($headers->{'Content-Type'} eq 'text/plain') {
212 # no way to validate raw messages, mixed encoding is possible.
214 } else { # strict encoding for HTML and XML
215 binmode STDOUT, ':encoding(UTF-8)';