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
29 if ($ENV{PI_PLACKUP}) {
32 # some servers (Ruby webrick) include scheme://host[:port] here,
33 # which confuses CGI.pm when generating self_url.
34 # RFC 3875 does not mention REQUEST_URI at all,
35 # so nuke it since CGI.pm functions without it.
37 delete $ENV{REQUEST_URI};
39 my $ret = main($req, $req->request_method);
41 if (@ARGV && $ARGV[0] eq 'static') {
48 # private functions below
51 my ($cgi, $method) = @_;
53 if ($method !~ /\AGET|HEAD\z/) {
54 return r(405, 'Method Not Allowed');
56 my $path_info = $cgi->path_info;
58 # top-level indices and feeds
59 if ($path_info eq '/') {
61 } elsif ($path_info =~ m!$LISTNAME_RE\z!o) {
62 invalid_list(\%ctx, $1) || redirect_list_index(\%ctx, $cgi);
63 } elsif ($path_info =~ m!$LISTNAME_RE(?:/|/index\.html)?\z!o) {
64 invalid_list(\%ctx, $1) || get_index(\%ctx, $cgi, 0);
65 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
66 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
68 # single-message pages
69 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
70 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
71 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
72 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
75 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
76 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
78 # convenience redirects, order matters
79 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
80 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
87 sub r404 { r(404, 'Not Found') }
89 # simple response for errors
90 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
92 # returns undef if valid, array ref response if invalid
94 my ($ctx, $listname) = @_;
95 my $git_dir = $pi_config->get($listname, "mainrepo");
96 if (defined $git_dir) {
97 $ctx->{git_dir} = $git_dir;
98 $ctx->{listname} = $listname;
104 # returns undef if valid, array ref response if invalid
105 sub invalid_list_mid {
106 my ($ctx, $listname, $mid) = @_;
107 my $ret = invalid_list($ctx, $listname, $mid);
108 $ctx->{mid} = uri_unescape($mid) unless $ret;
112 # /$LISTNAME/atom.xml -> Atom feed, includes replies
114 my ($ctx, $cgi, $top) = @_;
115 require PublicInbox::Feed;
116 [ 200, [ 'Content-Type' => 'application/xml' ],
117 [ PublicInbox::Feed->generate({
118 git_dir => $ctx->{git_dir},
119 listname => $ctx->{listname},
120 pi_config => $pi_config,
127 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
129 my ($ctx, $cgi, $top) = @_;
130 require PublicInbox::Feed;
131 [ 200, [ 'Content-Type' => 'text/html' ],
132 [ PublicInbox::Feed->generate_html_index({
133 git_dir => $ctx->{git_dir},
134 listname => $ctx->{listname},
135 pi_config => $pi_config,
142 # just returns a string ref for the blob in the current ctx
146 my $hex = Digest::SHA::sha1_hex($ctx->{mid});
147 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
148 die "BUG: not a SHA-1 hex: $hex";
150 my ($in, $blob, $err);
151 open my $null, '+<', '/dev/null' or die "open: $!\n";
152 IPC::Run::run(['git', '--git-dir', $ctx->{git_dir},
153 qw(cat-file blob), "HEAD:$1/$2"],
154 $null, \$blob, $null);
155 $? == 0 ? \$blob : undef;
158 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
160 my ($ctx, $cgi) = @_;
161 my $x = mid2blob($ctx);
162 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
165 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
167 my ($ctx, $cgi) = @_;
168 my $x = mid2blob($ctx);
169 return r404() unless $x;
171 require PublicInbox::View;
172 my $mid_href = PublicInbox::Hval::ascii_html(
173 uri_escape_utf8($ctx->{mid}));
174 my $pfx = "../f/$mid_href.html";
176 [ 200, [ '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, [ 'Content-Type' => 'text/html' ],
188 [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
193 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
196 sub redirect_list_index {
197 my ($ctx, $cgi) = @_;
198 do_redirect(self_url($cgi) . "/");
202 my ($ctx, $cgi) = @_;
203 my $url = self_url($cgi);
205 do_redirect($url . '.html');
211 [ Location => $url, 'Content-Type' => 'text/plain' ],
212 [ "Redirecting to $url\n" ]
217 # preload so we are CoW friendly
218 require PublicInbox::Feed;
219 require PublicInbox::View;
220 require Mail::Thread;
223 require XML::Atom::SimpleFeed;
224 require Plack::Request;
226 my $req = Plack::Request->new(@_);
227 main($req, $req->method);
233 my ($status, $headers, $body) = @$ret;
236 301 => 'Moved Permanently',
238 405 => 'Method Not Allowed',
241 print "Status: $status $codes{$status}\r\n";
243 while (my ($k, $v) = splice(@tmp, 0, 2)) {
246 print "\r\n", $body->[0];