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 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
49 # some servers (Ruby webrick) include scheme://host[:port] here,
50 # which confuses CGI.pm when generating self_url.
51 # RFC 3875 does not mention REQUEST_URI at all,
52 # so nuke it since CGI.pm functions without it.
53 delete $ENV{REQUEST_URI};
57 if ($cgi->request_method !~ /\AGET|HEAD\z/) {
58 return r("405 Method Not Allowed");
60 my $path_info = decode_utf8($cgi->path_info);
62 # top-level indices and feeds
63 if ($path_info eq "/") {
65 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
66 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
67 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
68 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 1);
69 } elsif ($path_info =~ m!$LISTNAME_RE/(?:all\.html)?\z!o) {
70 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
71 } elsif ($path_info =~ m!$LISTNAME_RE/index\.atom\.xml\z!o) {
72 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
73 } elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
74 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
76 # single-message pages
77 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
78 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
79 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
80 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
83 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
84 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
86 # convenience redirect
87 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
88 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
95 sub r404 { r("404 Not Found") }
97 # simple response for errors
98 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, $_[0]."\n" ] }
100 # returns undef if valid, array ref response if invalid
102 my ($ctx, $listname) = @_;
103 my $git_dir = $pi_config->get($listname, "mainrepo");
104 if (defined $git_dir) {
105 $ctx->{git_dir} = $git_dir;
106 $ctx->{listname} = $listname;
112 # returns undef if valid, array ref response if invalid
113 sub invalid_list_mid {
114 my ($ctx, $listname, $mid) = @_;
115 my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
116 $ctx->{mid} = uri_unescape($mid);
120 # /$LISTNAME/index.atom.xml -> Atom feed
121 # /$LISTNAME/all.atom.xml -> Atom feed, includes replies
123 my ($ctx, $cgi, $top) = @_;
124 require PublicInbox::Feed;
125 [ '200 OK', { 'Content-Type' => 'application/xml' },
126 PublicInbox::Feed->generate({
127 git_dir => $ctx->{git_dir},
128 listname => $ctx->{listname},
129 pi_config => $pi_config,
136 # /$LISTNAME/?before=$GIT_COMMIT -> HTML only
138 my ($ctx, $cgi, $top) = @_;
139 require PublicInbox::Feed;
140 [ '200 OK', { 'Content-Type' => 'text/html' },
141 PublicInbox::Feed->generate_html_index({
142 git_dir => $ctx->{git_dir},
143 listname => $ctx->{listname},
144 pi_config => $pi_config,
151 # just returns a string ref for the blob in the current ctx
154 local $ENV{GIT_DIR} = $ctx->{git_dir};
155 my $hex = sha1_hex($ctx->{mid});
156 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
157 die "BUG: not a SHA-1 hex: $hex";
158 my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
159 $? == 0 ? \$blob : undef;
162 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
164 my ($ctx, $cgi) = @_;
165 my $x = mid2blob($ctx);
166 $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
169 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
171 my ($ctx, $cgi) = @_;
172 my $x = mid2blob($ctx);
173 return r404() unless $x;
175 my $pfx = "../f/" . uri_escape($ctx->{mid}) . ".html";
176 require PublicInbox::View;
178 [ "200 OK", {'Content-Type' => 'text/html'},
179 PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
182 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
184 my ($ctx, $cgi) = @_;
185 my $x = mid2blob($ctx);
186 return r404() unless $x;
187 require PublicInbox::View;
189 [ "200 OK", {'Content-Type' => 'text/html'},
190 PublicInbox::View->as_html(Email::MIME->new($$x))];
193 sub redirect_list_index {
194 my ($ctx, $cgi) = @_;
195 do_redirect($cgi->self_url . "/");
199 my ($ctx, $cgi) = @_;
200 my $url = $cgi->self_url;
202 do_redirect($url . '.html');
207 [ '301 Moved Permanently',
208 { Location => $url, 'Content-Type' => 'text/plain' },
209 "Redirecting to $url\n"
213 # only used for CGI and static file generation modes
216 if ($headers->{'Content-Type'} eq 'text/plain') {
217 # no way to validate raw messages, mixed encoding is possible.
219 } else { # strict encoding for HTML and XML
220 binmode STDOUT, ':encoding(UTF-8)';