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/index\.atom\.xml\z!o) {
69 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 1);
70 } elsif ($path_info =~ m!$LISTNAME_RE/all\.atom\.xml\z!o) {
71 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
73 # single-message pages
74 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
75 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
76 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
77 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
80 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
81 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
83 # convenience redirect
84 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
85 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
92 sub r404 { r("404 Not Found") }
94 # simple response for errors
95 sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, $_[0]."\n" ] }
97 # returns undef if valid, array ref response if invalid
99 my ($ctx, $listname) = @_;
100 my $git_dir = $pi_config->get($listname, "mainrepo");
101 if (defined $git_dir) {
102 $ctx->{git_dir} = $git_dir;
103 $ctx->{listname} = $listname;
109 # returns undef if valid, array ref response if invalid
110 sub invalid_list_mid {
111 my ($ctx, $listname, $mid) = @_;
112 my $ret = invalid_list($ctx, $listname, $mid) and return $ret;
113 $ctx->{mid} = uri_unescape($mid);
117 # /$LISTNAME/index.atom.xml -> Atom feed
118 # /$LISTNAME/all.atom.xml -> Atom feed, includes replies
120 my ($ctx, $cgi, $top) = @_;
121 require PublicInbox::Feed;
122 [ '200 OK', { 'Content-Type' => 'application/xml' },
123 PublicInbox::Feed->generate({
124 git_dir => $ctx->{git_dir},
125 listname => $ctx->{listname},
126 pi_config => $pi_config,
133 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
135 my ($ctx, $cgi, $top) = @_;
136 require PublicInbox::Feed;
137 [ '200 OK', { 'Content-Type' => 'text/html' },
138 PublicInbox::Feed->generate_html_index({
139 git_dir => $ctx->{git_dir},
140 listname => $ctx->{listname},
141 pi_config => $pi_config,
148 # just returns a string ref for the blob in the current ctx
151 local $ENV{GIT_DIR} = $ctx->{git_dir};
153 my $hex = Digest::SHA::sha1_hex($ctx->{mid});
154 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
155 die "BUG: not a SHA-1 hex: $hex";
156 my $blob = `git cat-file blob HEAD:$1/$2 2>/dev/null`;
157 $? == 0 ? \$blob : undef;
160 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
162 my ($ctx, $cgi) = @_;
163 my $x = mid2blob($ctx);
164 $x ? [ "200 OK", {'Content-Type' => 'text/plain'}, $$x ] : r404();
167 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
169 my ($ctx, $cgi) = @_;
170 my $x = mid2blob($ctx);
171 return r404() unless $x;
173 my $pfx = "../f/" . uri_escape($ctx->{mid}) . ".html";
174 require PublicInbox::View;
176 [ "200 OK", {'Content-Type' => 'text/html'},
177 PublicInbox::View->as_html(Email::MIME->new($$x), $pfx)];
180 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
182 my ($ctx, $cgi) = @_;
183 my $x = mid2blob($ctx);
184 return r404() unless $x;
185 require PublicInbox::View;
187 [ "200 OK", {'Content-Type' => 'text/html'},
188 PublicInbox::View->as_html(Email::MIME->new($$x))];
191 sub redirect_list_index {
192 my ($ctx, $cgi) = @_;
193 do_redirect($cgi->self_url . "/");
197 my ($ctx, $cgi) = @_;
198 my $url = $cgi->self_url;
200 do_redirect($url . '.html');
205 [ '301 Moved Permanently',
206 { Location => $url, 'Content-Type' => 'text/plain' },
207 "Redirecting to $url\n"
211 # only used for CGI and static file generation modes
214 if ($headers->{'Content-Type'} eq 'text/plain') {
215 # no way to validate raw messages, mixed encoding is possible.
217 } else { # strict encoding for HTML and XML
218 binmode STDOUT, ':encoding(UTF-8)';