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 Digest::SHA qw(sha1_hex);
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
50 if ($cgi->request_method !~ /\AGET|HEAD\z/) {
51 return r("405 Method Not Allowed");
53 my $path_info = decode_utf8($ENV{PATH_INFO});
55 # top-level indices and feeds
56 if ($path_info eq "/") {
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);
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);
74 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
75 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
76 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
77 redirect_mid_html($cgi, $1, $2);
84 sub r404 { r("404 Not Found") }
86 # simple response for errors
87 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
89 # returns undef if valid, array ref response if invalid
91 my ($ctx, $listname) = @_;
92 my $git_dir = $pi_config->get($listname, "mainrepo");
93 if (defined $git_dir) {
94 $ctx->{git_dir} = $git_dir;
95 $ctx->{listname} = $listname;
101 # returns undef if valid, array ref response if invalid
102 sub invalid_list_mid {
103 my ($ctx, $listname, $mid) = @_;
104 my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
109 # /$LISTNAME/index.atom.xml -> Atom feed
110 # /$LISTNAME/all.atom.xml -> Atom feed, includes replies
112 my ($ctx, $cgi, $top) = @_;
113 require PublicInbox::Feed;
114 [ '200 OK', { 'Content-Type' => 'application/xml' },
115 PublicInbox::Feed->generate({
116 git_dir => $ctx->{git_dir},
117 listname => $ctx->{listname},
118 pi_config => $pi_config,
125 # /$LISTNAME/?before=$GIT_COMMIT -> HTML only
127 my ($ctx, $cgi, $top) = @_;
128 require PublicInbox::Feed;
129 [ '200 OK', { 'Content-Type' => 'text/html' },
130 PublicInbox::Feed->generate_html_index({
131 git_dir => $ctx->{git_dir},
132 listname => $ctx->{listname},
133 pi_config => $pi_config,
140 # just returns a string ref for the blob in the current ctx
143 local $ENV{GIT_DIR} = $ctx->{git_dir};
144 my $hex = sha1_hex($ctx->{mid});
145 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
146 die "BUG: not a SHA-1 hex: $hex";
147 my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
148 $? == 0 ? \$blob : undef;
151 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
153 my ($ctx, $cgi) = @_;
154 my $x = mid2blob($ctx);
155 $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
158 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
160 my ($ctx, $cgi) = @_;
161 my $x = mid2blob($ctx);
162 return r404() unless $x;
164 my $pfx = $cgi->self_url;
165 $pfx =~ s!/m/.+\z!/!; # FIXME: this is not robust
167 require PublicInbox::View;
169 [ "200 OK", {'Content-Type' => 'text/html'},
170 PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
173 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
175 my ($ctx, $cgi) = @_;
176 my $x = mid2blob($ctx);
177 return r404() unless $x;
178 require PublicInbox::View;
180 [ "200 OK", {'Content-Type' => 'text/html'},
181 PublicInbox::View->as_html(Email::MIME->new($$x))];
184 # only used for CGI and static file generation modes
187 if ($headers->{'Content-Type'} eq 'text/plain') {
188 # no way to validate raw messages, mixed encoding is possible.
190 } else { # strict encoding for HTML and XML
191 binmode STDOUT, ':encoding(UTF-8)';