about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2014-04-07 22:59:10 +0000
committerEric Wong <e@80x24.org>2014-04-07 22:59:10 +0000
commit29c9b0740bda4a4de8bb7ec538edc815654960a1 (patch)
treea6a832a6bfe97c1c3952890af4904a8a7875ed75
parent7c69497a0126cd613053d881d63586bead07dd0e (diff)
downloadpublic-inbox-29c9b0740bda4a4de8bb7ec538edc815654960a1.tar.gz
This should make it easier to support non-CGI uses,
as well as making it easier to generate static sites.
-rwxr-xr-xpublic-inbox-cgi75
1 files changed, 44 insertions, 31 deletions
diff --git a/public-inbox-cgi b/public-inbox-cgi
index ccfaae37..6cf70a9b 100755
--- a/public-inbox-cgi
+++ b/public-inbox-cgi
@@ -26,14 +26,33 @@ BEGIN {
         }
 }
 
+binmode STDOUT, ':utf8';
+
+my $ret = main();
+
+my ($status, $headers, $body) = @$ret;
+if (@ARGV && $ARGV[0] eq 'static') {
+        print $body;
+} else { # CGI
+        print "Status: $status\r\n";
+        while (my ($k, $v) = each %$headers) {
+                print "$k: $v\r\n";
+        }
+        print "\r\n", $body;
+}
+
+# TODO: plack support
+
+# private functions below
+
 sub main {
         my $cgi = CGI->new;
         if ($cgi->request_method !~ /\AGET|HEAD\z/) {
-                return r($cgi, "405 Method Not Allowed");
+                return r("405 Method Not Allowed");
         }
         my $path_info = decode_utf8($ENV{PATH_INFO});
         if ($path_info eq "/") {
-                r($cgi, "404 Not Found");
+                r("404 Not Found");
         } elsif ($path_info =~ m!$LISTNAME_RE/?\z!o) {
                 get_list_log($cgi, $1);
         } elsif ($path_info =~ m!$LISTNAME_RE/all\z!o) {
@@ -49,52 +68,46 @@ sub main {
         } elsif ($path_info =~ m!$LISTNAME_RE/mid/(\S+)\z!o) {
                 redirect_mid_html($cgi, $1, $2);
         } else {
-                r($cgi, "404 Not Found");
+                r("404 Not Found");
         }
 }
 
-binmode STDOUT, ':utf8';
-main();
-
 # simple response for errors
-sub r {
-        print $_[0]->header(-type => "text/plain",
-                                -status => $_[1],
-                                -charset => 'utf-8');
-}
+sub r { [ $_[0], { 'Content-Type' => 'text/plain' }, '' ] }
 
 # /$LISTNAME/all.atom.xml        -> Atom feed, includes replies
 sub get_atom_all {
         my ($cgi, $listname) = @_;
         my $git_dir = $pi_config->get($listname, "mainrepo");
-        defined $git_dir or return r($cgi, "404 Not Found");
+        defined $git_dir or return r("404 Not Found");
 
         require PublicInbox::Feed;
-        print $cgi->header(-type => "application/xml", -charset => 'us-ascii',
-                                -status => '200 OK');
-
-        print PublicInbox::Feed->generate({
-                git_dir => $git_dir,
-                pi_config => $pi_config,
-                listname => $listname,
-                cgi => $cgi
-        });
+        [ '200 OK',
+          { 'Content-Type' => 'application/xml; charset=us-ascii' },
+          PublicInbox::Feed->generate({
+                        git_dir => $git_dir,
+                        pi_config => $pi_config,
+                        listname => $listname,
+                        cgi => $cgi
+                })
+        ];
 }
 
 # /$LISTNAME/index.atom.xml        -> Atom feed
 sub get_atom_index {
         my ($cgi, $listname) = @_;
         my $git_dir = $pi_config->get($listname, "mainrepo");
-        defined $git_dir or return r($cgi, "404 Not Found");
+        defined $git_dir or return r("404 Not Found");
 
         require PublicInbox::Feed;
-        print $cgi->header(-type => "application/xml", -charset => 'us-ascii',
-                                -status => '200 OK');
-        print PublicInbox::Feed->generate({
-                git_dir => $git_dir,
-                pi_config => $pi_config,
-                listname => $listname,
-                cgi => $cgi,
-                top => 1
-        });
+        [ '200 OK',
+          { 'Content-Type' => 'application/xml; charset=us-ascii' },
+          PublicInbox::Feed->generate({
+                        git_dir => $git_dir,
+                        pi_config => $pi_config,
+                        listname => $listname,
+                        cgi => $cgi,
+                        top => 1
+                })
+        ];
 }