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(@_) }
88 $self->SUPER::close(@_);
93 package PublicInbox::HTTPD;
100 PublicInbox::HTTPD::Async->new($io, $cb);
104 my ($class, $sock, $app) = @_;
105 my $n = getsockname($sock) or die "not a socket: $sock $!\n";
106 my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
109 SERVER_NAME => $host,
110 SERVER_PORT => $port,
112 'psgi.version' => [ 1, 1 ],
113 'psgi.errors' => \*STDERR,
114 'psgi.url_scheme' => 'http',
115 'psgi.nonblocking' => Plack::Util::TRUE,
116 'psgi.streaming' => Plack::Util::TRUE,
117 'psgi.run_once' => Plack::Util::FALSE,
118 'psgi.multithread' => Plack::Util::FALSE,
119 'psgi.multiprocess' => Plack::Util::TRUE,
120 'psgix.harakiri'=> Plack::Util::FALSE,
121 'psgix.input.buffered' => Plack::Util::TRUE,
122 'pi-httpd.async' => do {