2 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # Standalone HTTP server for public-inbox.
9 use PublicInbox::Daemon;
10 use PublicInbox::HTTP;
17 eval { $app = Plack::Util::load_psgi(@ARGV) };
20 "$0 runs in /, command-line paths must be absolute\n";
23 require PublicInbox::WWW;
24 PublicInbox::WWW->preload;
25 my $www = PublicInbox::WWW->new;
37 "Plack::Middleware::Deflater missing, bandwidth will be wasted\n";
39 eval { enable 'ReverseProxy' };
41 "Plack::Middleware::ReverseProxy missing,\n",
42 "URL generation for redirects may be wrong if behind a reverse proxy\n";
45 sub { $www->call(@_) };
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),
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;
67 use base qw(Danga::Socket);
71 my ($class, $io, $cb) = @_;
72 my $self = fields::new($class);
73 IO::Handle::blocking($io, 0);
74 $self->SUPER::new($io);
80 sub event_read { $_[0]->{cb}->() }
81 sub event_hup { $_[0]->{cb}->() }
82 sub event_err { $_[0]->{cb}->() }
83 sub sysread { shift->{sock}->sysread(@_) }
87 package PublicInbox::HTTPD;
94 PublicInbox::HTTPD::Async->new($io, $cb);
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);
103 SERVER_NAME => $host,
104 SERVER_PORT => $port,
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 {