]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-httpd
daemon: introduce host_with_port for identifying sockets
[public-inbox.git] / script / public-inbox-httpd
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 #
5 # Standalone HTTP server for public-inbox.
6 use strict;
7 use warnings;
8 use Plack::Util;
9 use PublicInbox::Daemon;
10 use PublicInbox::HTTP;
11 use Plack::Request;
12 use Plack::Builder;
13 my %httpds;
14 my $app;
15 my $refresh = sub {
16         if (@ARGV) {
17                 eval { $app = Plack::Util::load_psgi(@ARGV) };
18                 if ($@) {
19                         die $@,
20 "$0 runs in /, command-line paths must be absolute\n";
21                 }
22         } else {
23                 require PublicInbox::WWW;
24                 PublicInbox::WWW->preload;
25                 my $www = PublicInbox::WWW->new;
26                 $app = eval {
27                         builder {
28                                 enable 'Chunked';
29                                 eval {
30                                         enable 'Deflater',
31                                                 content_type => [ qw(
32                                                         text/html
33                                                         text/plain
34                                                         application/atom+xml
35                                                         )]
36                                 };
37                                 $@ and warn
38 "Plack::Middleware::Deflater missing, bandwidth will be wasted\n";
39
40                                 eval { enable 'ReverseProxy' };
41                                 $@ and warn
42 "Plack::Middleware::ReverseProxy missing,\n",
43 "URL generation for redirects may be wrong if behind a reverse proxy\n";
44
45                                 enable 'Head';
46                                 sub { $www->call(@_) };
47                         };
48                 };
49         }
50 };
51
52 PublicInbox::Daemon::run('0.0.0.0:8080', $refresh,
53         sub ($$$) { # post_accept
54                 my ($client, $addr, $srv) = @_;
55                 my $fd = fileno($srv);
56                 my $h = $httpds{$fd} ||= PublicInbox::HTTPD->new($srv, $app);
57                 PublicInbox::HTTP->new($client, $addr, $h),
58         });
59
60 1;
61
62 # XXX This is a totally unstable API for public-inbox internal use only
63 # This is exposed via the 'pi-httpd.async' key in the PSGI env hash.
64 # The name of this key is not even stable!
65 # Currently is is intended for use with read-only pipes.
66 package PublicInbox::HTTPD::Async;
67 use strict;
68 use warnings;
69 use base qw(Danga::Socket);
70 use fields qw(cb);
71
72 sub new {
73         my ($class, $io, $cb) = @_;
74         my $self = fields::new($class);
75         IO::Handle::blocking($io, 0);
76         $self->SUPER::new($io);
77         $self->{cb} = $cb;
78         $self->watch_read(1);
79         $self;
80 }
81
82 sub event_read { $_[0]->{cb}->() }
83 sub event_hup { $_[0]->{cb}->() }
84 sub event_err { $_[0]->{cb}->() }
85 sub sysread { shift->{sock}->sysread(@_) }
86
87 1;
88
89 package PublicInbox::HTTPD;
90 use strict;
91 use warnings;
92 use Plack::Util;
93
94 sub pi_httpd_async {
95         my ($io, $cb) = @_;
96         PublicInbox::HTTPD::Async->new($io, $cb);
97 }
98
99 sub new {
100         my ($class, $sock, $app) = @_;
101         my $n = getsockname($sock) or die "not a socket: $sock $!\n";
102         my ($host, $port) = PublicInbox::Daemon::host_with_port($n);
103
104         my %env = (
105                 SERVER_NAME => $host,
106                 SERVER_PORT => $port,
107                 SCRIPT_NAME => '',
108                 'psgi.version' => [ 1, 1 ],
109                 'psgi.errors' => \*STDERR,
110                 'psgi.url_scheme' => 'http',
111                 'psgi.nonblocking' => Plack::Util::TRUE,
112                 'psgi.streaming' => Plack::Util::TRUE,
113                 'psgi.run_once'  => Plack::Util::FALSE,
114                 'psgi.multithread' => Plack::Util::FALSE,
115                 'psgi.multiprocess' => Plack::Util::TRUE,
116                 'psgix.harakiri'=> Plack::Util::FALSE,
117                 'psgix.input.buffered' => Plack::Util::TRUE,
118                 'pi-httpd.async' => do {
119                         no warnings 'once';
120                         *pi_httpd_async
121                 },
122         );
123         bless {
124                 app => $app,
125                 env => \%env,
126         }, $class;
127 }
128
129 1;