]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD.pm
public-inbox-netd: a multi-protocol superserver
[public-inbox.git] / lib / PublicInbox / HTTPD.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # wraps a listen socket for HTTP and links it to the PSGI app in
5 # public-inbox-httpd
6 package PublicInbox::HTTPD;
7 use v5.10.1;
8 use strict;
9 use Plack::Util ();
10 use Plack::Builder;
11 use PublicInbox::HTTP;
12 use PublicInbox::HTTPD::Async;
13
14 sub pi_httpd_async { PublicInbox::HTTPD::Async->new(@_) }
15
16 sub 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);
20
21         my %env = (
22                 SERVER_NAME => $host,
23                 SERVER_PORT => $port,
24                 SCRIPT_NAME => '',
25                 'psgi.version' => [ 1, 1 ],
26                 'psgi.errors' => \*STDERR,
27                 'psgi.url_scheme' => $client->can('accept_SSL') ?
28                                         'https' : 'http',
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,
34
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,
38
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
44         );
45         bless { app => $app, env => \%env }, $class;
46 }
47
48 my %httpds; # per-listen-FD mapping for HTTPD->{env}->{SERVER_<NAME|PORT>}
49 my $default_app; # ugh...
50
51 sub refresh {
52         if (@main::ARGV) {
53                 eval { $default_app = Plack::Util::load_psgi(@ARGV) };
54                 if ($@) {
55                         die $@,
56 "$0 runs in /, command-line paths must be absolute\n";
57                 }
58         } else {
59                 require PublicInbox::WWW;
60                 my $www = PublicInbox::WWW->new;
61                 $www->preload;
62                 $default_app = builder {
63                         eval { enable 'ReverseProxy' };
64                         $@ and warn <<EOM;
65 Plack::Middleware::ReverseProxy missing,
66 URL generation for redirects may be wrong if behind a reverse proxy
67 EOM
68                         enable 'Head';
69                         sub { $www->call(@_) };
70                 };
71         }
72         %httpds = (); # invalidate cache
73 }
74
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),
80 }
81
82 1;