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_unescape);
19 use Digest::SHA qw(sha1_hex);
20 our $LISTNAME_RE = qr!\A(?:/.*?)?/([\w\.\-]+)!;
23 $pi_config = PublicInbox::Config->new;
24 # TODO: detect and reload config as needed
32 my ($status, $headers, $body) = @$ret;
33 set_binmode($headers);
34 if (@ARGV && $ARGV[0] eq 'static') {
37 print "Status: $status\r\n";
38 while (my ($k, $v) = each %$headers) {
46 # private functions below
51 if ($cgi->request_method !~ /\AGET|HEAD\z/) {
52 return r("405 Method Not Allowed");
54 my $path_info = decode_utf8($cgi->path_info);
56 # top-level indices and feeds
57 if ($path_info eq "/") {
59 } elsif ($path_info =~ m!$LISTNAME_RE/(?:index\.html)?\z!o) {
60 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
61 } elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
62 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
63 } elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
64 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
66 # single-message pages
67 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
68 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
69 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
70 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
71 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\z!o) {
72 redirect_mid_html($cgi, $1, $2);
75 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
76 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
77 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\z!o) {
78 redirect_mid_html($cgi, $1, $2);
85 sub r404 { r("404 Not Found") }
87 # simple response for errors
88 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
90 # returns undef if valid, array ref response if invalid
92 my ($ctx, $listname) = @_;
93 my $git_dir = $pi_config->get($listname, "mainrepo");
94 if (defined $git_dir) {
95 $ctx->{git_dir} = $git_dir;
96 $ctx->{listname} = $listname;
102 # returns undef if valid, array ref response if invalid
103 sub invalid_list_mid {
104 my ($ctx, $listname, $mid) = @_;
105 my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
106 $ctx->{mid} = uri_unescape($mid);
110 # /$LISTNAME/index.atom.xml -> Atom feed
111 # /$LISTNAME/all.atom.xml -> Atom feed, includes replies
113 my ($ctx, $cgi, $top) = @_;
114 require PublicInbox::Feed;
115 [ '200 OK', { 'Content-Type' => 'application/xml' },
116 PublicInbox::Feed->generate({
117 git_dir => $ctx->{git_dir},
118 listname => $ctx->{listname},
119 pi_config => $pi_config,
126 # /$LISTNAME/?before=$GIT_COMMIT -> HTML only
128 my ($ctx, $cgi, $top) = @_;
129 require PublicInbox::Feed;
130 [ '200 OK', { 'Content-Type' => 'text/html' },
131 PublicInbox::Feed->generate_html_index({
132 git_dir => $ctx->{git_dir},
133 listname => $ctx->{listname},
134 pi_config => $pi_config,
141 # just returns a string ref for the blob in the current ctx
144 local $ENV{GIT_DIR} = $ctx->{git_dir};
145 my $hex = sha1_hex($ctx->{mid});
146 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
147 die "BUG: not a SHA-1 hex: $hex";
148 my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
149 $? == 0 ? \$blob : undef;
152 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
154 my ($ctx, $cgi) = @_;
155 my $x = mid2blob($ctx);
156 $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
159 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
161 my ($ctx, $cgi) = @_;
162 my $x = mid2blob($ctx);
163 return r404() unless $x;
165 my $pfx = $cgi->self_url;
166 $pfx =~ s!/m/.+\z!/!; # FIXME: this is not robust
168 require PublicInbox::View;
170 [ "200 OK", {'Content-Type' => 'text/html'},
171 PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
174 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
176 my ($ctx, $cgi) = @_;
177 my $x = mid2blob($ctx);
178 return r404() unless $x;
179 require PublicInbox::View;
181 [ "200 OK", {'Content-Type' => 'text/html'},
182 PublicInbox::View->as_html(Email::MIME->new($$x))];
185 # only used for CGI and static file generation modes
188 if ($headers->{'Content-Type'} eq 'text/plain') {
189 # no way to validate raw messages, mixed encoding is possible.
191 } else { # strict encoding for HTML and XML
192 binmode STDOUT, ':encoding(UTF-8)';