]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/HTTPD.pm
02f424c67138edca6af65835b1f2feb328d3a72d
[public-inbox.git] / lib / PublicInbox / HTTPD.pm
1 # Copyright (C) 2016-2021 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 strict;
8 use warnings;
9 use Plack::Util;
10 use PublicInbox::HTTPD::Async;
11 use PublicInbox::Daemon;
12
13 sub pi_httpd_async { PublicInbox::HTTPD::Async->new(@_) }
14
15 sub new {
16         my ($class, $sock, $app, $client) = @_;
17         my $n = getsockname($sock) or die "not a socket: $sock $!\n";
18         my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
19
20         my %env = (
21                 SERVER_NAME => $host,
22                 SERVER_PORT => $port,
23                 SCRIPT_NAME => '',
24                 'psgi.version' => [ 1, 1 ],
25                 'psgi.errors' => \*STDERR,
26                 'psgi.url_scheme' => $client->can('accept_SSL') ?
27                                         'https' : 'http',
28                 'psgi.nonblocking' => Plack::Util::TRUE,
29                 'psgi.streaming' => Plack::Util::TRUE,
30                 'psgi.run_once'  => Plack::Util::FALSE,
31                 'psgi.multithread' => Plack::Util::FALSE,
32                 'psgi.multiprocess' => Plack::Util::TRUE,
33
34                 # We don't use this anywhere, but we can support
35                 # other PSGI apps which might use it:
36                 'psgix.input.buffered' => Plack::Util::TRUE,
37
38                 # XXX unstable API!, only GitHTTPBackend needs
39                 # this to limit git-http-backend(1) parallelism.
40                 # We also check for the truthiness of this to
41                 # detect when to use async paths for slow blobs
42                 'pi-httpd.async' => \&pi_httpd_async
43         );
44         bless {
45                 app => $app,
46                 env => \%env
47         }, $class;
48 }
49
50 1;