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\.\-]+)!;
18 our $NO_SCRIPT_NAME; # for prettier redirects with mod_perl2
21 $pi_config = PublicInbox::Config->new;
22 # TODO: detect and reload config as needed
23 $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
32 if ($ENV{PI_PLACKUP}) {
35 # some servers (Ruby webrick) include scheme://host[:port] here,
36 # which confuses CGI.pm when generating self_url.
37 # RFC 3875 does not mention REQUEST_URI at all,
38 # so nuke it since CGI.pm functions without it.
40 delete $ENV{REQUEST_URI};
41 $ENV{SCRIPT_NAME} = '' if $NO_SCRIPT_NAME;
43 my $ret = main($req, $req->request_method);
45 if (@ARGV && $ARGV[0] eq 'static') {
52 # private functions below
55 my ($cgi, $method) = @_;
57 if ($method !~ /\AGET|HEAD\z/) {
58 return r(405, 'Method Not Allowed');
60 my $path_info = $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, 0);
69 } elsif ($path_info =~ m!$LISTNAME_RE/atom\.xml\z!o) {
70 invalid_list(\%ctx, $1) || get_atom(\%ctx, $cgi, 0);
72 # single-message pages
73 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.txt\z!o) {
74 invalid_list_mid(\%ctx, $1, $2) || get_mid_txt(\%ctx, $cgi);
75 } elsif ($path_info =~ m!$LISTNAME_RE/m/(\S+)\.html\z!o) {
76 invalid_list_mid(\%ctx, $1, $2) || get_mid_html(\%ctx, $cgi);
79 } elsif ($path_info =~ m!$LISTNAME_RE/f/(\S+)\.html\z!o) {
80 invalid_list_mid(\%ctx, $1, $2) || get_full_html(\%ctx, $cgi);
82 # convenience redirects, order matters
83 } elsif ($path_info =~ m!$LISTNAME_RE/(?:m|f)/(\S+)\z!o) {
84 invalid_list_mid(\%ctx, $1, $2) || redirect_mid(\%ctx, $cgi);
91 sub r404 { r(404, 'Not Found') }
93 # simple response for errors
94 sub r { [ $_[0], ['Content-Type' => 'text/plain'], [ join(' ', @_, "\n") ] ] }
96 # returns undef if valid, array ref response if invalid
98 my ($ctx, $listname) = @_;
99 my $git_dir = $pi_config->get($listname, "mainrepo");
100 if (defined $git_dir) {
101 $ctx->{git_dir} = $git_dir;
102 $ctx->{listname} = $listname;
108 # returns undef if valid, array ref response if invalid
109 sub invalid_list_mid {
110 my ($ctx, $listname, $mid) = @_;
111 my $ret = invalid_list($ctx, $listname, $mid);
112 $ctx->{mid} = uri_unescape($mid) unless $ret;
116 # /$LISTNAME/atom.xml -> Atom feed, includes replies
118 my ($ctx, $cgi, $top) = @_;
119 require PublicInbox::Feed;
120 [ 200, [ 'Content-Type' => 'application/xml' ],
121 [ PublicInbox::Feed->generate({
122 git_dir => $ctx->{git_dir},
123 listname => $ctx->{listname},
124 pi_config => $pi_config,
131 # /$LISTNAME/?r=$GIT_COMMIT -> HTML only
133 my ($ctx, $cgi, $top) = @_;
134 require PublicInbox::Feed;
135 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
136 [ PublicInbox::Feed->generate_html_index({
137 git_dir => $ctx->{git_dir},
138 listname => $ctx->{listname},
139 pi_config => $pi_config,
146 # just returns a string ref for the blob in the current ctx
150 my $hex = Digest::SHA::sha1_hex($ctx->{mid});
151 $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
152 die "BUG: not a SHA-1 hex: $hex";
154 my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
155 qw(cat-file blob), "HEAD:$1/$2");
156 my $cmd = join(' ', @cmd);
157 my $pid = open my $fh, '-|';
158 defined $pid or die "fork failed: $!\n";
160 open STDERR, '>', '/dev/null'; # ignore errors
161 exec @cmd or die "exec failed: $!\n";
163 my $blob = eval { local $/; <$fh> };
165 $? == 0 ? \$blob : undef;
169 # /$LISTNAME/m/$MESSAGE_ID.txt -> raw original
171 my ($ctx, $cgi) = @_;
172 my $x = mid2blob($ctx);
173 $x ? [ 200, [ 'Content-Type' => 'text/plain' ], [ $$x ] ] : r404();
176 # /$LISTNAME/m/$MESSAGE_ID.html -> HTML content (short quotes)
178 my ($ctx, $cgi) = @_;
179 my $x = mid2blob($ctx);
180 return r404() unless $x;
182 require PublicInbox::View;
183 my $mid_href = PublicInbox::Hval::ascii_html(
184 uri_escape_utf8($ctx->{mid}));
185 my $pfx = "../f/$mid_href.html";
187 [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
188 [ PublicInbox::View->as_html(Email::MIME->new($$x), $pfx) ] ];
191 # /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
193 my ($ctx, $cgi) = @_;
194 my $x = mid2blob($ctx);
195 return r404() unless $x;
196 require PublicInbox::View;
198 [ 200, [ 'Content-Type' => 'text/html' ],
199 [ PublicInbox::View->as_html(Email::MIME->new($$x))] ];
204 ref($cgi) eq 'CGI' ? $cgi->self_url : $cgi->uri->as_string;
207 sub redirect_list_index {
208 my ($ctx, $cgi) = @_;
209 do_redirect(self_url($cgi) . "/");
213 my ($ctx, $cgi) = @_;
214 my $url = self_url($cgi);
216 do_redirect($url . '.html');
222 [ Location => $url, 'Content-Type' => 'text/plain' ],
223 [ "Redirecting to $url\n" ]
228 # preload so we are CoW friendly
229 require PublicInbox::Feed;
230 require PublicInbox::View;
231 require Mail::Thread;
234 require XML::Atom::SimpleFeed;
235 require Plack::Request;
237 my $req = Plack::Request->new(@_);
238 main($req, $req->method);
244 my ($status, $headers, $body) = @$ret;
247 301 => 'Moved Permanently',
249 405 => 'Method Not Allowed',
252 print "Status: $status $codes{$status}\r\n";
254 while (my ($k, $v) = splice(@tmp, 0, 2)) {
257 print "\r\n", $body->[0];