]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-httpd
httpd: remove unnecessary eval
[public-inbox.git] / script / public-inbox-httpd
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Standalone HTTP server for public-inbox.
6 use strict;
7 use warnings;
8 use Plack::Util;
9 use PublicInbox::Daemon;
10 use PublicInbox::HTTP;
11 use Plack::Request;
12 use Plack::Builder;
13 my %httpds;
14 my $app;
15 my $refresh = sub {
16         if (@ARGV) {
17                 eval { $app = Plack::Util::load_psgi(@ARGV) };
18                 if ($@) {
19                         die $@,
20 "$0 runs in /, command-line paths must be absolute\n";
21                 }
22         } else {
23                 require PublicInbox::WWW;
24                 PublicInbox::WWW->preload;
25                 my $www = PublicInbox::WWW->new;
26                 $app = builder {
27                         enable 'Chunked';
28                         eval {
29                                 enable 'Deflater',
30                                         content_type => [ qw(
31                                                 text/html
32                                                 text/plain
33                                                 application/atom+xml
34                                                 )]
35                         };
36                         $@ and warn
37 "Plack::Middleware::Deflater missing, bandwidth will be wasted\n";
38
39                         eval { enable 'ReverseProxy' };
40                         $@ and warn
41 "Plack::Middleware::ReverseProxy missing,\n",
42 "URL generation for redirects may be wrong if behind a reverse proxy\n";
43
44                         enable 'Head';
45                         sub { $www->call(@_) };
46                 };
47         }
48 };
49
50 PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
51         sub ($$$) { # post_accept
52                 my ($client, $addr, $srv) = @_;
53                 my $fd = fileno($srv);
54                 my $h = $httpds{$fd} ||= PublicInbox::HTTPD->new($srv, $app);
55                 PublicInbox::HTTP->new($client, $addr, $h),
56         });
57
58 1;
59
60 # XXX This is a totally unstable API for public-inbox internal use only
61 # This is exposed via the 'pi-httpd.async' key in the PSGI env hash.
62 # The name of this key is not even stable!
63 # Currently is is intended for use with read-only pipes.
64 package PublicInbox::HTTPD::Async;
65 use strict;
66 use warnings;
67 use base qw(Danga::Socket);
68 use fields qw(cb);
69
70 sub new {
71         my ($class, $io, $cb) = @_;
72         my $self = fields::new($class);
73         IO::Handle::blocking($io, 0);
74         $self->SUPER::new($io);
75         $self->{cb} = $cb;
76         $self->watch_read(1);
77         $self;
78 }
79
80 sub event_read { $_[0]->{cb}->() }
81 sub event_hup { $_[0]->{cb}->() }
82 sub event_err { $_[0]->{cb}->() }
83 sub sysread { shift->{sock}->sysread(@_) }
84
85 1;
86
87 package PublicInbox::HTTPD;
88 use strict;
89 use warnings;
90 use Plack::Util;
91
92 sub pi_httpd_async {
93         my ($io, $cb) = @_;
94         PublicInbox::HTTPD::Async->new($io, $cb);
95 }
96
97 sub new {
98         my ($class, $sock, $app) = @_;
99         my $n = getsockname($sock) or die "not a socket: $sock $!\n";
100         my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
101
102         my %env = (
103                 SERVER_NAME => $host,
104                 SERVER_PORT => $port,
105                 SCRIPT_NAME => '',
106                 'psgi.version' => [ 1, 1 ],
107                 'psgi.errors' => \*STDERR,
108                 'psgi.url_scheme' => 'http',
109                 'psgi.nonblocking' => Plack::Util::TRUE,
110                 'psgi.streaming' => Plack::Util::TRUE,
111                 'psgi.run_once'  => Plack::Util::FALSE,
112                 'psgi.multithread' => Plack::Util::FALSE,
113                 'psgi.multiprocess' => Plack::Util::TRUE,
114                 'psgix.harakiri'=> Plack::Util::FALSE,
115                 'psgix.input.buffered' => Plack::Util::TRUE,
116                 'pi-httpd.async' => do {
117                         no warnings 'once';
118                         *pi_httpd_async
119                 },
120         );
121         bless {
122                 app => $app,
123                 env => \%env,
124         }, $class;
125 }
126
127 1;