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 PublicInbox::Config;
16 use URI::Escape qw(uri_escape_utf8 uri_unescape);
17 our $LISTNAME_RE = qr!\A/([\w\.\-]+)!;
20 $pi_config = PublicInbox::Config->new;
21 # TODO: detect and reload config as needed
30 if ($ENV{PI_PLACKUP}) {
33 # some servers (Ruby webrick) include scheme://host[:port] here,
34 # which confuses CGI.pm when generating self_url.
35 # RFC 3875 does not mention REQUEST_URI at all,
36 # so nuke it since CGI.pm functions without it.
38 delete $ENV{REQUEST_URI};
40 my $ret = main($req, $req->request_method);
42 if (@ARGV && $ARGV[0] eq 'static') {
49 # private functions below
52 my ($cgi, $method) = @_;
54 if ($method !~ /\AGET|HEAD\z/) {
55 return r(405, 'Method Not Allowed');
57 my $path_info = $cgi->path_info;
59 # top-level indices and feeds
60 if ($path_info eq '/') {
62 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
63 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
64 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
65 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
66 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
67 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
69 # single-message pages
70 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
71 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
72 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
73 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
76 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
77 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
79 # convenience redirects, order matters
80 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
81 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
88 sub r404 { r(404, 'Not Found') }
90 # simple response for errors
91 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
93 # returns undef if valid, array ref response if invalid
95 my ($ctx, $listname) = @_;
96 my $git_dir = $pi_config->get($listname, "mainrepo");
97 if (defined $git_dir) {
98 $ctx->{git_dir} = $git_dir;
99 $ctx->{listname} = $listname;
105 # returns undef if valid, array ref response if invalid
106 sub invalid_list_mid {
107 my ($ctx, $listname, $mid) = @_;
108 my $ret = invalid_list($ctx, $listname, $mid);
109 $ctx->{mid} = uri_unescape($mid) unless $ret;
113 # /$LISTNAME/atom.xml -> Atom feed, includes replies
115 my ($ctx, $cgi, $top) = @_;
116 require PublicInbox::Feed;
117 [ 200, [ 'Content-Type' => 'application/xml' ],
118 [ PublicInbox::Feed->generate({
119 git_dir => $ctx->{git_dir},
120 listname => $ctx->{listname},
121 pi_config => $pi_config,
128 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
130 my ($ctx, $cgi, $top) = @_;
131 require PublicInbox::Feed;
132 [ 200, [ 'Content-Type' => 'text/html' ],
133 [ PublicInbox::Feed->generate_html_index({
134 git_dir => $ctx->{git_dir},
135 listname => $ctx->{listname},
136 pi_config => $pi_config,
143 # just returns a string ref for the blob in the current ctx
147 my $hex = Digest::SHA::sha1_hex($ctx->{mid});
148 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
149 die "BUG: not a SHA-1 hex: $hex";
151 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
152 qw(cat-file blob), "HEAD:$1/$2");
153 my $cmd = join(' ', @cmd);
154 my $pid = open my $fh, '-|';
155 defined $pid or die "fork failed: $!\n";
157 open STDERR, '>', '/dev/null'; # ignore errors
158 exec @cmd or die "exec failed: $!\n";
160 my $blob = eval { local $/; <$fh> };
162 $? == 0 ? \$blob : undef;
166 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
168 my ($ctx, $cgi) = @_;
169 my $x = mid2blob($ctx);
170 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
173 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
175 my ($ctx, $cgi) = @_;
176 my $x = mid2blob($ctx);
177 return r404() unless $x;
179 require PublicInbox::View;
180 my $mid_href = PublicInbox::Hval::ascii_html(
181 uri_escape_utf8($ctx->{mid}));
182 my $pfx = "../f/$mid_href.html";
184 [ 200, [ 'Content-Type' => 'text/html' ],
185 [ PublicInbox::View->as_html(Email::MIME->new($$x), $pfx) ] ];
188 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
190 my ($ctx, $cgi) = @_;
191 my $x = mid2blob($ctx);
192 return r404() unless $x;
193 require PublicInbox::View;
195 [ 200, [ 'Content-Type' => 'text/html' ],
196 [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
201 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
204 sub redirect_list_index {
205 my ($ctx, $cgi) = @_;
206 do_redirect(self_url($cgi) . "/");
210 my ($ctx, $cgi) = @_;
211 my $url = self_url($cgi);
213 do_redirect($url . '.html');
219 [ Location => $url, 'Content-Type' => 'text/plain' ],
220 [ "Redirecting to $url\n" ]
225 # preload so we are CoW friendly
226 require PublicInbox::Feed;
227 require PublicInbox::View;
228 require Mail::Thread;
231 require XML::Atom::SimpleFeed;
232 require Plack::Request;
234 my $req = Plack::Request->new(@_);
235 main($req, $req->method);
241 my ($status, $headers, $body) = @$ret;
244 301 => 'Moved Permanently',
246 405 => 'Method Not Allowed',
249 print "Status: $status $codes{$status}\r\n";
251 while (my ($k, $v) = splice(@tmp, 0, 2)) {
254 print "\r\n", $body->[0];