about summary refs log tree commit homepage
path: root/public-inbox.cgi
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-08-21 01:29:04 +0000
committerEric Wong <e@80x24.org>2015-08-21 10:25:28 +0000
commitf71e9e9b67a6ff23642ccd119390bd6b3cb0d91e (patch)
tree432b1afa027048b608797651719d97ff1beb7480 /public-inbox.cgi
parent6a8f5c169911d1ee9338bb77a3d5302deb86fdb4 (diff)
downloadpublic-inbox-f71e9e9b67a6ff23642ccd119390bd6b3cb0d91e.tar.gz
Some folks may not want to download and install Perl code like
ssoma, so allow downloading an mbox containing the entire
thread.
Diffstat (limited to 'public-inbox.cgi')
-rwxr-xr-xpublic-inbox.cgi43
1 files changed, 30 insertions, 13 deletions
diff --git a/public-inbox.cgi b/public-inbox.cgi
index ed0f12c8..5c6dfe8a 100755
--- a/public-inbox.cgi
+++ b/public-inbox.cgi
@@ -3,12 +3,22 @@
 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
 use strict;
 use warnings;
+use IO::Handle;
 require PublicInbox::WWW;
 use CGI qw/-nosticky/;
 our $NO_SCRIPT_NAME;
+our %HTTP_CODES;
 BEGIN {
         $NO_SCRIPT_NAME = 1 if $ENV{NO_SCRIPT_NAME};
         CGI->compile if $ENV{MOD_PERL};
+
+        %HTTP_CODES = (
+                200 => 'OK',
+                301 => 'Moved Permanently',
+                404 => 'Not Found',
+                405 => 'Method Not Allowed',
+                501 => 'Not Implemented',
+        );
 }
 
 # some servers (Ruby webrick) include scheme://host[:port] here,
@@ -19,23 +29,30 @@ delete $ENV{REQUEST_URI};
 $ENV{SCRIPT_NAME} = '' if $NO_SCRIPT_NAME;
 my $req = CGI->new;
 my $ret = PublicInbox::WWW::run($req, $req->request_method);
-binmode STDOUT;
+
+my $out = select;
+$out->binmode;
 if (@ARGV && $ARGV[0] eq 'static') {
-        print $ret->[2]->[0]; # only show the body
+        $out->write($ret->[2]->[0]); # only show the body
 } else { # CGI
-        my ($status, $headers, $body) = @$ret;
-        my %codes = (
-                200 => 'OK',
-                301 => 'Moved Permanently',
-                404 => 'Not Found',
-                405 => 'Method Not Allowed',
-                501 => 'Not Implemented',
-        );
+        if (ref($ret) eq 'CODE') {
+                $ret->(*dump_header);
+        } else {
+                my ($status, $headers, $body) = @$ret;
+
+                dump_header([$status, $headers])->write($body->[0]);
+        }
+}
 
-        print "Status: $status $codes{$status}\r\n";
+sub dump_header {
+        my ($res) = @_;
+        my $fh = select;
+        my ($status, $headers) = @$res;
+        $fh->write("Status: $status $HTTP_CODES{$status}\r\n");
         my @tmp = @$headers;
         while (my ($k, $v) = splice(@tmp, 0, 2)) {
-                print "$k: $v\r\n";
+                $fh->write("$k: $v\r\n");
         }
-        print "\r\n", $body->[0];
+        $fh->write("\r\n");
+        $fh;
 }