1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # wraps a listen socket for HTTP and links it to the PSGI app in
6 package PublicInbox::HTTPD;
11 use PublicInbox::HTTP;
12 use PublicInbox::HTTPD::Async;
14 sub pi_httpd_async { PublicInbox::HTTPD::Async->new(@_) }
17 my ($class, $sock, $app, $client) = @_;
18 my $n = getsockname($sock) or die "not a socket: $sock $!\n";
19 my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
25 'psgi.version' => [ 1, 1 ],
26 'psgi.errors' => \*STDERR,
27 'psgi.url_scheme' => $client->can('accept_SSL') ?
29 'psgi.nonblocking' => Plack::Util::TRUE,
30 'psgi.streaming' => Plack::Util::TRUE,
31 'psgi.run_once' => Plack::Util::FALSE,
32 'psgi.multithread' => Plack::Util::FALSE,
33 'psgi.multiprocess' => Plack::Util::TRUE,
35 # We don't use this anywhere, but we can support
36 # other PSGI apps which might use it:
37 'psgix.input.buffered' => Plack::Util::TRUE,
39 # XXX unstable API!, only GitHTTPBackend needs
40 # this to limit git-http-backend(1) parallelism.
41 # We also check for the truthiness of this to
42 # detect when to use async paths for slow blobs
43 'pi-httpd.async' => \&pi_httpd_async
45 bless { app => $app, env => \%env }, $class;
48 my %httpds; # per-listen-FD mapping for HTTPD->{env}->{SERVER_<NAME|PORT>}
49 my $default_app; # ugh...
53 eval { $default_app = Plack::Util::load_psgi(@ARGV) };
56 "$0 runs in /, command-line paths must be absolute\n";
59 require PublicInbox::WWW;
60 my $www = PublicInbox::WWW->new;
62 $default_app = builder {
63 eval { enable 'ReverseProxy' };
65 Plack::Middleware::ReverseProxy missing,
66 URL generation for redirects may be wrong if behind a reverse proxy
69 sub { $www->call(@_) };
72 %httpds = (); # invalidate cache
75 sub post_accept { # Listener->{post_accept}
76 my ($client, $addr, $srv) = @_; # $_[3] - tls_wrap (unused)
77 my $httpd = $httpds{fileno($srv)} //=
78 __PACKAGE__->new($srv, $default_app, $client);
79 PublicInbox::HTTP->new($client, $addr, $httpd),