]> Sergey Matveev's repositories - public-inbox.git/blobdiff - script/public-inbox-httpd
httpd: remove unnecessary eval
[public-inbox.git] / script / public-inbox-httpd
index 6109af01a62cdad79fdd11a3bdeea9652033f94d..e7ed3c9df4fa1e122dd1714cea5678e9d2e56ab6 100755 (executable)
@@ -23,27 +23,31 @@ my $refresh = sub {
                require PublicInbox::WWW;
                PublicInbox::WWW->preload;
                my $www = PublicInbox::WWW->new;
-               $app = eval {
-                       my $deflate_types = eval {
-                               require Plack::Middleware::Deflater;
-                               [ 'text/html', 'text/plain',
-                                       'application/atom+xml' ]
-                       };
-                       builder {
-                               enable 'Chunked';
-                               if ($deflate_types) {
-                                       enable 'Deflater',
-                                               content_type => $deflate_types
-                               }
-                               enable 'ReverseProxy';
-                               enable 'Head';
-                               sub { $www->call(@_) };
+               $app = builder {
+                       enable 'Chunked';
+                       eval {
+                               enable 'Deflater',
+                                       content_type => [ qw(
+                                               text/html
+                                               text/plain
+                                               application/atom+xml
+                                               )]
                        };
+                       $@ and warn
+"Plack::Middleware::Deflater missing, bandwidth will be wasted\n";
+
+                       eval { enable 'ReverseProxy' };
+                       $@ and warn
+"Plack::Middleware::ReverseProxy missing,\n",
+"URL generation for redirects may be wrong if behind a reverse proxy\n";
+
+                       enable 'Head';
+                       sub { $www->call(@_) };
                };
        }
 };
 
-daemon_run('0.0.0.0:8080', $refresh,
+PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
        sub ($$$) { # post_accept
                my ($client, $addr, $srv) = @_;
                my $fd = fileno($srv);
@@ -53,6 +57,10 @@ daemon_run('0.0.0.0:8080', $refresh,
 
 1;
 
+# XXX This is a totally unstable API for public-inbox internal use only
+# This is exposed via the 'pi-httpd.async' key in the PSGI env hash.
+# The name of this key is not even stable!
+# Currently is is intended for use with read-only pipes.
 package PublicInbox::HTTPD::Async;
 use strict;
 use warnings;
@@ -62,7 +70,7 @@ use fields qw(cb);
 sub new {
        my ($class, $io, $cb) = @_;
        my $self = fields::new($class);
-       $io->blocking(0);
+       IO::Handle::blocking($io, 0);
        $self->SUPER::new($io);
        $self->{cb} = $cb;
        $self->watch_read(1);
@@ -89,18 +97,10 @@ sub pi_httpd_async {
 sub new {
        my ($class, $sock, $app) = @_;
        my $n = getsockname($sock) or die "not a socket: $sock $!\n";
-       my ($port, $addr);
-       if (length($n) >= 28) {
-               require Socket6;
-               ($port, $addr) = Socket6::unpack_sockaddr_in6($n);
-       } else {
-               ($port, $addr) = Socket::unpack_sockaddr_in($n);
-       }
+       my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
 
        my %env = (
-               REMOTE_HOST => '',
-               REMOTE_PORT => 0,
-               SERVER_NAME => $addr,
+               SERVER_NAME => $host,
                SERVER_PORT => $port,
                SCRIPT_NAME => '',
                'psgi.version' => [ 1, 1 ],
@@ -119,8 +119,6 @@ sub new {
                },
        );
        bless {
-               err => \*STDERR,
-               out => \*STDOUT,
                app => $app,
                env => \%env,
        }, $class;